docx2pdf Library
The docx2pdf library provides a simple Pythonic way to convert Microsoft Word .docx files to PDF format. It works exclusively on Windows and macOS, leveraging an installed version of Microsoft Word to perform the conversion. The library is currently at version 0.1.8 and sees updates on an as-needed basis.
Warnings
- breaking The library *requires* Microsoft Word to be installed on the system (Windows or macOS). It is not a standalone converter. If Word is not installed, the conversion will fail with a COM error on Windows or an AppleScript error on macOS.
- gotcha docx2pdf is platform-specific and only works on Windows and macOS. It cannot be used on Linux servers or environments without a graphical interface and an installed copy of Microsoft Word.
- gotcha Performance can vary significantly. Starting Microsoft Word, opening the document, and saving it as PDF can be slow, especially for the first conversion or with complex documents. Multiple conversions can also lead to Word instances accumulating.
- gotcha When converting entire directories, ensure the output directory exists before calling `convert`. If the target output directory does not exist, the function might fail.
Install
-
pip install docx2pdf
Imports
- convert
from docx2pdf import convert
Quickstart
import os
from docx2pdf import convert
# This example demonstrates the API usage. For a full test,
# you need an actual 'example.docx' file in the current directory
# and Microsoft Word installed on your system (Windows/macOS).
# Example 1: Convert a single file
input_file = "example.docx" # Replace with your .docx file path
output_file = "example.pdf"
# Uncomment the following block to run if 'example.docx' exists
# if os.path.exists(input_file):
# print(f"Converting {input_file} to {output_file}...")
# try:
# convert(input_file, output_file)
# print("Conversion complete.")
# except Exception as e:
# print(f"Error during conversion: {e}")
# else:
# print(f"Skipping single file conversion: '{input_file}' not found.")
# Example 2: Convert all docx files in a directory
# For this, create a directory like './input_docs' with .docx files.
# input_dir = "./input_docs"
# output_dir = "./output_pdfs"
# if os.path.exists(input_dir):
# if not os.path.exists(output_dir):
# os.makedirs(output_dir)
# print(f"Converting all .docx files in '{input_dir}' to '{output_dir}'...")
# try:
# convert(input_dir, output_dir)
# print("Directory conversion complete.")
# except Exception as e:
# print(f"Error during directory conversion: {e}")
# else:
# print(f"Skipping directory conversion: '{input_dir}' not found.")