{"id":5197,"library":"docx2pdf","title":"docx2pdf Library","description":"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.","status":"active","version":"0.1.8","language":"en","source_language":"en","source_url":"https://github.com/AlJohri/docx2pdf","tags":["document conversion","docx","pdf","microsoft word","windows","macos"],"install":[{"cmd":"pip install docx2pdf","lang":"bash","label":"Install latest version"}],"dependencies":[],"imports":[{"symbol":"convert","correct":"from docx2pdf import convert"}],"quickstart":{"code":"import os\nfrom docx2pdf import convert\n\n# This example demonstrates the API usage. For a full test,\n# you need an actual 'example.docx' file in the current directory\n# and Microsoft Word installed on your system (Windows/macOS).\n\n# Example 1: Convert a single file\ninput_file = \"example.docx\" # Replace with your .docx file path\noutput_file = \"example.pdf\"\n\n# Uncomment the following block to run if 'example.docx' exists\n# if os.path.exists(input_file):\n#     print(f\"Converting {input_file} to {output_file}...\")\n#     try:\n#         convert(input_file, output_file)\n#         print(\"Conversion complete.\")\n#     except Exception as e:\n#         print(f\"Error during conversion: {e}\")\n# else:\n#     print(f\"Skipping single file conversion: '{input_file}' not found.\")\n\n# Example 2: Convert all docx files in a directory\n# For this, create a directory like './input_docs' with .docx files.\n# input_dir = \"./input_docs\"\n# output_dir = \"./output_pdfs\"\n\n# if os.path.exists(input_dir):\n#     if not os.path.exists(output_dir):\n#         os.makedirs(output_dir)\n#     print(f\"Converting all .docx files in '{input_dir}' to '{output_dir}'...\")\n#     try:\n#         convert(input_dir, output_dir)\n#         print(\"Directory conversion complete.\")\n#     except Exception as e:\n#         print(f\"Error during directory conversion: {e}\")\n# else:\n#     print(f\"Skipping directory conversion: '{input_dir}' not found.\")\n","lang":"python","description":"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."},"warnings":[{"fix":"Ensure Microsoft Word is installed, properly licensed, and runnable on the machine where the code is executed.","message":"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.","severity":"breaking","affected_versions":"All versions"},{"fix":"For cross-platform or server-side conversion, consider cloud-based APIs (e.g., Google Docs API, Adobe PDF Services) or tools that use LibreOffice/OpenOffice (e.g., `unoconv`) for headless conversions.","message":"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.","severity":"gotcha","affected_versions":"All versions"},{"fix":"For bulk conversions, consider processing documents in batches or with delays between calls to allow Word to close properly. Implement robust error handling for Word process failures.","message":"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.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Always create the output directory using `os.makedirs(output_dir, exist_ok=True)` before calling `convert` for directory conversions.","message":"When converting entire directories, ensure the output directory exists before calling `convert`. If the target output directory does not exist, the function might fail.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-13T00:00:00.000Z","next_check":"2026-07-12T00:00:00.000Z"}