Refresh Excel External Data with Python

前端 未结 5 1087
北荒
北荒 2020-11-29 08:39

I have an Excel file that I run a Python script on. The Excel file has external data connections that need to be refreshed before the Python script is run. The functionality

5条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-29 09:13

    Adding on top of what everyone else has said, I kept getting the save dialog again when the code got to the Quit line. I set the DisplayAlerts flag to false and it fixed my issue. I didn't need the sleep timer either. This is what worked for me:

    xlapp = win32com.client.DispatchEx("Excel.Application")
    wb = xlapp.Workbooks.Open()
    wb.RefreshAll()
    xlapp.CalculateUntilAsyncQueriesDone()
    xlapp.DisplayAlerts = False
    wb.Save()
    xlapp.Quit()
    

提交回复
热议问题