How to use Application.ProductCode in the xlwings API way

The ProductCode property of the Application object in Excel’s object model is a read-only property that returns a globally unique identifier (GUID) for the installed Microsoft Excel product. This GUID is a string that uniquely identifies the specific version and edition of Excel, such as whether it is a retail, volume-licensed, or OEM version. This property is particularly useful for developers and system administrators who need to programmatically identify or verify the Excel installation on a machine, for example, in software deployment, licensing checks, or compatibility validations within automated scripts or applications.

In xlwings, the Application object is accessed through the app property of a Book object or directly when starting an application. The ProductCode property can be called as an attribute on the app object. The syntax is straightforward, as it does not take any parameters:

product_code = app.api.ProductCode

Here, app refers to the xlwings App instance, and .api is used to access the underlying Excel Application object from the COM interface. The property returns a string representing the ProductCode GUID. Note that this property is specific to the Excel application instance, so it will reflect the version of Excel that xlwings is connected to.

For example, to retrieve the ProductCode of an active Excel instance using xlwings, you can use the following code. This example assumes Excel is already running or will be started by xlwings:

import xlwings as xw

# Connect to the active Excel instance or start a new one
app = xw.apps.active if xw.apps.active else xw.App(visible=True)

# Get the ProductCode property
product_code = app.api.ProductCode
print(f"Excel ProductCode: {product_code}")

# Optionally, close the app if it was started for this purpose
if not xw.apps.active:
    app.quit()

In this code, xw.apps.active is used to attach to an existing Excel application; if none exists, a new one is created with xw.App(visible=True). The app.api.ProductCode call retrieves the GUID, which is then printed. The GUID typically looks something like {90160000-0011-0000-1000-0000000FF1CE} for an Office 2016 version, but it varies by installation. This output can be used to identify the Excel product programmatically.

Another practical use case is to check the ProductCode in a script that requires a specific Excel version. For instance, you might want to ensure compatibility before proceeding with automation tasks:

import xlwings as xw

app = xw.apps.active if xw.apps.active else xw.App(visible=False)
expected_product_code = "{90160000-0011-0000-1000-0000000FF1CE}" # Example for Office 2016

if app.api.ProductCode == expected_product_code:
    print("Compatible Excel version detected. Proceeding with automation.")
    # Add your automation code here
else:
    print(f"Incompatible Excel version. ProductCode: {app.api.ProductCode}")

app.quit()

July 4, 2026 (0)


Leave a Reply

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