【问题标题】:ValueError when opening Word file with comtypes in Python在 Python 中打开带有 comtypes 的 Word 文件时出现 ValueError
【发布时间】:2019-09-26 04:08:22
【问题描述】:

我正在尝试将一些有效的 VBS 代码移植到 Python 中,以分析 Word 文件的集合。我希望 comtypes 可以让我重用我的大部分代码,但是当 Word 实例打开文件时出现错误:

ValueError: NULL COM 指针访问

In [2]: from comtypes.client import CreateObject

In [3]: objWord = CreateObject("Word.Application")

In [4]: objWord.Visible = False

In [5]: objDoc = objWord.Documents.Open('my_file.docx')
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-5-c1e34bdd2b13> in <module>
----> 1 objDoc = objWord.Documents.Open('my_file.docx')

c:\program files\python37\lib\site-packages\comtypes\_meta.py in _wrap_coclass(self)
     11     itf = self._com_interfaces_[0]
     12     punk = cast(self, POINTER(itf))
---> 13     result = punk.QueryInterface(itf)
     14     result.__dict__["__clsid"] = str(self._reg_clsid_)
     15     return result

c:\program files\python37\lib\site-packages\comtypes\__init__.py in QueryInterface(self, interface, iid)
   1156         if iid is None:
   1157             iid = interface._iid_
-> 1158         self.__com_QueryInterface(byref(iid), byref(p))
   1159         clsid = self.__dict__.get('__clsid')
   1160         if clsid is not None:

ValueError: NULL COM pointer access

我希望得到一个可以阅读的文档对象:

nbpages = objDoc.Range.Information(4)

【问题讨论】:

    标签: python ms-word com comtypes


    【解决方案1】:

    似乎我需要提供文件的完整绝对路径。也许 Python 工作文件夹没有传递给 COM 对象。

    【讨论】:

      猜你喜欢
      • 2020-07-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多