Power Query Python (Openpyxl) Refresh on Open

Copper Contributor

I have an excel file that uses a power query connection. I have written a script in python using openpyxl to access that file, find any new data that has been added, and place that data into a different file.

My question is this: If I set the query properties to 'Refresh data when opening the file', will using openpyxl to access and 'load' the workbook trigger this refresh?

I am not sure how Excel defines the file being opened, because the script does not actually 'open' the file on my computer (like, I can't see it open in my task bar), but I need the data to refresh every time the script runs.

Then, if openpyxl access does not trigger the refresh, how might I achieve something like that? Would Windows Task Scheduler opening the file manage to do that? Thanks for any help, and happy to provide some snippets of code if needed, although I think this is more of a question about Excel's properties than it is about the actual code.

1 Reply


Accessing an Excel workbook with openpyxl does not trigger the 'Refresh data when opening the file' property because openpyxl does not open the file in the traditional sense. It reads and writes the file without invoking Excel's built-in functions or triggering events like refreshing data connections.

To ensure that your Power Query connection refreshes each time the script runs, you need a method that actually opens the file in Excel and triggers the refresh. Here are a few approaches to achieve this:

Method 1: Use Excel's COM Interface with Python

You can use the win32com.client library to open the Excel application, refresh the workbook, and then save and close it. This approach will trigger the 'Refresh data when opening the file' property.


Python Code is untested bachup your file and data.

import win32com.client

def refresh_excel_workbook(file_path):
    # Open Excel application
    excel_app = win32com.client.Dispatch("Excel.Application")
    excel_app.Visible = False  # Keep Excel application invisible
    # Open the workbook
    workbook = excel_app.Workbooks.Open(file_path)
    # Refresh all data connections
    # Wait until refresh is complete
    # Save and close the workbook
    # Quit Excel application

# Path to your Excel workbook
file_path = "path_to_your_workbook.xlsx"

Method 2: Use Windows Task Scheduler

You can set up a task in Windows Task Scheduler to open the workbook at specified intervals. This will open Excel, which will then refresh the data according to the 'Refresh data when opening the file' setting.

1. Create a VBA Macro to Refresh and Save the Workbook:

    • Open Excel and press Alt + F11 to open the VBA editor.
    • Insert a new module and add the following code:

Vba Code

Sub AutoRefresh()
    Application.Wait (Now + TimeValue("00:00:30")) ' Wait for 30 seconds for the refresh to complete
End Sub
    • Save the workbook as a macro-enabled file (.xlsm).

2. Create a Task in Windows Task Scheduler:

    • Open Task Scheduler and create a new task.
    • Set the trigger to your desired schedule.
    • In the actions tab, set the action to "Start a program".
    • In the program/script box, enter the path to Excel (e.g., C:\Program Files\Microsoft Office\root\Office16\EXCEL.EXE).
    • In the "Add arguments" box, enter the path to your workbook (e.g., "C:\path_to_your_workbook.xlsm").
    • Save the task.

Method 3: Use Power Automate

If you have access to Power Automate (formerly Microsoft Flow), you can create a flow that opens the Excel file and refreshes it. Power Automate can handle the automation in a cloud environment, ensuring your data is always up to date.

  1. Create a Flow:
    • Go to Power Automate and create a new flow.
    • Set a trigger, such as a schedule or an event in another application.
    • Add an action to "Run a script" in Excel for the web, where you can specify a script that refreshes the data connections.

By using one of these methods, you can ensure that your Power Query data is refreshed every time your script runs, or on a regular schedule, without manually opening the Excel file.


NOTE: My knowledge about Power Query Python is very limited, but since no one has answered it for at least one day or more, I entered your question in AI too. Maybe it will help you further in your project, if not please just ignore it.



My answers are voluntary and without guarantee!


Hope this will help you.

Was the answer useful? Mark as best response and Like it!

This will help all forum participants.