【发布时间】:2020-03-11 19:07:42
【问题描述】:
我正在使用以下代码刷新 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()
但是,文件不会刷新。实际上它看起来像下面这样:
另一方面,如果我只是使用鼠标单击在桌面上打开文件,我会看到预期的数据。
【问题讨论】:
-
RefreshAll 根据文档没有括号。你试过没有他们吗? docs.microsoft.com/de-de/office/vba/api/…
-
是的,刚刚试过,但文件仍然没有刷新。
-
1) 您是否将其作为宏运行?
-
不是代码是python脚本而不是excel宏。
-
你能检查下面的答案吗?
标签: win32com python-3.8