【发布时间】:2018-06-04 16:12:03
【问题描述】:
我有一个 python 脚本,它调用打开一个 Excel 文件,在文件中调用一个宏,然后关闭。如果我从 CLI 运行该文件,它就可以工作。如果我把它放在任务计划程序中,我会从 win32com 收到一个错误。
打开Excel文件的方法:
import win32com.client as WinCom
if os.path.exists(reportGeneratorFileName):
try:
xl = WinCom.Dispatch("Excel.Application")
xl.Workbooks.Open(Filename=os.path.abspath(reportGeneratorFileName))
xl.Application.Visible = False
xl.Application.Run("'{}'!Runner.Runner".format(reportGeneratorFileName))
l.info('Start Sleeping')
# Async mode of pythonw causes this to finish before the file is made
time.sleep(300)
l.info('Done Sleeping')
xl.Application.Quit()
except Exception as e:
l.error('Error updating file')
l.error(e, exc_info=True)
这是我得到的错误:
06/04/2018 06:56:19 AM ERROR: (-2146959355, 'Server execution failed', None, None)
Traceback (most recent call last):
File "LAW Report.py", line 846, in createReport
xl = WinCom.Dispatch("Excel.Application")
File "c:\python27\lib\site-packages\win32com\client\__init__.py", line 95, in Dispatch
dispatch, userName = dynamic._GetGoodDispatchAndUserName(dispatch,userName,clsctx)
File "c:\python27\lib\site-packages\win32com\client\dynamic.py", line 114, in _GetGoodDispatchAndUserName
return (_GetGoodDispatch(IDispatch, clsctx), userName)
File "c:\python27\lib\site-packages\win32com\client\dynamic.py", line 91, in _GetGoodDispatch
IDispatch = pythoncom.CoCreateInstance(IDispatch, None, clsctx, pythoncom.IID_IDispatch)
com_error: (-2146959355, 'Server execution failed', None, None)
现在我正在运行 64 位 python 2.7 和 64 位 win32com,而 office 是 32 位,但正如我上面所说,如果我只是从 CLI 运行脚本,它运行良好,而不是从任务管理器运行。我在 Windows Server 2012R2 上运行它。我尝试为 2008、2008r2 和 2012r2 配置任务。我也尝试过最高权限。无论用户是否登录,我都需要它才能运行。每次我测试,用户都已经登录了。
【问题讨论】:
标签: python-2.7 scheduled-tasks win32com