Open jackjindtnt opened 7 years ago
Adding a comment to a cell Comments have a text attribute and an author attribute, which must both be set
from openpyxl import Workbook from openpyxl.comments import Comment wb = Workbook() ws = wb.active comment = ws["A1"].comment comment = Comment('This is the comment text', 'Comment Author') comment.text 'This is the comment text' comment.author 'Comment Author' If you assign the same comment to multiple cells then openpyxl will automatically create copies
from openpyxl import Workbook from openpyxl.comments import Comment wb=Workbook() ws=wb.active comment = Comment("Text", "Author") ws["A1"].comment = comment ws["B2"].comment = comment ws["A1"].comment is comment True ws["B2"].comment is comment False
Create a workbook There is no need to create a file on the filesystem to get started with openpyxl. Just import the Workbook class and start using it
This function uses the _active_sheet_index property, set to 0 by default. Unless you modify its value, you will always get the first worksheet by using this method. You can also create new worksheets by using the openpyxl.workbook.Workbook.create_sheet() method
ws.title = "New Title" The background color of the tab holding this title is white by default. You can change this providing an RRGGBB color code to the sheet_properties.tabColor property:
ws.sheet_properties.tabColor = "1072BA" Once you gave a worksheet a name, you can get it as a key of the workbook:
openpyxl.workbook.Workbook.copy_worksheet() method:
Only cells and styles can be copied. You cannot copy worksheets between workbooks. You can copy worksheets in a workbook with the
Playing with data Accessing one cell Now we know how to access a worksheet, we can start modifying cells content.
Cells can be accessed directly as keys of the worksheet
This provides access to cells using row and column notation:
When a worksheet is created in memory, it contains no cells. They are created when first accessed. Warning
Because of this feature, scrolling through cells instead of accessing them directly will create them all in memory, even if you don’t assign them a value.
Something like
This operation will overwrite existing files without warning. Note
Extension is not forced to be xlsx or xlsm, although you might have some trouble opening it directly with another application if you don’t use an official extension.
As OOXML files are basically ZIP files, you can also end the filename with .zip and open it with your favourite ZIP archive manager. You can specify the attribute template=True, to save a workbook as a template:
You should monitor the data attributes and document extensions for saving documents in the document templates and vice versa, otherwise the result table engine can not open the document. Note
The following will fail: