Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ImportError: cannot import name COMError in python

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
like image 800
hard coder Avatar asked May 11 '16 11:05

hard coder


People also ask

How do you fix ImportError Cannot import name?

If the error occurs due to a circular dependency, it can be resolved by moving the imported classes to a third file and importing them from this file. If the error occurs due to a misspelled name, the name of the class in the Python file should be verified and corrected.

How do I fix the ImportError in Python?

Python's ImportError ( ModuleNotFoundError ) indicates that you tried to import a module that Python doesn't find. It can usually be eliminated by adding a file named __init__.py to the directory and then adding this directory to $PYTHONPATH .

Why I cant import in Python?

In Python "ImportError: cannot import name" error generally occurs when the imported class is not accessible, or the imported class is in a circular dependency.

What is Import error in Python?

ImportError is raised when a module, or member of a module, cannot be imported. There are a two conditions where an ImportError might be raised. If a module does not exist.


1 Answers

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.

like image 100
SuperBiasedMan Avatar answered Sep 26 '22 15:09

SuperBiasedMan