{"id":2192,"date":"2026-07-18T16:34:00","date_gmt":"2026-07-18T08:34:00","guid":{"rendered":"https:\/\/xlwings.net\/blog\/?p=2192"},"modified":"2026-03-28T10:18:19","modified_gmt":"2026-03-28T10:18:19","slug":"how-to-use-applicationsmartartlayouts-in-the-xlwings-api-way","status":"publish","type":"post","link":"https:\/\/xlwings.net\/blog\/how-to-use-applicationsmartartlayouts-in-the-xlwings-api-way\/","title":{"rendered":"How to use Application.SmartArtLayouts in the xlwings API way"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">The SmartArtLayouts member of the Application object in Excel&#8217;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.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">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:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>app.smart_art_layouts<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Here, <code>app<\/code> represents an instance of the xlwings App class, which corresponds to the Excel Application object. The <code>smart_art_layouts<\/code> 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:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>layout = app.smart_art_layouts&#91;1] # Access by index\nlayout_by_id = app.smart_art_layouts('{LayoutID}') # Access by ID<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">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&#8217;s object model or by enumerating the collection. The SmartArtLayout object itself has properties like <code>Id<\/code>, <code>Name<\/code>, and <code>Category<\/code>, which provide details about the layout. For instance, <code>layout.id<\/code> returns the ID, and <code>layout.name<\/code> returns the display name. This allows for precise control when selecting layouts based on specific criteria.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">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 <code>Layout<\/code> property. Below is an example code snippet that demonstrates this process:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import xlwings as xw\n\n# Connect to the active Excel instance\napp = xw.apps.active\n\n# Access the SmartArtLayouts collection and select a layout by index\nsmartart_layouts = app.smart_art_layouts\ntarget_layout = smartart_layouts&#91;3] # Assuming index 3 corresponds to a desired layout\n\n# Open a workbook and select a worksheet\nwb = app.books.open('example.xlsx')\nws = wb.sheets&#91;'Sheet1']\n\n# Assume there is a SmartArt graphic in the worksheet; get the first shape (adjust as needed)\nsmartart_shape = ws.shapes&#91;0] # This should be a SmartArt shape\n\n# Apply the selected layout to the SmartArt graphic\nsmartart_shape.smart_art.layout = target_layout\n\n# Save and close the workbook\nwb.save()\nwb.close()<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">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:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>for layout in app.smart_art_layouts:\n    print(f\"ID: {layout.id}, Name: {layout.name}, Category: {layout.category}\")<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The SmartArtLayouts member of the Application object in Excel&apos;s object model provides access to the &#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[25],"tags":[],"class_list":["post-2192","post","type-post","status-publish","format-standard","hentry","category-xlwings-api-reference"],"_links":{"self":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2192","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/comments?post=2192"}],"version-history":[{"count":1,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2192\/revisions"}],"predecessor-version":[{"id":3352,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2192\/revisions\/3352"}],"wp:attachment":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/media?parent=2192"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/categories?post=2192"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/tags?post=2192"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}