How to Convert DOCX to PDF in Python (3 Methods)
Automating document conversion can save you hours of manual work. In this tutorial, we'll explore how to convert Word documents (DOCX) to PDF using Python. We'll cover cross-platform solutions and Windows-specific methods.
Method 1: Using docx2pdf (Recommended for Windows/macOS)
The easiest way to convert DOCX to PDF on Windows or macOS (with Microsoft Word installed) is using the
docx2pdf library. It's a simple wrapper around Microsoft Word's APIs.
First, install the library:
pip install docx2pdf
Then, use the following Python script:
from docx2pdf import convert
# Convert a single file
convert("input.docx", "output.pdf")
# Convert an entire folder of Word documents
convert("my_docs/")
Method 2: Using comtypes (Windows Only)
For more granular control on Windows, you can use comtypes to interact directly with Microsoft
Word's COM interface. This is useful if you need to customize the export settings.
import sys
import os
import comtypes.client
# Word's PDF format code
wdFormatPDF = 17
in_file = os.path.abspath("input.docx")
out_file = os.path.abspath("output.pdf")
word = comtypes.client.CreateObject('Word.Application')
doc = word.Documents.Open(in_file)
doc.SaveAs(out_file, FileFormat=wdFormatPDF)
doc.Close()
word.Quit()
Method 3: Using LibreOffice (Linux/Headless)
If you are running on a Linux server (like Ubuntu or CentOS) without Microsoft Office, you can use LibreOffice in
headless mode via Python's subprocess module.
import subprocess
# Ensure LibreOffice is installed first
# sudo apt-get install libreoffice
subprocess.run(['libreoffice', '--headless', '--convert-to', 'pdf', 'input.docx'])
No-Code Alternative: HappyConvert
Don't want to write code, manage Python environments, or install Microsoft Word on your server? HappyConvert offers a fast, secure, and easy way to convert your documents online.
- No Python installation required
- Works on any device (Windows, Mac, Linux, Mobile)
- Batch processing support
- High-quality conversion keeping formatting intact



