How to use Application.TemplatesPath in the xlwings API way

In Excel, the Application object serves as the top-level object representing the entire Excel application. Among its many members, the TemplatesPath property is a read‑only property that returns the full path to the folder where Excel stores its template files. This is useful when you need to programmatically locate the default template directory, for example to save a custom template or to list available templates. In xlwings, you can access this property through the Application object, which is exposed via the app object when you have an active connection to Excel.

The xlwings API syntax for accessing the TemplatesPath property is straightforward. Since it is a property, you simply reference it without parentheses. The general format is:

app.api.TemplatesPath

Here, app is an instance of the xlwings App class, which corresponds to the Excel Application object. The .api attribute provides direct access to the underlying Excel object model, allowing you to call native Excel properties and methods. The TemplatesPath property returns a string representing the full directory path. No parameters are required because it is a read‑only property.

For example, if you want to retrieve the default templates path and print it, you would use the following code:

import xlwings as xw

# Connect to the active Excel instance or start a new one
app = xw.apps.active

# Get the TemplatesPath
templates_folder = app.api.TemplatesPath
print(f"The default templates path is: {templates_folder}")

This code snippet first imports xlwings and then connects to the currently active Excel application. By accessing app.api.TemplatesPath, it retrieves the path and prints it. The output might look like C:\Users\[Username]\AppData\Roaming\Microsoft\Templates\ on Windows or a corresponding path on macOS.

Another practical use case is to combine the TemplatesPath with other operations, such as saving a workbook as a template in the default location. For instance:

import xlwings as xw
import os

app = xw.apps.active
wb = app.books.active

# Get the templates path and define a new template file name
templates_path = app.api.TemplatesPath
new_template_name = "MyCustomTemplate.xltx"
full_path = os.path.join(templates_path, new_template_name)

# Save the active workbook as a template in the default folder
wb.save(full_path)
print(f"Template saved to: {full_path}")

July 22, 2026 (0)


Leave a Reply

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