3

This code in Python is not working:

from comtypes import client
word = client.CreateObject("Word.Application")
word.Documents.Open("C:\\test.docx")

I get this error message:

Traceback (most recent call last):
File "<pyshell#5>", line 1, in <module>
doc = word.Documents.Open("test.docx")
AttributeError: 'POINTER(IUnknown)' object has no attribute 'Documents'

Similar code using the class Excel.Appliation also gives the same kind of error. Another test with InternetExplorer.Application worked. So it seems a problem of Office.

I also tested the same code in VBScript and worked.

The code works in Windows7-64bits and Office 2013. The computer where it is not working is Windows10-64bits and Office 365. The Python version in both computers is 3.6-64bits.

2
  • It could be that the object is not in a running state and that is why the call to the Documents property fails. if you call the WinAPI function OleRun(word), it will guarantee the object is in the running state. OTOH, there might be a Python function that does the same thing for all I know. Commented Mar 15, 2017 at 23:12
  • Thank you @JoeWillcoxson, but I'm not an expert in Python nor in WinAPI Commented Mar 16, 2017 at 1:14

1 Answer 1

3

This is not a good solution but I think I will be able to live with it. I'm using this code now:

from comtypes import client
word = client.CreateObject("Word.Application", dynamic = True)
word.Visible = True
word.Documents.Open("test.docx")
word.Documents[0].SaveAs("test.pdf", 17)
word.Documents[0].Close()

Important is the word.Visible = True row and the dynamic = True parameter. Without them the Documents collection is not found.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.