How to use Application.StandardFontSize in the xlwings API way

The StandardFontSize member of the Application object in Excel allows you to get or set the default font size, measured in points, that is used for new workbooks and standard text styles. This is a global setting within the Excel application instance, meaning it affects the entire environment, not just a specific workbook. By adjusting this property, you can standardize the default appearance of text across newly created documents without manually formatting each cell. This is particularly useful for maintaining corporate branding or ensuring consistency in report generation.

In xlwings, you access this property through the api property of the App object, which provides direct access to the underlying Excel object model. The property is available for both reading and writing.

Syntax:

# To get the current standard font size
current_size = app.api.StandardFontSize

# To set a new standard font size
app.api.StandardFontSize = new_size
  • app: An instance of the xlwings App class representing the Excel application.
  • current_size: The returned value is a floating-point number representing the font size in points (e.g., 11.0).
  • new_size: A numeric value (integer or float) specifying the desired default font size in points. It must be a positive number, typically within the range supported by Excel (e.g., 1 to 409 points). Setting this property immediately changes the application’s default, but note that it does not retroactively alter existing workbooks; it only applies to new workbooks created thereafter.

Example Usage:
Here are practical xlwings code snippets demonstrating how to work with the StandardFontSize property.

  1. Retrieving the Current Standard Font Size:
import xlwings as xw

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

# Get the current standard font size
default_size = app.api.StandardFontSize
print(f"The current standard font size is {default_size} points.")
  1. Setting a New Standard Font Size:
import xlwings as xw

# Start a new Excel application
app = xw.App()

# Set the standard font size to 14 points
app.api.StandardFontSize = 14

# Create a new workbook to see the effect
wb = app.books.add()
ws = wb.sheets[0]
# The default font in cell A1 should now be 14 points
print(f"Standard font size set to {app.api.StandardFontSize} points.")

# Save and close
wb.save('new_workbook.xlsx')
wb.close()
app.quit()
  1. Modifying the Setting and Creating Multiple Workbooks:
import xlwings as xw

app = xw.App(visible=False) # Run in background

# Store the original setting for later restoration
original_size = app.api.StandardFontSize
print(f"Original standard font size: {original_size} points")

# Change to a larger font for emphasis
app.api.StandardFontSize = 16

# Generate two new workbooks with the updated default
for i in range(2):
    wb = app.books.add()
    ws = wb.sheets[0]
    ws.range('A1').value = f"This text uses the new standard size of {app.api.StandardFontSize} points."
    wb.save(f'report_{i+1}.xlsx')
    wb.close()

# Restore the original setting
app.api.StandardFontSize = original_size
print(f"Restored to original size: {app.api.StandardFontSize} points")

app.quit()

July 21, 2026 (0)


Leave a Reply

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