I've been trying to convert old .doc files to PDFs using Python, but I have been running into some trouble. Here is my code:
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()
Whenever I execute it, I get:
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))
I have been trying to do some research on what this error could be mean, but I have been unsuccessful. Any help would be appreciated. Thanks!
Example:
# 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()
If it still doesn't work, try deleting the cache files inside the "gen" folder in the path:
C:\Users\\AppData\Local\Programs\Python\Python39\Lib\site-packages\comtypes\gen
Try using the msoffice2pdf library using Microsoft Office or LibreOffice installed in the environment.