Search code examples
pythonexcelautomationpywin32

Refreshing Excel with pywin32 is broken by pop up


I have a Python script, which is refreshing Excel files every 15 minutes. Those files are connected to Analysis Services, so simple "Refresh All" can do the refresh. In order to automate that on Python, I am using pywin32 library.

import win32com.client
xlapp = win32com.client.gencache.EnsureDispatch("Excel.Application")
wb_7 = xlapp.Workbooks.Open(excel_str_path)
wb_7.RefreshAll()
xlapp.CalculateUntilAsyncQueriesDone()
wb_7.Save()
wb_7.Close(True)

However, once in a while I face the problem with analysis service pop-up. Something like this: enter image description here

So when this message pop-ups - the Python script is waiting till "Refresh All" is completed which never happens. As the result - the script is dead and not performing any actions until this pop up closed manually.

What is the best way to handle this?


Solution

  • So far I didn't find any solution to fix this using Python. So I used Power Automate script which is checking the screen every 30 seconds and closes this window if it finds it.