Last modified: Nov 14, 2025 By Alexander Williams
Add Comments to Excel Cells Using Python openpyxl
Excel comments help explain data. They provide context. Python openpyxl makes adding comments easy. This guide shows you how.
You will learn to add, edit, and format comments. We cover practical examples. Let's start with the basics.
What Are Excel Cell Comments?
Comments are notes attached to cells. They appear when you hover over a cell. Comments explain data or provide instructions.
They are useful for documentation. Teams can understand data better. Comments make spreadsheets more informative.
Installing openpyxl
First, install openpyxl. Use pip for installation. Run this command in your terminal.
pip install openpyxl
This installs the latest version. Now you can work with Excel files. You can read, write, and modify them.
Basic Comment Creation
Let's create a simple comment. Use the Comment class. Assign it to a cell's comment property.
from openpyxl import Workbook
from openpyxl.comments import Comment
# Create a new workbook
wb = Workbook()
ws = wb.active
# Create a comment
comment_text = "This is the total sales for Q1"
comment = Comment(comment_text, "Author Name")
# Assign comment to cell B5
ws['B5'].comment = comment
# Save the workbook
wb.save("workbook_with_comment.xlsx")
This code adds a comment to cell B5. The comment appears when you hover over it. The author name shows who created it.
Comment Properties and Customization
Comments have several properties. You can customize them. Change the text, author, and size.
from openpyxl import Workbook
from openpyxl.comments import Comment
wb = Workbook()
ws = wb.active
# Create a customized comment
comment = Comment("Monthly revenue exceeded targets", "Finance Team")
comment.width = 200
comment.height = 50
ws['C10'].comment = comment
wb.save("customized_comment.xlsx")
This sets specific dimensions. The comment box will be 200 pixels wide. It will be 50 pixels tall.
Working with Multiple Comments
You can add comments to multiple cells. Use loops for efficiency. This is helpful for bulk operations.
from openpyxl import Workbook
from openpyxl.comments import Comment
wb = Workbook()
ws = wb.active
# Sample data
data = [
["Product", "Sales", "Comment"],
["Laptop", 1500, "Best selling product"],
["Mouse", 300, "Steady sales"],
["Keyboard", 450, "New model performing well"]
]
# Add data and comments
for row_idx, row_data in enumerate(data, 1):
for col_idx, value in enumerate(row_data, 1):
ws.cell(row=row_idx, column=col_idx, value=value)
# Add comment to sales cells (column 2)
if row_idx > 1: # Skip header row
sales_cell = ws.cell(row=row_idx, column=2)
comment_text = f"Sales figure for {data[row_idx-1][0]}"
sales_cell.comment = Comment(comment_text, "Sales Manager")
wb.save("multiple_comments.xlsx")
This adds comments to all sales cells. Each comment references the product name. It makes data tracking easier.
Reading and Modifying Existing Comments
You can read existing comments. Modify them as needed. This is useful for updating documentation.
from openpyxl import load_workbook
# Load existing workbook
wb = load_workbook("multiple_comments.xlsx")
ws = wb.active
# Read and modify comments
for row in ws.iter_rows():
for cell in row:
if cell.comment:
print(f"Cell {cell.coordinate}: {cell.comment.text}")
# Update comment text
cell.comment.text += " - Updated on 2024"
cell.comment.author = "Updated By System"
wb.save("updated_comments.xlsx")
This code reads all comments. It appends text to each one. It also updates the author name.
Comment Best Practices
Follow these best practices. They make your comments more effective.
Keep comments concise. Long comments can be overwhelming. Use clear, simple language.
Use meaningful author names. Identify who added the comment. This helps with accountability.
Update comments regularly. Outdated comments cause confusion. Remove or update them as needed.
Integration with Other openpyxl Features
Comments work well with other openpyxl features. Combine them with formatting for better spreadsheets.
Use comments with column width settings. This creates well-organized sheets. Data becomes easier to understand.
Combine with sheet protection. Protect important comments from accidental changes. Maintain data integrity.
Use with table formatting. Create professional-looking reports. Comments add valuable context.
Common Use Cases
Comments serve many purposes. Here are common scenarios.
Data validation explanations. Explain why data might look unusual. Provide context for outliers.
Calculation notes. Explain complex formulas. Help others understand your methodology.
Review feedback. Team members can add suggestions. Collaborate effectively on spreadsheets.
Error Handling
Handle errors gracefully. Check if comments exist before modifying them.
from openpyxl import load_workbook
wb = load_workbook("example.xlsx")
ws = wb.active
cell = ws['A1']
# Safe comment modification
if cell.comment:
cell.comment.text = "Updated comment"
else:
print("No comment found in cell A1")
wb.save("safe_update.xlsx")
This prevents errors. It checks for existing comments first. Then it updates them safely.
Performance Considerations
Comments affect file size. Large numbers of comments can slow performance.
For large files, consider efficient handling techniques. Use read-only mode when only reading comments.
Batch process comments when possible. Avoid opening and closing files repeatedly.
Conclusion
Cell comments are powerful tools. They add valuable context to your data. Python openpyxl makes comment management easy.
You can create, read, and update comments. Combine them with other Excel features. Create more informative spreadsheets.
Start adding comments to your Excel files today. Improve your data documentation. Make your spreadsheets more useful for everyone.