How to use Application.DisplayRecentFiles in the xlwings API way

The DisplayRecentFiles property of the Application object in Excel’s object model controls whether the list of recently used files is shown in the Excel application’s backstage view (accessible via the File menu). In xlwings, a Python library for interacting with Excel, you can access and manipulate this property to customize the user interface experience programmatically. This can be useful in automation scripts where you want to streamline the Excel environment by hiding or showing recent files based on specific workflows or user preferences.

Functionality:
The DisplayRecentFiles property is a Boolean value that determines the visibility of the recent files list. When set to True, Excel displays the list of recently opened documents; when set to False, the list is hidden. This property affects the Excel application globally, meaning it applies to all workbooks open in that instance of Excel. It is part of the application-level settings and can be used to enhance security or reduce clutter in user interfaces, especially in controlled corporate environments or automated reporting tools.

Syntax in xlwings:
In xlwings, you access the Application object through the app property of a workbook or by creating an instance of the Excel application. The DisplayRecentFiles property is exposed as an attribute that can be read or written. The syntax is straightforward:

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 value of DisplayRecentFiles
current_setting = app.api.DisplayRecentFiles

# Set DisplayRecentFiles to a new value
app.api.DisplayRecentFiles = False # Hides the recent files list

Note: The api property in xlwings provides direct access to the underlying Excel object model, allowing you to use properties and methods as defined in Excel’s VBA documentation. For DisplayRecentFiles, no parameters are required—it is a simple read/write property.

Code Examples:
Here are practical xlwings API code snippets demonstrating the use of DisplayRecentFiles:

  1. Check and Report the Current Setting:
    This example retrieves the current state of DisplayRecentFiles and prints a message based on its value.
import xlwings as xw

# Connect to Excel
app = xw.apps.active

# Get the property value
is_displayed = app.api.DisplayRecentFiles
if is_displayed:
    print("The recent files list is currently visible in Excel.")
else:
    print("The recent files list is hidden.")
  1. Toggle the Visibility of Recent Files:
    This script toggles the DisplayRecentFiles setting, switching it from its current state to the opposite. It can be used in a macro or automated task to dynamically adjust the UI.
import xlwings as xw

app = xw.apps.active
# Toggle the property
app.api.DisplayRecentFiles = not app.api.DisplayRecentFiles
print(f"DisplayRecentFiles toggled to: {app.api.DisplayRecentFiles}")
  1. Hide Recent Files for a Clean Interface:
    In an automation scenario where you want to present a distraction-free Excel environment—for instance, when generating reports—you can set DisplayRecentFiles to False at the start and restore it later.
import xlwings as xw

app = xw.apps.active
# Save the original setting
original_setting = app.api.DisplayRecentFiles

# Hide the recent files list
app.api.DisplayRecentFiles = False
print("Recent files list hidden. Perform your tasks here...")

# Restore the original setting (optional)
app.api.DisplayRecentFiles = original_setting
print("Original setting restored.")

May 29, 2026 (0)


Leave a Reply

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