[英]Error when trying to convert .doc to .pdf with Python
我一直在嘗試使用 Python 將 old.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\Python\Python39\Lib\site-packages\comtypes\gen
嘗試使用環境中安裝的 Microsoft Office 或 LibreOffice 使用 msoffice2pdf 庫。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.