Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (16.4k points)
Is there any way for converting .doc files into pdf using python? I know it is possible by using python COM automation, but I don't know how to start. I couldn't able to find the solution on the web too. Anyone help me to clear my doubt!

1 Answer

0 votes
by (26.4k points)

Using comtypes, you can convert any file, where input and output filenames are given as commandline arguments. 

For more details about comtype, click on this link

Look at the following code,

import sys

import os

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

doc = word.Documents.Open(in_file)

doc.SaveAs(out_file, FileFormat=wdFormatPDF)

doc.Close()

word.Quit()

You can also use pywin32, which would be same except:

import win32com.client

then,

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

Wanna become a python expert? Come and join: Python ceritification course

For more details about python, do check out..

Related questions

0 votes
1 answer
0 votes
1 answer
asked Jan 2, 2021 in Python by ashely (50.2k points)
0 votes
1 answer
asked Dec 2, 2020 in Python by ashely (50.2k points)

Browse Categories

...