Python – Microsoft Word to PDF

The following will convert an existing Microsoft Word document into a PDF file type. I find this especially useful for bulk conversions before doing some form of mass mailout.

Find the full source code for this project below:

import comtypes.client
import time
from pathlib import Path


wdFormatPDF = 17
file="Some Document.docx"

if Path(file).is_file():
    out_file="Some PDF.pdf"
    word = comtypes.client.CreateObject('Word.Application')
    word.Visible = True
    time.sleep(3)
    doc=word.Documents.Open(file) 
    doc.SaveAs(out_file, FileFormat=wdFormatPDF) 
    doc.Close()
    word.Visible = False
        

  

Leave a Reply