”;
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.
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.
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 individual 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 Charlie Clark'' ws.write_comment(''C5'', text, {''visible'':True}) wb.close()
Output
It will produce the following output −
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 −
”;