I have an Excel sheet which has a data connection with PowerQuery. Usually, I used to click "Refresh All" to update the data. I want to automate this and wrote a Python script to do that.
However, in Excel I found under Connection Properties an option: "Refresh while opening". Now when I open the sheet, the Refresh automatically triggers and data is updated.
From Python code, I open this sheet and have put a sleep command so that it waits for 30 seconds till the sheet automatically refreshes. After that I am calling "Save As" to save the updated sheet with a different name.
I am using below code:
import win32com.client
import time
# Start an instance of Excel
xlapp = win32com.client.DispatchEx("Excel.Application")
# Open the workbook in said instance of Excel
wb = xlapp.workbooks.open("test.xlsx")
# Optional, e.g. if you want to debug
# xlapp.Visible = True
time.sleep(30)
wb.SaveAs("New.xlsx")
# Quit
xlapp.Quit()
I noticed, that even after "Save As", the data values are not updated. If I directly open Excel, without Python script, the Refresh works and then I manually click Save. It updates. But with above code, it is not saving the updated version.
Please advise.