How to use Application.StandardFont in the xlwings API way

The Application.StandardFont property in Excel refers to the default font name used for new workbooks and worksheets. Through xlwings, this property can be accessed and modified to programmatically control the standard font setting across Excel sessions. This is particularly useful for ensuring consistency in document formatting or adapting the default appearance to corporate style guidelines without manual intervention.

Functionality
This property allows you to retrieve or set the name of the standard font as a string. When you change it, any new workbook created thereafter will use this font as the default for cell text. Note that existing workbooks are not automatically updated; the change applies prospectively. It affects the Excel application instance, making it a global setting.

Syntax in xlwings
In xlwings, you interact with this property via the app object, which represents the Excel Application. The syntax is straightforward:

  • To get the current standard font: app.api.StandardFont
  • To set a new standard font: app.api.StandardFont = "font_name"

Here, app is your xlwings App instance. The .api attribute provides direct access to the underlying Excel object model (through pywin32 on Windows or appscript on macOS). The StandardFont property expects a string value representing a valid font name installed on the system, such as “Calibri”, “Arial”, or “Times New Roman”. There are no additional parameters.

Code Examples
Below are practical examples demonstrating how to use the StandardFont property with xlwings.

Example 1: Retrieving the Current Standard Font

import xlwings as xw

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

# Get the current standard font name
current_font = app.api.StandardFont
print(f"The current standard font is: {current_font}")

Example 2: Setting a New Standard Font

import xlwings as xw

# Ensure Excel is running; start if necessary
app = xw.App(visible=True)

# Set the standard font to Arial
app.api.StandardFont = "Arial"
print("Standard font changed to Arial.")

# Create a new workbook to see the effect
wb = app.books.add()
ws = wb.sheets[0]
ws.range("A1").value = "This text should be in Arial."

# Save and close
wb.save("new_workbook.xlsx")
wb.close()
app.quit()

Example 3: Verifying the Change Across Sessions

import xlwings as xw

# First, set the standard font
app = xw.App(visible=False)
app.api.StandardFont = "Courier New"
app.quit()

# Restart Excel and check
app_new = xw.App(visible=False)
font_check = app_new.api.StandardFont
print(f"After restart, standard font is: {font_check}") # Should be "Courier New"
app_new.quit()

July 20, 2026 (0)


Leave a Reply

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