Skip to content
Advertisement

Save doc file as pdf file using python

I want to save a doc file as pdf using python, I tried so many solution but I couldn’t find the right one.

This is my code, I tried to make the output file as a pdf file but it didn’t open. Any help is highly appreciated :

def replace_string(filenameInput, filenameOutput):  

    doc = Document(filenameInput)

    for p in doc.paragraphs:
        for d in J['data']:
            if p.text.find(d['variable']) >= 0:
                p.text = p.text.replace(d['variable'], d['value'])
    
    doc.save(filenameOutput)

replace_string('test.docx', 'test2.pdf')

Advertisement

Answer

import docx2pdf
def convert_file(filenameInput, filenameOutput):
    docx2pdf.convert(filenameInput, filenameOutput)

convert_file('test.docx', 'test2.pdf')

There is a Python package called docx2pdf. You can use it to simply convert docx file into pdf!

There is a link to the package! https://pypi.org/project/docx2pdf/

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement