How to use Application.DisplayCommentIndicator in the xlwings API way

The DisplayCommentIndicator property in the Excel Application object determines how cell comments (also known as notes) are visually indicated within a worksheet. This setting is crucial for controlling the visibility of comment indicators, which are the small red triangles typically found in the top-right corner of cells containing comments. By adjusting this property, users can tailor the display to suit different workflows, such as hiding indicators for a cleaner view or showing them only when comments are present. This property is particularly useful in collaborative environments where comment tracking is essential, or when preparing reports where visual clutter should be minimized.

In xlwings, the DisplayCommentIndicator property can be accessed through the Application object. The syntax for using this property is straightforward: app.display_comment_indicator. Here, app refers to an instance of the xlwings App class, which represents the Excel application. The property accepts integer values that correspond to specific display modes, as defined in Excel’s object model. The possible values and their meanings are as follows:

  • -1: Displays comment indicators only when comments are present (default). This is the standard setting where red triangles appear in cells with comments.
  • 0: Hides comment indicators entirely. In this mode, no visual cues are shown, even if cells contain comments, which can make the worksheet look cleaner but may hide important annotations.
  • 1: Always shows comment indicators, regardless of whether comments are present. This mode is less common but can be used for consistency in certain templates or to highlight cells intended for comments.

To set or retrieve the DisplayCommentIndicator property in xlwings, you first need to establish a connection to the Excel application. For example, you can use xlwings.App() to launch a new instance or connect to an existing one. Once the application object is available, you can directly assign or read the property value. This property is application-wide, meaning it affects all open workbooks and worksheets in that Excel instance. It is important to note that changes made via xlwings are immediately reflected in the Excel interface, allowing for dynamic adjustments during automation scripts.

Here are some practical xlwings API code examples demonstrating the use of the DisplayCommentIndicator property:

  1. Retrieving the current display setting:
import xlwings as xw
app = xw.App(visible=True) # Start or connect to Excel
current_setting = app.display_comment_indicator
print(f"Current DisplayCommentIndicator setting: {current_setting}")
# This will output -1, 0, or 1 based on the current configuration.
app.quit() # Close the application
  1. Hiding comment indicators for a cleaner view:
import xlwings as xw
app = xw.App(visible=True)
app.display_comment_indicator = 0 # Hide indicators
print("Comment indicators are now hidden.")
# You can open a workbook here to see the effect, e.g., app.books.open('example.xlsx')
app.quit()
  1. Showing indicators only when comments exist (default reset):
import xlwings as xw
app = xw.App(visible=False) # Run in background
app.display_comment_indicator = -1 # Set to default
print("DisplayCommentIndicator reset to default (show only with comments).")
app.quit()
  1. Always displaying comment indicators for consistency:
import xlwings as xw
with xw.App(visible=True) as app: # Using context manager for automatic cleanup
app.display_comment_indicator = 1 # Always show indicators
wb = app.books.open('sample.xlsx')
# The workbook will now show red triangles in all cells, even empty ones.
wb.save()
# The setting persists until changed or Excel is restarted.

May 23, 2026 (0)


Leave a Reply

Your email address will not be published. Required fields are marked *