ImportError:无法在python中导入name comeror

2024-09-25 02:35:56 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试用以下代码将docx文件转换为pdf

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()

它抛出了一个错误

ImportError: cannot import name COMError

我已经安装了comtypes包。

我对python很陌生,我不知道如何解决这个问题。

[编辑]

堆栈跟踪

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

Tags: pathinimportclientdocossysout