How to use Range.ClearOutline in the xlwings API way

The ClearOutline member of the Range object in the Excel object model is a method used to remove any grouping or outlining that has been applied to a specified range of cells. Outlining in Excel allows users to collapse and expand rows or columns to hide or show detail data, often used in summarizing reports. The ClearOutline method effectively clears these groupings, returning the range to a flat, ungrouped state without altering the actual data within the cells. This can be useful when preparing data for further analysis or when resetting the view of a worksheet.

In xlwings, the ClearOutline method is accessed through the api property of a Range object, which provides direct access to the underlying Excel object model. The syntax for calling this method in xlwings is straightforward, as it does not require any parameters. It is invoked on a Range object that represents the cells from which you want to remove outlining.

Syntax:

range_object.api.ClearOutline()
  • range_object: This is an xlwings Range object, typically obtained using sheet.range() or similar methods, specifying the cells (e.g., by address like “A1:D10”) where outlining should be cleared.
  • The method takes no arguments; it directly acts on the range to clear any existing row or column outlines.

Example Usage:
Suppose you have an Excel workbook where rows 2 to 10 are grouped together in an outline (e.g., with a collapse/expand button). To clear this outlining using xlwings, you can use the following code:

import xlwings as xw

# Connect to the active Excel instance or open a workbook
app = xw.apps.active
wb = app.books['YourWorkbook.xlsx'] # Replace with your workbook name
sheet = wb.sheets['Sheet1'] # Replace with your sheet name

# Define the range that has outlining (e.g., rows 2 to 10)
target_range = sheet.range('2:10') # This selects entire rows 2 through 10

# Clear the outlining from the specified range
target_range.api.ClearOutline()

# Save and close if needed
wb.save()
wb.close()

October 20, 2026 (0)


Leave a Reply

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