Home  >  Q&A  >  body text

windows - Python调用win32com.client.Dispatch('Word.Application')报错

我现在使用Python进行office文件操作,但是在调用win32com.client.Dispatch模块时报错,代码如下:

import win32com
from win32com.client import Dispatch,constants
w = win32com.client.Dispatch('Word.Application')

错误信息如下所示:
Traceback (most recent call last):
File "<interactive input>", line 1, in <module>
File "C:Python27libsite-packageswin32comclient__init__.py", line 95, in Dispatch

dispatch, userName = dynamic._GetGoodDispatchAndUserName(dispatch,userName,clsctx)

File "C:Python27libsite-packageswin32comclientdynamic.py", line 114, in _GetGoodDispatchAndUserName

return (_GetGoodDispatch(IDispatch, clsctx), userName)

File "C:Python27libsite-packageswin32comclientdynamic.py", line 91, in _GetGoodDispatch

IDispatch = pythoncom.CoCreateInstance(IDispatch, None, clsctx, pythoncom.IID_IDispatch)

com_error: (-2147221164, 'xc3xbbxd3xd0xd7xa2xb2xe1xc0xe0', None, None)

我有点看不太懂,也没查到太多有用信息,各位有没有遇到类似问题的,请赐教。

高洛峰高洛峰2740 days ago1457

reply all(1)I'll reply

  • ringa_lee

    ringa_lee2017-04-18 09:07:10

    The cause of the problem was not found, but it was solved using other methods.
    The environment where this problem occurs is Python 2.7 32-bit, pywin32 32-bit, win7 64-bit, and office 2007.
    I changed to another environment on another machine, Python 2.7 64-bit, pywin32-220.win-amd64-py2.7 64-bit, win7 64-bit, office 2007 (newly installed).
    The second environment test passed.
    Cause analysis: 1. The first environment is on an office computer with a lot of software installed, including anti-virus software and DLP encryption software. I don’t know if it will have any impact on the office program, which may lead to errors in calling win32com.
    2. The system environment must be consistent with the software configuration, that is, both 64-bit or 32-bit. Because there may be problems with 32-bit Python modules calling 64-bit office programs.
    The above are my thoughts, everyone is welcome to add.

    reply
    0
  • Cancelreply