Python Xlsxwriter 简明教程
Python XlsxWriter - Cell Comments
在 Excel 工作表中,可以出于各种原因插入 comments 。一种用法是解释单元格中的公式。此外,Excel 批注还可以作为其他用户提醒或笔记。它们对于与其他 Excel 工作簿交叉引用很有用。
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.
从 Excel 的菜单系统,可在功能区中的“审阅”菜单中找到批注功能。
From Excel’s menu system, comment feature is available on Review menu in the ribbon.

若要添加和设置批注的格式,XlsxWriter 具有 add_comment() 方法。该方法的两个必需参数是 cell location (A1 类型或行号和列号)和 comment text 。
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
当我们打开工作簿,然后将光标置于 C3 单元格顶部右侧的标记上时,将看到一个带有批注。
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.

默认情况下,批注不可见,直到光标悬停在写入批注的单元格上。可以通过调用工作表对象的 show_comment() 方法或者将单个批注的可见属性设置为 True 来显示工作表中的所有批注。
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
在以下代码中,放置了三条批注。但是,C3单元格中的一个已配置为可见属性设置为 False。因此,在光标置于该单元格之前看不到它。
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 −

可以设置 author 选项以指示单元格批注的作者。批注的作者还显示在工作表底部的状态栏中。
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'})
可以使用 set_comments_author() 方法设置所有单元格批注的默认作者:
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 −
