Operations 10 min read

Batch Convert Word Documents to PDF with Python and Win32COM

This guide demonstrates how to create a Python script that uses the win32com library to batch convert Word (.doc/.docx) files to PDF on Windows, including code examples, handling of Office/WPS detection, folder management, common pitfalls, and packaging the script into an executable.

Rare Earth Juejin Tech Community
Rare Earth Juejin Tech Community
Rare Earth Juejin Tech Community
Batch Convert Word Documents to PDF with Python and Win32COM

Idea: Word to PDF is just batch processing

The core task is to open each Word document programmatically and save it as a PDF, which can be done efficiently with a few lines of Python code.

Basic script using win32com

The simplest implementation imports win32com.client and defines a function that opens a Word file and saves it as PDF (FileFormat=17).

import os
import win32com.client

def word_to_pdf(input_path, output_path):
    word = win32com.client.Dispatch("Word.Application")
    word.Visible = False  # run in background
    doc = word.Documents.Open(input_path)
    doc.SaveAs(output_path, FileFormat=17)  # 17 = PDF
    doc.Close()
    word.Quit()

# example usage
word_to_pdf("C:/Users/YourName/Desktop/test.docx", "C:/Users/YourName/Desktop/test.pdf")

Explanation of key lines

Dispatch("Word.Application") launches the Word COM object.

FileFormat=17 tells Word to save the document as PDF.

Calling Quit() ensures Word exits and does not stay in memory.

If you use WPS instead of Microsoft Office, replace the dispatch string with Dispatch("kwps.Application") .

Batch conversion for a whole folder

To process many files at once, iterate over the files in a directory, open each .doc/.docx file, convert it, and handle errors.

def batch_convert(folder_path):
    word = win32com.client.Dispatch("Word.Application")
    word.Visible = False

    for file in os.listdir(folder_path):
        if file.endswith(".doc") or file.endswith(".docx"):
            doc_path = os.path.join(folder_path, file)
            pdf_path = os.path.splitext(doc_path)[0] + ".pdf"
            try:
                doc = word.Documents.Open(doc_path)
                doc.SaveAs(pdf_path, FileFormat=17)
                doc.Close()
                print(f"✅ Conversion succeeded: {file}")
            except Exception as e:
                print(f"❌ Conversion failed: {file}, reason: {e}")
    word.Quit()

Common pitfalls

System must be Windows with Microsoft Office (or WPS) installed; the script relies on COM.

Documents containing macros or password protection may cause errors.

Avoid long file names or paths with Chinese characters or spaces; use plain‑ASCII folders.

Extra features

Additional utilities can be added, such as automatically creating a timestamped output folder, scanning the script’s directory for Word files, detecting whether Office or WPS is installed, and packaging the script as an executable with pyinstaller .

def gen_output_folder():
    timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
    output_folder = os.path.join(os.path.dirname(os.path.abspath(__file__)), f"pdf_{timestamp}")
    os.makedirs(output_folder, exist_ok=True)
    return output_folder

Detecting the available engine:

def detect_office_or_wps():
    try:
        win32com.client.gencache.EnsureDispatch("Word.Application")
        return "office"
    except:
        try:
            win32com.client.gencache.EnsureDispatch("Kwps.Application")
            return "wps"
        except:
            return None

Putting everything together:

def batch_convert_here():
    engine = detect_office_or_wps()
    if not engine:
        print("No Office or WPS installed")
        return
    folder = os.path.dirname(os.path.abspath(__file__))
    word_files = [f for f in os.listdir(folder) if f.endswith('.doc') or f.endswith('.docx')]
    if not word_files:
        print("No Word files found in the current folder")
        return
    output_folder = gen_output_folder()
    for word_file in word_files:
        pdf_path = os.path.join(output_folder, os.path.splitext(word_file)[0] + '.pdf')
        convert_word_to_pdf_auto(os.path.join(folder, word_file), pdf_path, engine)
    print("All conversions completed!")

if __name__ == "__main__":
    batch_convert_here()

Packaging as an executable

Finally, the script can be turned into a standalone .exe using PyInstaller:

pyinstaller -F word2pdf.py

The generated dist/word2pdf.exe can be distributed to users who only need to run the conversion without installing Python.

Pythonautomationbatch processingPDFscriptwordwin32com
Rare Earth Juejin Tech Community
Written by

Rare Earth Juejin Tech Community

Juejin, a tech community that helps developers grow.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.