Pywin32 save .docx as pdf
Asked Answered
G

3

9

I'm using Word 2013 to automatically create a report as a docx and then save it as a pdf format.

But when I call the function SaveAs2(), the script pop 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 to 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'm using : python2.7 with pywin32 (build 219)

Does someone had an idea why it doesn't work?

Geomorphic answered 27/5, 2015 at 11:27 Comment(1)
Why not just create the report using reportlab? Then it's all in Python and you don't have to worry about these conversion issues.Incurable
D
5

There are a couple of nice libraries to handle this task:

There is also an example of doing exactly this in this ActiveState Recipe Convert Microsoft Word files to PDF with DOCXtoPDF


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


You could also do this using comtypes (Thanks to .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()
Doubleminded answered 27/5, 2015 at 12:16 Comment(2)
Hi James and thanks for your answer and your suggestion! I have tried your examples with comtypes and ActiveState but unfortunately, it creates the same trouble as above during the save part. As for python-docx, it doesn’t allow to save it as an pdf [document]( github.com/python-openxml/python-docx/issues/113) and all the other library doesn’t seem to take the docx header.Geomorphic
James, the python-docx library is great (already using it) but cannot be used to generate the PDF. You need a "renderer" to be able to do that.Generous
Y
1

Looks like the "Office 2013" is the bottleneck.

I have the same issue while using Word 2013 ("Office 2013"),
but when I try to run your code snippet with "Office 365" and "Office 2010", it works.

I can recommend two solutions for now:

  • try out different MS Office versions (365 and 2010 tested)
  • use some online API-s to convert it to PDF

Note:
Changing the module/library won't fix the issue,
only the right Office version will.

Yoruba answered 1/3, 2021 at 8:47 Comment(0)
N
1

Use this, don't forget to install win32 like this:

pip install pywin32

the function to convert doc to pdf is like this:

import win32com.client as win32  
def convert_to_pdf(doc):
    """Convert given word document to pdf"""
    word = win32.DispatchEx("Word.Application")
    new_name = doc.replace(".docx", r".pdf")
    worddoc = word.Documents.Open(doc)
    worddoc.SaveAs(new_name, FileFormat=17)
    worddoc.Close()
    return None

path_to_word_document = os.path.join(os.getcwd(), 'report_1.docx')
convert_to_pdf(path_to_word_document)

give me my starts, i really need it :-) for more look for the documentation in the library https://pypi.org/project/pywin32/

Nettles answered 9/12, 2021 at 22:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.