How to use Worksheets.Creator in the xlwings API way

The Creator property of the Worksheets object in the Excel object model is a read-only property that returns a 32-bit integer indicating the application in which the specified object was created. This property is primarily used to identify the creator application, especially in scenarios involving OLE (Object Linking and Embedding) automation or when working with objects that might be shared across different applications (e.g., between Microsoft Excel and another application like Microsoft Word). In xlwings, you can access this property to retrieve this creator code, which can be useful for debugging, logging, or conditional logic based on the originating application.

Functionality:
The main purpose of the Creator property is to provide a unique identifier for the application that created the Excel workbook or specific object. This can be particularly helpful in macro-enabled environments or when integrating Excel with other Office applications through COM automation. For the Worksheets object, it refers to the collection of all worksheets in a workbook, and accessing its Creator property returns the creator code for the entire workbook’s worksheet collection. Note that this property is inherited from the base object model and is not commonly used in everyday xlwings scripting, but it can be valuable in advanced automation tasks.

Syntax:
In xlwings, you can access the Creator property via the api property of a workbook or worksheet object, which exposes the underlying Excel object model. The general syntax is as follows:

workbook_or_worksheet_object.api.Creator

  • workbook_or_worksheet_object: This is an xlwings object representing a workbook or a specific worksheet. For the Worksheets object, you typically access it through a workbook.
  • api: This property provides direct access to the pywin32 or appscript object, allowing you to call native Excel VBA object model properties and methods.
  • Creator: This is the property name, and it does not take any parameters. It returns a Long (32-bit integer) value.

The return value is an integer code. For Microsoft Excel, the standard creator code is 1480803660 (hexadecimal: 0x5843454C), which corresponds to “XCEL” in ASCII. Other applications have different codes; for example, Microsoft Word uses 1297307460 (hexadecimal: 0x4D535744 for “MSWD”).

Examples:
Here are some xlwings code examples demonstrating how to use the Creator property with the Worksheets object:

  1. Accessing Creator for the entire Worksheets collection in a workbook:
    This example opens an Excel workbook and retrieves the creator code for its worksheets collection.
import xlwings as xw

# Open an existing workbook
wb = xw.Book('example.xlsx')

# Access the Worksheets object's Creator property via the workbook's api
creator_code = wb.api.Worksheets.Creator

# Print the creator code
print(f"Creator code for the worksheets collection: {creator_code}")

# Check if it was created by Excel
if creator_code == 1480803660:
    print("This workbook was created by Microsoft Excel.")
else:
    print("This workbook was created by another application.")
  1. Accessing Creator for a specific worksheet within the collection:
    This example shows how to get the creator code for a particular worksheet, which will be the same as the workbook’s creator since worksheets are part of the Excel file.
import xlwings as xw

# Open a workbook and select a specific worksheet
wb = xw.Book('example.xlsx')
ws = wb.sheets['Sheet1'] # xlwings sheet object

# Access the Creator property via the worksheet's underlying api
creator_code = ws.api.Creator # This accesses the Worksheet object's Creator

# Alternatively, access through the Worksheets collection
creator_code_via_collection = wb.api.Worksheets('Sheet1').Creator

print(f"Creator code for Sheet1: {creator_code}")
print(f"Creator code via collection: {creator_code_via_collection}")
  1. Using Creator in a loop to inspect all worksheets:
    This example iterates through all worksheets in a workbook and logs their creator codes, which should be consistent across all sheets.
import xlwings as xw

wb = xw.Book('example.xlsx')

for sheet in wb.sheets:
    creator = sheet.api.Creator
    sheet_name = sheet.name
    print(f"Worksheet '{sheet_name}' has creator code: {creator}")

# Close the workbook if needed
wb.close()

August 24, 2026 (0)


Leave a Reply

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