com_error: When converting docx to pdf in python

144 views Asked by At

I am currently trying to convert some word documents to PDF using python, some of the files converted successfully with the code I have while I got the following error messages on other files:

Error 1: some files reported

com_error: (-2147352567, 'Exception occurred.', (0, 'Microsoft Word', 'String is longer than 255 characters', 'wdmain11.chm', 41873, -2146819183), None)

Error 2: Some reported

com_error: (-2147352567, 'Exception occurred.', (0, 'Microsoft Word', 'Command failed', 'wdmain11.chm', 36966, -2146824090), None)

Error 3: Some reported

AttributeError: 'NoneType' object has no attribute 'SaveAs'

Below is the code I am using, any idea why this is happening:

from docx2pdf import convert


def convert_word_to_pdf(word_path):
    pdf_path = word_path.replace(".docx", ".pdf")
    convert(word_path, pdf_path)


word_path = '*file_path*'
convert_word_to_pdf(word_path)

Thank you for your support

I expected the python code above to convert all the files to PDF.

1

There are 1 answers

0
kixxalot On

I encountered Error 1 String is longer than 255 characters. In my case, I traced this to long file paths.

So a workaround is to copy the docx files to a short named temporary folder before calling convert.