How to convert DOC files to PDF files in Python

1079845 pts.
Tags:
.DOC file
PDF Files
Python
My partner and I have been given this project that we have convert hundreds of .DOC files to PDF files. We currently have Microsoft Word 2010. Is there a way to automatically do this using Python? Any help would be great.

Answer Wiki

Thanks. We'll let you know when a new response is added.

You can try something like this

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

Discuss This Question: 2  Replies

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.
  • Vamshikrishna
    Hi,

    This works perfectly for PDF. However, I want to check is there any way to convert doc files to rtf using the same script. I mean, can 'comtypes' handle rtf conversion as well? 

    Appreciate your reply.

    Regards,
    Vamshi
    10 pointsBadges:
    report
  • Subhendu Sen
    @Vamshi, you can try the same with python docx module. This can help to just extract contains to a text based format. Here is a link that can assist you, http://python-docx.readthedocs.io/en/latest/user/documents.html
    109,680 pointsBadges:
    report

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

To follow this tag...

There was an error processing your information. Please try again later.

Thanks! We'll email you when relevant content is added and updated.

Following

Share this item with your network: