Python Doc to PDF converting using

694 views Asked by At

I am trying to convert a document .docx to .pdf So I tried to use docx2pdf and comtypes then aspose.words

my code is simple:

from docx2pdf import convert
def openfile():
    convert('Xp_Tank.docx','output/XpTank.pdf')
openfile()

and the other code is:

import win32com.client
def convert2():
    wdFormatPDF = 17
    inputFile = os.path.abspath("Xp_Tank.docx")
    outputFile = os.path.abspath("output/XpTank2.pdf")
    word = win32com.client.Dispatch('Word.Application')
    doc = word.Documents.Open(inputFile)
    doc.SaveAs(outputFile, FileFormat=wdFormatPDF)
    doc.Close()
    word.Quit()
convert2()

also I tried with:

import comtypes.client
def newpdf():
    wdFormatPDF = 17
    inputFile = os.path.abspath("Xp_Tank.docx")
    outputFile = os.path.abspath("output/comtypes3.pdf")
    word = comtypes.client.CreateObject('Word.Application')
    doc = word.Documents.Open(inputFile)
    doc.SaveAs(outputFile, FileFormat=wdFormatPDF)
    doc.Close()
    word.Quit()
newpdf()

But all have the same formatting error! like the image shows:

enter image description here

SO lastly I converted it with aspose.words it works perfectly but it's not a free package!

enter image description here

Is there another way to correct one of the above codes or is there another way to do that? Many thanks

0

There are 0 answers