Last modified: Feb 07, 2026 By Alexander Williams

Convert DOCX to PDF with Python Easily

You need to convert Word documents to PDFs. Python can automate this task. This guide shows you how.

We will cover the best libraries. You will learn step-by-step methods. We include code examples you can run.

Why Convert DOCX to PDF with Python?

PDFs are the standard for sharing documents. They keep formatting consistent across all devices.

Manually saving as PDF is slow for many files. Python scripts can convert hundreds of files automatically.

This is useful for reports, invoices, or batch processing. Automation saves time and reduces errors.

Prerequisites for Python DOCX to PDF Conversion

You need Python installed. Version 3.7 or newer is recommended. You also need to install specific libraries.

We will use pip, the Python package installer. Open your terminal or command prompt to begin.

Basic Python knowledge is helpful. But the code examples are clear for beginners.

Method 1: Using python-docx and reportlab

This method is great for control. You read the DOCX content with python-docx. Then you write it to a PDF with reportlab.

First, install the required libraries.


pip install python-docx reportlab
    

Now, let's write a script. This example reads text and basic formatting.


from docx import Document
from reportlab.lib.pagesizes import letter
from reportlab.pdfgen import canvas

def docx_to_pdf_python_docx(docx_path, pdf_path):
    # Open the DOCX file
    doc = Document(docx_path)
    # Create a PDF canvas
    c = canvas.Canvas(pdf_path, pagesize=letter)
    y = 750  # Starting Y position on the page
    line_height = 14

    # Iterate through paragraphs in the document
    for paragraph in doc.paragraphs:
        if y < 50:  # Check for new page
            c.showPage()
            y = 750
        # Draw the paragraph text on the PDF
        c.drawString(50, y, paragraph.text)
        y -= line_height

    # Save the PDF
    c.save()
    print(f"PDF saved to: {pdf_path}")

# Example usage
docx_to_pdf_python_docx("input.docx", "output_method1.pdf")
    

This script creates a basic PDF. It preserves the text order. For complex formatting, you need more reportlab code.

You can learn to add metadata to your new PDFs using our guide on Python PdfWriter.add_metadata.

Method 2: Using the docx2pdf Library

The docx2pdf library is simpler. It acts as a wrapper for Microsoft Word on Windows and macOS.

It requires Word or LibreOffice to be installed. It's perfect for quick, faithful conversions.


pip install docx2pdf
    

The conversion code is very straightforward.


from docx2pdf import convert

# Convert a single file
convert("input.docx", "output_method2.pdf")

# Convert all DOCX files in a folder
convert("./docx_files/", "./pdf_output/")
    

This method is excellent for batch processing. The output PDF will look exactly like the original Word document.

Method 3: Using LibreOffice in Headless Mode

This is a powerful, platform-independent method. It uses LibreOffice's command-line interface.

You must have LibreOffice installed on your system. The subprocess module calls it.


import subprocess
import os

def convert_docx_to_pdf_libreoffice(docx_path, output_folder):
    # Construct the LibreOffice command
    command = [
        'soffice',
        '--headless',
        '--convert-to', 'pdf',
        '--outdir', output_folder,
        docx_path
    ]
    
    # Run the command
    result = subprocess.run(command, capture_output=True, text=True)
    
    if result.returncode == 0:
        print("Conversion successful!")
        # Get the generated PDF filename
        pdf_name = os.path.splitext(os.path.basename(docx_path))[0] + '.pdf'
        pdf_path = os.path.join(output_folder, pdf_name)
        print(f"PDF saved to: {pdf_path}")
        return pdf_path
    else:
        print("Conversion failed:", result.stderr)
        return None

# Example usage
convert_docx_to_pdf_libreoffice("report.docx", "./converted_pdfs")
    

This method is very reliable. It handles complex documents well. It works on Linux, Windows, and macOS servers.

Comparing the Python DOCX to PDF Methods

Choose the right tool for your job.

python-docx + reportlab: Best for programmatic control. You can change styles and layout in code. It does not need external software.

docx2pdf: Best for simplicity and accuracy. It needs Word or LibreOffice. It's ideal for batch conversions where the output must match the input exactly.

LibreOffice CLI: Best for server environments. It is free, open-source, and handles many file formats. It is the most robust for automated systems.

Handling Errors and Advanced Tips

Always wrap your code in try-except blocks. File paths might be wrong. Permissions could be an issue.


try:
    convert("my_document.docx", "my_output.pdf")
except Exception as e:
    print(f"An error occurred: {e}")
    

For advanced PDF manipulation after conversion, you can merge your new PDF with others. Learn how with our tutorial on Python PdfFileMerger.write.

You might also need to extract data from existing PDFs. Our guide on Python PdfReader.getDocumentInfo can help you read metadata.

Conclusion

Converting DOCX to PDF with Python is straightforward. You have multiple effective methods.

Use python-docx and reportlab for custom PDF generation. Use docx2pdf for quick, accurate conversions on desktops.

Use the LibreOffice method for reliable, server-side automation. Choose the one that fits your project's needs.

Start automating your document workflow today. Save time and ensure consistency with Python.