ImportError: cannot import name COMError in python
Asked Answered
H

1

10

I am trying to convert docx file to pdf with following code

import sys
import os
import comtypes.client


wdFormatPDF = 17

in_file = os.path.abspath(sys.argv[1])
out_file = os.path.abspath(sys.argv[2])

word = comtypes.client.CreateObject('Word.Application')
doc = word.Documents.Open(in_file)
doc.SaveAs(out_file, FileFormat=wdFormatPDF)
doc.Close()
word.Quit()

It is throwing an error

ImportError: cannot import name COMError

I have installed comtypes package.

I am very new to python, I can not figure out how to resolve this problem.

[Edit]

Stacktrace

Traceback (most recent call last):
  File "converttopdf.py", line 3, in <module>
    import comtypes.client
  File "/usr/local/lib/python2.7/dist-packages/comtypes-1.1.2-py2.7.egg/comtypes/__init__.py", line 23, in <module>
    from _ctypes import COMError
ImportError: cannot import name COMError
Heber answered 11/5, 2016 at 11:40 Comment(8)
instead of import comtypes.client try import comtypes. comtypes.client only imports the client package. The COMError might be elsewhereBarbital
@Barbital still getting same errorHeber
how did you install comtypes?Barbital
Downloaded comtypes-1.1.2 and then python setup.py installHeber
can you include the stack trace?Barbital
included in questionHeber
What's your OS? COMtypes is for Windows only.Saddlebag
@sarvesh I'm afraid COMtypes wont work then. I wrote an answer explaining this. Apologies that it doesn't explain how you could do this, but I'm not personally familiar with any tools that would help.Saddlebag
S
19

Unfortunately COMTypes is designed for Windows, not Linux.

comtypes allows to define, call, and implement custom and dispatch-based COM interfaces in pure Python. It works on Windows, 64-bit Windows, and Windows CE.

Source

You'll need to find another way to do your conversion, likely through another library.

Saddlebag answered 11/5, 2016 at 13:12 Comment(1)
And what is the other way??Sphacelus

© 2022 - 2024 — McMap. All rights reserved.