Pywin32 save.docx as pdf

I use Word 2013 to automatically generate a report as docx and then save it in pdf format.

But when I call the SaveAs2 () function, the script pulls out the Save As windows and throws this exception:

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

Here is my code to open and save as a new file:

 self.path = os.path.abspath(path) self.wordApp = win32.Dispatch('Word.Application') #create a word application object self.wordApp.Visible = False # if false hide the word application (app does't open but still usable) self.document = self.wordApp.Documents.Open(self.path + "/" + documentRef) # opening the template file absFileName = "D:\\test.pdf" self.document.SaveAs2(FileName=absFileName,FileFormat=17) 

And I use: python2.7 with pywin32 (build 219)

Does anyone have an idea why this is not working?

+7
python word-2013 pywin32
source share
1 answer

There are some good libraries to solve this problem:

There's also an example of just that in this ActiveState recipe. Convert Microsoft Word files to PDF with DOCXtoPDF


If you insist on using the Windows API (s), there is also an example of this using win32com in this recipe Convert doc and docx files to PDF


You can also do this using comtypes (thanks .doc to pdf using Python )

Example:

 import os import sys import comtypes.client wdFormatPDF = 17 def covx_to_pdf(infile, outfile): """Convert a Word .docx to PDF""" word = comtypes.client.CreateObject('Word.Application') doc = word.Documents.Open(infile) doc.SaveAs(outfile, FileFormat=wdFormatPDF) doc.Close() word.Quit() 
+3
source share

All Articles