mrgou
mrgou

Reputation: 2510

ValueError when opening Word file with comtypes in Python

I'm trying to port some working VBS code into Python, to analyze a collection of Word files. I was hoping that the comtypes would allow me to reuse most of my code, but I get an error when a Word instance opens a file:

ValueError: NULL COM pointer access

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

I would expect to get a document object that I can then read:

nbpages = objDoc.Range.Information(4)

Upvotes: 0

Views: 704

Answers (1)

mrgou
mrgou

Reputation: 2510

Seems like I needed to provide the full, absolute path to the file. Maybe the Python working folder isn't passed on to the COM object.

Upvotes: 1

Related Questions