docx2pdf Library

0.1.8 · active · verified Mon Apr 13

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

Install

Imports

Quickstart

This quickstart demonstrates how to use the `convert` function for both single file and directory conversions. To run this code successfully, you must have Microsoft Word installed (on Windows or macOS) and provide actual .docx files/directories as input. The actual conversion calls are commented out to prevent errors in environments without Word or input files.

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.")

view raw JSON →