Python OpenPyXL Add Comment to Excel Cells
In this Python openpyxl tutorial we learn how to add comments to Excel xlsx cells using the openpyxl package.
How to install the openpyxl package
To install the openpyxl package using the command below.
pip install openpyxl
How to add comment to Excel cells
Import the Workbook and Comment from the openpyxl package.
from openpyxl import Workbook
from openpyxl.comments import Comment
Create a new Workbook object.
wb = Workbook()
Get the default active Worksheet object.
ws = wb.active
Create a new Comment object with text content and author values.
comment = Comment(text='Python Tutorial', author='Tori Code')
Assign the comment to A1 cell.
ws['A1'].comment = comment
Then save the Workbook as add_comment.xlsx file.
wb.save('add_comment.xlsx')
The complete Python program as below.
add_comment.py
from openpyxl import Workbook
from openpyxl.comments import Comment
wb = Workbook()
ws = wb.active
comment = Comment(text='Python Tutorial', author='Tori Code')
ws['A1'].comment = comment
wb.save('add_comment.xlsx')
Execute the above Python program we will get the Excel file add_comment.xlsx as below screenshot.
Happy Coding 😊