English 中文(简体)
Python XlsxWriter - Cell Comments
  • 时间:2024-09-17

Python XlsxWriter - Cell Comments


Previous Page Next Page  

In an Excel worksheet, comments can be inserted for various reasons. One of the uses is to explain a formula in a cell. Also, Excel comments also serve as reminders or notes for other users. They are useful for cross-referencing with other Excel workbooks.

From Excel s menu system, comment feature is available on Review menu in the ribbon.

Comment

To add and format comments, XlsxWriter has add_comment() method. Two mandatory parameters for this method are cell location (either in A1 type or row and column number), and the comment text.

Example

Here is a simple example −


import xlsxwriter

wb = xlsxwriter.Workbook( hello.xlsx )
ws = wb.add_worksheet()

data= XlsxWriter Library 

ws.set_column( C:C , 25)
ws.set_row(2, 50)
ws.write( C3 , data)

text =  Developed by John McNamara 
ws.write_comment( C3 , text)

wb.close()

Output

When we open the workbook, a comment will be seen with a marker on top right corner of C3 cell when the cursor is placed in it.

Comment1

By default, the comments are not visible until the cursor hovers on the cell in which the comment is written. You can either show all the comments in a worksheet by invoking show_comment() method of worksheet object, or setting visible property of inspanidual comment to True.


ws.write_comment( C3 , text, { visible : True})

Example

In the following code, there are three comments placed. However, the one in cell C3 is has been configured with visible property set to False. Hence, it cannot be seen until the cursor is placed in the cell.


import xlsxwriter

wb = xlsxwriter.Workbook( hello.xlsx )
ws = wb.add_worksheet()

ws.show_comments()

data= Python 
ws.set_column( C:C , 25)
ws.set_row(0, 50)
ws.write( C1 , data)
text =  Programming language developed by Guido Van Rossum 
ws.write_comment( C1 , text)
data=  XlsxWriter 
ws.set_row(2, 50)
ws.write( C3 , data)
text =  Developed by John McNamara 
ws.write_comment( C3 , text, { visible :False})
data=  OpenPyXl 
ws.set_row(4, 50)
ws.write( C5 , data)
text =  Developed by Eric Gazoni and Charpe Clark 
ws.write_comment( C5 , text, { visible :True})

wb.close()

Output

It will produce the following output −

Show Comment

You can set author option to indicate who is the author of the cell comment. The author of the comment is also displayed in the status bar at the bottom of the worksheet.


worksheet.write_comment( C3 ,  Atonement , { author :  Tutorialspoint })

The default author for all cell comments can be set using the set_comments_author() method −


worksheet.set_comments_author( Tutorialspoint )

It will produce the following output −

Set Comments Author Advertisements