刷新 Excel 时出现属性错误:Excel.Application.CalculateUntilAsyncQueriesDone

问题描述 投票:0回答:1

我正在尝试自动化 Excel 可刷新报告,但我不断收到 属性错误:Excel.Application.CalculateUntilAsyncQueriesDone。

我的目标是让python打开excel报表,刷新两次,然后用新名称保存。 几周前它还可以工作,但 IT 部门做了一些更新,现在我收到此错误。 我正在使用 anaconda 运行它,并使用我创建的环境。

适用于 Microsoft 365 MSO 的 Microsoft® Excel®(版本 2310 内部版本 16.0.16924.20054)64 位
Python:3.11 64 位

该错误导致 Excel 文件“report_pathway”保持打开状态。 但是,即使运行 taskkill /IM Excel.exe /F 后,我仍然收到错误。

# This is my code.

import win32com.client
from datetime import datetime

report_pathway = '//WorkReportPathway'

# Start an instance of Excel
xlapp = win32com.client.DispatchEx("Excel.Application")

# Open the workbook in said instance of Excel
wb = xlapp.workbooks.open(report_pathway)

# Refresh all data connections twice to make sure pivot tables update as well.
wb.RefreshAll()
xlapp.CalculateUntilAsyncQueriesDone()
wb.RefreshAll()
xlapp.CalculateUntilAsyncQueriesDone()

# Save Report
xlapp.DisplayAlerts = False
wb.Save()
xlapp.DisplayAlerts = True

# Quit
xlapp.Quit()

with open(report_pathway+".xlsx", "rb") as f1:
  with open(report_pathway+ "_"+ timeline()+".xlsx", "wb") as f2:
    f2.write(f1.read())



# This is the error
AttributeError                            Traceback (most recent call last)
WorkReportPathway\Report.ipynb Cell 2 line 1
 13 # Refresh all data connections twice to make sure pivot tables update as well.
 14 wb.RefreshAll()
---> 15 xlapp.CalculateUntilAsyncQueriesDone()
 16 wb.RefreshAll()
 17 xlapp.CalculateUntilAsyncQueriesDone()

 File c:\Users\brycec\AppData\Local\anaconda3\envs\swnenv\Lib\site-        packages\win32com\client\dynamic.py:638, in CDispatch.__getattr__(self, attr)
635     return self._get_good_object_(ret)
637 # no where else to look.
--> 638 raise AttributeError("%s.%s" % (self._username_, attr))

AttributeError: Excel.Application.CalculateUntilAsyncQueriesDone
python excel win32com
1个回答
0
投票

我也有同样的问题: 睡眠(10)对我来说是这样的

workbook.RefreshAll()
time.sleep(10)
xl.CalculateUntilAsyncQueriesDone()

时间没测试过,也许更短也可以

© www.soinside.com 2019 - 2024. All rights reserved.