我正在使用以下代码刷新excel文件中的数据,该文件使用外部加载项来接收数据。
import sys,os,pandas as pd,numpy as np,time,win32com.client
import win32com.client as w3c
if __name__ == '__main__':
your_file_path = r'C:\Book11.xlsx'
for ii in np.arange(1,10):
xlapp = w3c.gencache.EnsureDispatch('Excel.Application')
xlapp.Visible = 0
xlwb = xlapp.Workbooks.Open(your_file_path,False,True,None)
books = w3c.Dispatch(xlwb)
xlwb.RefreshAll() # Runs with no errors,but doesn't refresh
xlapp.DisplayAlerts = False
xlwb.Save()
xlapp.Quit()
df = pd.read_excel(your_file_path) # updates should be applied
print(df)
time.sleep(20)
# Another version of code that I tried is following:
# xlapp = win32com.client.DispatchEx("Excel.Application")
# xlapp.Visible = True
# wb = xlapp.Workbooks.Open(your_file_path)
# wb.RefreshAll()
# xlapp.CalculateUntilAsyncQueriesDone()
# xlapp.DisplayAlerts = False
# wb.Save()
# xlapp.Quit()
但是,文件不会刷新。实际上,它看起来如下所示:
另一方面,如果我只是通过单击鼠标在桌面上打开文件,则会看到预期的数据。