尝试使用Python将.doc转换为.pdf时出错



我一直在尝试使用Python将旧的.doc文件转换为PDF,但遇到了一些麻烦。这是我的代码:

import os 
import comtypes.client 
format_code = 17
file_input = os.path.abspath('(1)2014-06-18.doc')
file_output = os.path.abspath('test.pdf')
word_app = comtypes.client.CreateObject('Word.Application')
word_file = word_app.Documents.Open(file_input)
word_file.SaveAs(file_output,FileFormat=format_code)
word_file.Close()
word_app.Quit()

每当我执行它时,我得到:

Users/gawel/OneDrive/Desktop/scraping/doctotxt.py
Traceback (most recent call last):
File "c:/Users/gawel/OneDrive/Desktop/scraping/doctotxt.py", line 10, in <module>
word_file.SaveAs(file_output,FileFormat=format_code)
_ctypes.COMError: (-2147418111, 'Call was rejected by callee.', (None, None, None, 0, None))

我一直试图对这个错误的含义进行一些研究,但一直没有成功。如有任何帮助,我们将不胜感激。谢谢

示例:

# Open Microsoft DOC 
app = client.Dispatch("Word.Application")

# Read Doc File
doc = app.Documents.Open('C:/Users/<User>/Downloads/document.docx')

# Convert into PDF File
doc.ExportAsFixedFormat('C:/Users/<User>/Downloads/document.pdf', 17, Item=7, CreateBookmarks=0)
app.Quit()

如果仍然不起作用,请尝试删除"中的缓存文件;gen";路径中的文件夹:

C:\Users\\AppData\Local\Programs\Python39\Lib\site packages\comtypes\gen

请尝试使用安装在环境中的Microsoft Office或LibreOffice来使用msoffice2.pdf库。

https://pypi.org/project/msoffice2pdf/

最新更新