Sunday 5 January 2020

Convert DOC to PDF using python

Below is the code:

import sys
import os
import win32com.client

wdFormatPDF = 17


in_file = os.path.abspath(sys.argv[1])

out_file = os.path.abspath(sys.argv[2])

word = win32com.client.Dispatch('Word.Application')

doc = word.Documents.Open(in_file)
doc.SaveAs(out_file, FileFormat=wdFormatPDF)
doc.Close()
word.Quit()

Execute from command line :


>python doc2pdf.py test.docx test.pdf


import comtypes.client

wdFormatPDF = 17

in_file = os.path.abspath(sys.argv[1])
out_file = os.path.abspath(sys.argv[2])

word = comtypes.client.CreateObject('Word.Application')