{"id":2309,"date":"2026-09-15T07:02:02","date_gmt":"2026-09-14T23:02:02","guid":{"rendered":"https:\/\/xlwings.net\/blog\/?p=2309"},"modified":"2026-03-28T12:49:28","modified_gmt":"2026-03-28T12:49:28","slug":"how-to-use-worksheetcomments-in-the-xlwings-api-way","status":"publish","type":"post","link":"https:\/\/xlwings.net\/blog\/how-to-use-worksheetcomments-in-the-xlwings-api-way\/","title":{"rendered":"How to use Worksheet.Comments in the xlwings API way"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">The Worksheet.Comments property in xlwings provides access to the collection of all comments (also known as notes in newer Excel versions) within a specific worksheet. This property is essential for programmatically managing cell annotations, enabling developers to add, retrieve, modify, or delete comments. Comments are useful for adding explanatory notes, instructions, or feedback directly to cells, enhancing the interactivity and clarity of Excel workbooks. Through xlwings, you can automate comment-related tasks, such as bulk updates or extracting comment text for reporting purposes.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The syntax for accessing comments via xlwings is straightforward. The property returns a Comments object that represents a collection of individual Comment objects. You can reference it as: <code>sheet.comments<\/code>, where <code>sheet<\/code> is an xlwings Worksheet object. To interact with a specific comment, you can index it by its cell address or use iteration over all comments. Key methods and properties include:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>add(cell, text)<\/code>: Adds a new comment to a specified cell with the given text. The <code>cell<\/code> parameter can be a string (e.g., &#8220;A1&#8221;) or a tuple (e.g., (1, 1)), and <code>text<\/code> is a string containing the comment content.<\/li>\n\n\n\n<li><code>count<\/code>: Returns the number of comments in the worksheet.<\/li>\n\n\n\n<li><code>item(index)<\/code>: Retrieves a Comment object by index (0-based) or by cell address.<\/li>\n\n\n\n<li><code>clear()<\/code>: Removes all comments from the worksheet.<br>Each Comment object has properties like <code>text<\/code> (to get or set the comment text), <code>author<\/code> (to get or set the author name), and <code>delete()<\/code> (to remove the comment). Note that in Excel, comments and notes are distinct; xlwings primarily handles traditional comments, but it may adapt based on the Excel version.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">For example, consider a scenario where you need to add a comment to cell B5 with a reminder and then list all comments in the worksheet. Here is a code instance using xlwings:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import xlwings as xw\n\n# Open an existing workbook and select a worksheet\nwb = xw.Book('example.xlsx')\nsheet = wb.sheets&#91;'Sheet1']\n\n# Add a new comment to cell B5\nsheet.comments.add('B5', 'Review this value for accuracy.')\n\n# Get the count of comments\ncomment_count = sheet.comments.count\nprint(f\"Total comments: {comment_count}\")\n\n# Iterate through all comments and print their details\nfor comment in sheet.comments:\n    print(f\"Cell: {comment.cell.address}, Text: {comment.text}, Author: {comment.author}\")\n\n# Modify an existing comment in cell A3 (if it exists)\nif sheet.comments.count > 0:\n    # Assuming A3 has a comment; you can check with try-except or conditions\n    try:\n        comment_a3 = sheet.comments&#91;'A3']\n        comment_a3.text = 'Updated note: Please verify.'\n    except Exception as e:\n        print(f\"Comment not found in A3: {e}\")\n\n# Delete a comment from cell B5\nsheet.comments&#91;'B5'].delete()\n\n# Clear all comments from the worksheet\nsheet.comments.clear()\n\n# Save and close\nwb.save()\nwb.close()<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The Worksheet.Comments property in xlwings provides access to the collection of all comments (also k&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[25],"tags":[],"class_list":["post-2309","post","type-post","status-publish","format-standard","hentry","category-xlwings-api-reference"],"_links":{"self":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2309","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/comments?post=2309"}],"version-history":[{"count":2,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2309\/revisions"}],"predecessor-version":[{"id":3521,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2309\/revisions\/3521"}],"wp:attachment":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/media?parent=2309"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/categories?post=2309"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/tags?post=2309"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}