How to use Worksheet.Type in the xlwings API way

The Type member of the Worksheet object in the Excel object model represents the type of sheet, such as a standard worksheet, a chart sheet, or a macro sheet. In xlwings, this property is accessible via the api property, which provides direct access to the underlying Excel object model. This is particularly useful when you need to programmatically determine the nature of a sheet within a workbook, enabling conditional logic based on sheet type.

Functionality:
The primary function is to identify the sheet type. Common values include:

  • xlWorksheet (a standard worksheet, value -4167)
  • xlChart (a chart sheet, value -4109)
  • xlExcel4MacroSheet (an Excel version 4 macro sheet, value 3)
  • xlExcel4IntlMacroSheet (an international macro sheet, value 4)

Syntax:
The property is accessed through the xlwings Sheet object’s api property. The Sheet object in xlwings corresponds to both worksheets and chart sheets. The syntax is:

sheet_type = sheet.api.Type

Where sheet is an xlwings Sheet object. The Type property is read-only and returns an integer value (a XlSheetType enumeration constant).

Example:
The following xlwings code demonstrates how to check the type of each sheet in the active workbook and print a corresponding message. It uses the Type property via the api interface.

import xlwings as xw

# Connect to the active Excel instance and workbook
app = xw.apps.active
wb = app.books.active

# Iterate through all sheets in the workbook
for sheet in wb.sheets:
    sheet_type = sheet.api.Type

if sheet_type == -4167: # xlWorksheet
    print(f"Sheet '{sheet.name}' is a standard worksheet.")
elif sheet_type == -4109: # xlChart
    print(f"Sheet '{sheet.name}' is a chart sheet.")
elif sheet_type == 3: # xlExcel4MacroSheet
    print(f"Sheet '{sheet.name}' is an Excel 4 macro sheet.")
elif sheet_type == 4: # xlExcel4IntlMacroSheet
    print(f"Sheet '{sheet.name}' is an international macro sheet.")
else:
    print(f"Sheet '{sheet.name}' has an unknown sheet type: {sheet_type}")

# Example to check a specific sheet's type
target_sheet = wb.sheets['Sheet1']
if target_sheet.api.Type == -4167:
    print(f"\n'{target_sheet.name}' is confirmed to be a worksheet. Proceeding with data operations.")
    # Perform worksheet-specific tasks here, such as data manipulation
    target_sheet.range('A1').value = 'Data Entry Point'

October 7, 2026 (0)


Leave a Reply

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