docx2pdf

repository·main·Indexed 20 days ago

https://github.com/aljohri/docx2pdf

A utility for converting DOCX files to PDF on Windows and macOS using the installed version of Microsoft Word via win32com or JXA. It provides both a CLI for single and batch conversions and a Python library featuring the convert() and resolve_paths() functions.

Tokens
1.5K
Snippets
7
Records
7
Agent score
20%

What's inside docx2pdf

  1. Install docx2pdf

    main

    You can install docx2pdf using Homebrew (macOS), pipx, or pip.

    Note: This tool requires Microsoft Word to be installed on your system to function. It uses win32com on Windows and JXA (JavaScript for Automation) on macOS.

    # macOS
    brew install aljohri/-/docx2pdf
    
    # Via pipx
    pipx install docx2pdf
    
    # Via pip
    pip install docx2pdf
  2. Configure docx2pdf for Jupyter Notebook

    main

    To ensure the tqdm progress bar renders correctly within a Jupyter Notebook environment, you must install and enable ipywidgets.

    pip install ipywidgets
    jupyter nbextension enable --py widgetsnbextension
  3. Use docx2pdf as a Python library

    main

    Import the convert function from docx2pdf to perform conversions programmatically. The library API mirrors the CLI functionality.

    from docx2pdf import convert
    
    # Convert single file in-place
    convert("input.docx")
    
    # Convert single file to specific output
    convert("input.docx", "output.pdf")
    
    # Batch convert an entire folder
    convert("my_docx_folder/")
  4. Reference docx2pdf CLI arguments

    main

    The following flags and arguments are available for the docx2pdf command-line interface.

    positional arguments:
      input          input file or folder. batch converts entire folder or convert
                     single file
      output         output file or folder
    
    optional arguments:
      -h, --help     show this help message and exit
      --keep-active  prevent closing word after conversion
      --version      display version and exit
  5. Use the docx2pdf CLI

    main

    The CLI allows for single file conversion, batch folder conversion, and specifying custom output paths.

    Usage Pattern: docx2pdf [options] input [output]

    Common Commands:

    • Convert a single file in-place: docx2pdf myfile.docx
    • Batch convert a folder in-place: docx2pdf myfolder/
    • Convert a single file to a specific output path: docx2pdf input.docx output.pdf
    • Convert a single file to a specific output directory: docx2pdf input.docx output_dir/
    • Batch convert a folder to a different output directory: docx2pdf input_dir/ output_dir/
    # Example: Convert single docx file in-place
    docx2pdf myfile.docx
    
    # Example: Batch convert docx folder in-place
    docx2pdf myfolder/
    
    # Example: Convert single docx file with explicit output filepath
    docx2pdf input.docx output.pdf
    
    # Example: Batch convert docx folder to a different explicit folder
    docx2pdf input_dir/ output_dir/
  6. Resolve DOCX and PDF paths with resolve_paths()

    main

    The resolve_paths function determines whether a conversion is a single-file operation or a batch operation and calculates the resulting input and output paths.

    Behavior:

    • If input_path is a directory, batch is set to True and the output directory is derived from the input directory (or the provided output_path).
    • If input_path is a file, batch is set to False. The output path is generated by changing the file extension to .pdf unless an explicit output file is provided.

    Returns: A dictionary containing:

    • batch (bool): True if converting a folder, False for a single file.
    • input (str): The resolved absolute path to the input.
    • output (str): The resolved absolute path to the output file or directory.
    from docx2pdf import resolve_paths
    
    # Example of path resolution logic
    paths = resolve_paths("my_document.docx", "custom_output.pdf")
    # Result: {'batch': False, 'input': '.../my_document.docx', 'output': '.../custom_output.pdf'}
  7. Convert DOCX to PDF using convert()

    main

    The convert function is the primary entrypoint for converting Microsoft Word documents to PDF. It automatically detects your operating system and uses the appropriate method (via win32com on Windows or osascript on macOS).

    Parameters:

    • input_path (str or Path): The path to a single .doc or .docx file, or a directory containing multiple documents for batch conversion.
    • output_path (str or Path, optional): The destination file path or directory. If not provided, the PDF will be created in the same location as the input.
    • keep_active (bool, default: False): If set to True, the Microsoft Word application instance will remain open after the conversion process completes.
    from docx2pdf import convert
    
    # Convert a single file
    convert("myfile.docx")
    
    # Convert a single file to a specific output path
    convert("input.docx", "output.pdf")
    
    # Batch convert a folder
    convert("myfolder/")
    
    # Batch convert a folder to a different output directory
    convert("input_dir/", "output_dir/")