【发布时间】:2018-04-25 11:54:33
【问题描述】:
我使用的是 Python 2.7.14。
我目前正在开发一个程序,该程序可以调整 Word 文档中的某些信息。这个目标已经实现,但是我在将 .docx 文档转换为 .pdf 时遇到了麻烦。
我想使用此链接中的代码:.doc to pdf using python
但是在导入 comtypes.client 时出现错误
Traceback (most recent call last):
File "C:\Python27\comtypes.py", line 2, in <module>
from comtypes.client import CreateObject, GetActiveObject
File "C:\Python27\comtypes.py", line 2, in <module>
from comtypes.client import CreateObject, GetActiveObject
ImportError: No module named client
看起来我使用pip install comtypes 安装了comtypes。
这是我想用来将我的 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()
【问题讨论】:
-
您能否再次使用
pip install comtypes确认您已安装它? -
comtypes.py 里面是什么?它看起来根本不像你需要的。