MS Excel supports adding comments to the cells in the worksheets to provide additional information. In various cases, comments are used to explain a formula. Moreover, MS Excel allows you to define the font size, height, width, etc. of the comments. In this article, you will learn how to add comments to the Excel worksheets programmatically using Python.
Python API to Add Comments in Excel
In order to add comments to the cells in Excel worksheets, we will use Aspose.Cells for Python via Java. The API lets you create, modify, and convert Excel files from within your Python applications. You can either download the API or install it using the following pip command.
pip install aspose-cells
Add Comments to an Excel Worksheet in Python
The following are the steps to add a comment to a cell in an Excel worksheet using Python.
- Load the Excel file using Workbook class.
- Get reference of the desired Worksheet using Workbook.getWorksheets().get(index) method.
- Add comment to the desired cell using Worksheet.getComments().add(string cellName) method and get the reference of the comment in a Comment object.
- Set comment’s note using Comment.setNote() method.
- Save the updated Excel file using Workbook.save(string) method.
The following code sample shows how to add a comment in an Excel worksheet using Python.
Apply Formatting to Comments in Excel
The following are the steps to apply formatting to the comments in Excel using Python.
- Load the Excel file using Workbook class.
- Get reference of the desired Worksheet using Workbook.getWorksheets().get(index) method.
- Add comment to the cell using Worksheet.getComments().add(string cellName) method and get the reference of the comment in a Comment object.
- Set comment’s note using Comment.setNote() method.
- Set desired formatting of the comment using Comment object.
- Save the updated Excel file using Workbook.save(string) method.
The following code sample shows how to set the formatting of the comments in Excel.
Get a Free License
You can use Aspose.Cells for Python via Java without evaluation limitations using a temporary license.
Conclusion
In this article, you have learned how to add comments to the cells in Excel worksheets using Python. Furthermore, you have seen how to apply formatting to the comments programmatically. You can explore other features of the API using the documentation. In case you would have any queries, feel free to post to our forum.