How to use Application.SmartArtLayouts in the xlwings API way

The SmartArtLayouts member of the Application object in Excel’s object model provides access to the collection of SmartArt layouts available within the application. This collection is essential for programmatically managing and applying different visual layouts to SmartArt graphics, which are used to create professional diagrams and information graphics. Through xlwings, a powerful Python library for Excel automation, developers can interact with this collection to enumerate available layouts, retrieve specific layouts by their index or ID, and apply them to SmartArt shapes in workbooks. This functionality is particularly valuable in automating report generation, dashboard creation, and data visualization tasks where consistent and dynamic diagram styling is required.

In xlwings, the SmartArtLayouts member is accessed via the Application object. The syntax for referencing this collection is straightforward, as it does not require parameters for the property itself. However, when accessing individual layouts within the collection, methods and properties are used with specific arguments. The basic calling format is:

app.smart_art_layouts

Here, app represents an instance of the xlwings App class, which corresponds to the Excel Application object. The smart_art_layouts property returns a collection object that supports typical collection methods such as indexing. To retrieve a specific SmartArtLayout object, you can use an index (1-based) or a layout ID string. For example:

layout = app.smart_art_layouts[1] # Access by index
layout_by_id = app.smart_art_layouts('{LayoutID}') # Access by ID

The index refers to the position in the collection, which may vary based on the Excel version and installed templates. The layout ID is a unique string identifier for each layout, which can be obtained from Excel’s object model or by enumerating the collection. The SmartArtLayout object itself has properties like Id, Name, and Category, which provide details about the layout. For instance, layout.id returns the ID, and layout.name returns the display name. This allows for precise control when selecting layouts based on specific criteria.

A practical use case involves applying a SmartArt layout to an existing SmartArt graphic in a workbook. First, you need to identify the SmartArt shape, then set its layout using the Layout property. Below is an example code snippet that demonstrates this process:

import xlwings as xw

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

# Access the SmartArtLayouts collection and select a layout by index
smartart_layouts = app.smart_art_layouts
target_layout = smartart_layouts[3] # Assuming index 3 corresponds to a desired layout

# Open a workbook and select a worksheet
wb = app.books.open('example.xlsx')
ws = wb.sheets['Sheet1']

# Assume there is a SmartArt graphic in the worksheet; get the first shape (adjust as needed)
smartart_shape = ws.shapes[0] # This should be a SmartArt shape

# Apply the selected layout to the SmartArt graphic
smartart_shape.smart_art.layout = target_layout

# Save and close the workbook
wb.save()
wb.close()

In this example, we connect to Excel, retrieve the third layout from the SmartArtLayouts collection, and apply it to the first shape in a worksheet, assuming it is a SmartArt graphic. This automation can be extended to loop through multiple shapes or workbooks, applying consistent layouts based on dynamic conditions. Additionally, you can enumerate all available layouts to list their properties for reference:

for layout in app.smart_art_layouts:
    print(f"ID: {layout.id}, Name: {layout.name}, Category: {layout.category}")

July 18, 2026 (0)


Leave a Reply

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