How to use Application.AddIns2 in the xlwings API way

In the Excel object model, the Application.AddIns2 property returns an AddIns2 collection that represents all the add-ins currently available to Excel, including both installed add-ins and those that are simply listed in the add-in manager. This collection is more modern than the older AddIns collection, as it includes both COM add-ins and automation add-ins. In xlwings, you can access this property to inspect, manage, or manipulate Excel add-ins programmatically using Python. This is particularly useful for automating tasks that involve checking add-in availability, loading or unloading add-ins, or retrieving information about them for administrative or development purposes.

The xlwings API provides a straightforward way to interact with the Application.AddIns2 property. The syntax for accessing it is through the app object, which represents the Excel application. Specifically, you can use app.api.AddIns2 to get the underlying COM object, allowing you to call its methods and properties. The AddIns2 collection has members such as Count, Item, and Add, which can be used to iterate over add-ins, retrieve specific ones, or install new ones. For example, the Item method takes an index (either a numeric position or a string name) to return a specific AddIn object. Each AddIn object has properties like Name, FullName, Installed, and Path, which provide details about the add-in.

To illustrate, here is a simple xlwings code example that lists all available add-ins and their installation status:

import xlwings as xw

# Connect to the active Excel instance
app = xw.apps.active

# Access the AddIns2 collection
addins2 = app.api.AddIns2

# Print the count of add-ins
print(f"Total add-ins available: {addins2.Count}")

# Iterate through each add-in and display details
for i in range(1, addins2.Count + 1):
    addin = addins2.Item(i)
    print(f"Name: {addin.Name}, Installed: {addin.Installed}, Path: {addin.Path}")

Another example demonstrates how to install an add-in using the Add method. This method requires the full file path of the add-in file (typically with a .xlam or .xll extension) and an optional boolean parameter to specify whether to copy the file to the add-in directory. The method returns the AddIn object for the newly added add-in, which can then be manipulated further:

import xlwings as xw

app = xw.apps.active
addins2 = app.api.AddIns2

# Add a new add-in from a specified path
addin_path = r"C:\Path\To\Your\AddIn.xlam"
new_addin = addins2.Add(addin_path, True) # True copies the file to the add-in directory

# Check if it's installed and install it if not
if not new_addin.Installed:
    new_addin.Installed = True
    print(f"Add-in '{new_addin.Name}' has been installed.")
else:
    print(f"Add-in '{new_addin.Name}' is already installed.")

April 27, 2026 (0)


Leave a Reply

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