How to convert DOC files to PDF files in Python

1094090 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: 4  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
    118,040 pointsBadges:
    report
  • Girish81
    Hi, can someone send the code on how to convert multiple doc resumes to docx resumes?
    10 pointsBadges:
    report
  • Subhendu Sen
    @Girish81, First of all take backup of these MS Word files and keep in a safe place. Then open the CMD (Command Prompt) and go to the same folder where you want to change the file name extensions. After that, type the command as,

    ren *.* *.docx 
    and press Enter key. Next you can see all files of this particular directory/ folder changed to the same extension. Keep in mind, in this folder if there are some other files with different extensions, please move them to another drive.

    118,040 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: