简体   繁体   中英

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')
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/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM