
It can be used as both a CLI and a python library. You can use the docx2pdf python package to bulk convert docx to pdf. # key point 2: wait for the COM Server to prepare well.ĭoc=(in_file) # open docx file 1ĭoc.SaveAs(out_file, FileFormat=wdFormatPDF) # conversionĭoc = (in_file2) # open docx file 2ĭoc.SaveAs(out_file2, FileFormat=wdFormatPDF) # conversion # key point 1: make word visible before open a new document Out_file2=r'absolute path of outputpdf file 2' In_file2=r'absolute path of input docx file 2' Out_file=r'absolute path of output pdf file 1' In_file=r'absolute path of input docx file 1' # be careful about the slash '\', use '\\' or '/' or raw string r"." If you have encountered the same problems, try to follow these two steps. So I add a delay before I tried to open a document.Īfter doing these two steps, the program will work perfectly with no failure anymore. The crash error "COMError: (-2147418111, 'Call was rejected by callee.', (None, None, None, 0, None))" means that the COM Server may not be able to response so quickly.

After doing (1), the program will work well sometimes but may fail often. If I do not do this on my computer, the program will crash when I try to open a document in the invisible model, then the 'Word.Application' object will be deleted by OS. (Actually, even I myself cannot explain why this works. The first time when I created the 'Word.Application' object, I should make it (the word app) visible before open any documents.

Steven's answer is right, but it will fail on my computer.

I have worked on this problem for half a day, so I think I should share some of my experience on this matter. You could also use pywin32, which would be the same except for: import win32com.clientĪnd then: word = ('Word.Application')

Word = ('Word.Application')ĭoc.SaveAs(out_file, FileFormat=wdFormatPDF) A simple example using comtypes, converting a single file, input and output filenames given as commandline arguments: import sys
