### Install docx2pdf via pip Source: https://github.com/aljohri/docx2pdf/blob/main/README.md Install the docx2pdf package using pip, the standard package installer for Python. ```bash pip install docx2pdf ``` -------------------------------- ### Install ipywidgets for Jupyter Notebook Source: https://github.com/aljohri/docx2pdf/blob/main/README.md Instructions for installing ipywidgets and enabling the Jupyter Notebook extension, required for the tqdm progress bar to render properly in Jupyter environments. ```bash pip install ipywidgets jupyter nbextension enable --py widgetsnbextension ``` -------------------------------- ### CLI Usage Examples for docx2pdf Source: https://github.com/aljohri/docx2pdf/blob/main/README.md Demonstrates various command-line interface (CLI) usages for converting DOCX files to PDF, including single file conversion, batch conversion, and specifying output paths. ```bash docx2pdf myfile.docx ``` ```bash docx2pdf myfolder/ ``` ```bash docx2pdf input.docx output.pdf ``` ```bash docx2pdf input.docx output_dir/ ``` ```bash docx2pdf input_dir/ output_dir/ ``` -------------------------------- ### Install docx2pdf via pipx Source: https://github.com/aljohri/docx2pdf/blob/main/README.md Install the docx2pdf package using pipx, a tool for installing and running Python applications in isolated environments. ```bash pipx install docx2pdf ``` -------------------------------- ### Install docx2pdf via Homebrew Source: https://github.com/aljohri/docx2pdf/blob/main/README.md Use this command to install the docx2pdf package on macOS using Homebrew. ```bash brew install aljohri/-/docx2pdf ``` -------------------------------- ### Convert DOCX to PDF using CLI Source: https://context7.com/aljohri/docx2pdf/llms.txt The `docx2pdf` command-line tool supports single file, batch, and explicit output path conversions. Use `--help` for options and `--version` to check the installed version. ```bash # Show help docx2pdf --help # Show installed version docx2pdf --version # Convert a single file in-place docx2pdf myfile.docx # → myfile.pdf written next to myfile.docx # Convert with an explicit output file docx2pdf input.docx output.pdf # Convert into a specific folder docx2pdf input.docx output_dir/ # Batch-convert an entire folder in-place docx2pdf myfolder/ # Batch-convert to a separate output folder docx2pdf input_dir/ output_dir/ ``` -------------------------------- ### Batch Convert Documents in Jupyter Notebook Source: https://context7.com/aljohri/docx2pdf/llms.txt Convert all .docx files in a directory to .pdf files and display a live progress bar within the Jupyter cell output. This requires `ipywidgets` to be installed. ```python from docx2pdf import convert # Batch-convert all drafts and display a live progress bar in the cell output convert("drafts/", "pdfs/") ``` -------------------------------- ### convert(input_path, output_path=None, keep_active=False) Source: https://context7.com/aljohri/docx2pdf/llms.txt The primary Python API for converting DOCX files to PDF. It handles single file or batch conversions, supports explicit output paths, and can keep Microsoft Word active between calls. ```APIDOC ## `convert(input_path, output_path=None, keep_active=False)` ### Description The primary Python API. Resolves input/output paths, detects the host platform, and dispatches to the appropriate Word automation backend. When `input_path` is a directory, it performs a batch conversion of every `.doc`/`.docx` file found inside it. When `output_path` is omitted, the PDF is written alongside the source file with the same stem. Setting `keep_active=True` prevents the function from quitting Microsoft Word after conversion, which is useful when Word is already open or when converting many files in sequence from application code. ### Parameters - **input_path** (str) - Required - Path to the input DOCX file or directory. - **output_path** (str, optional) - Path to the output PDF file or directory. If omitted, the PDF is saved next to the source file. - **keep_active** (bool, optional) - If True, keeps Microsoft Word open after conversion. Defaults to False. ### Request Example ```python from docx2pdf import convert # Convert a single file in-place convert("report.docx") # Convert with an explicit output file path convert("report.docx", "exports/report.pdf") # Convert a single file into an existing output directory convert("report.docx", "exports/") # Batch-convert all .docx files in a folder in-place convert("docs/") # Batch-convert a folder and write PDFs to a separate output directory convert("docs/", "pdfs/") # Keep Microsoft Word open after conversion convert("draft.docx", keep_active=True) ``` ### Error Handling ```python import sys try: convert("input.docx", "output.pdf") except NotImplementedError as e: # Raised on Linux: "docx2pdf is not implemented for linux …" print(f"Platform not supported: {e}", file=sys.stderr) sys.exit(1) except AssertionError as e: # Raised when paths are invalid (wrong extension, missing output dir, …) print(f"Path error: {e}", file=sys.stderr) sys.exit(1) ``` ``` -------------------------------- ### Convert DOCX to PDF using Python API Source: https://context7.com/aljohri/docx2pdf/llms.txt Use the `convert` function for single file or batch conversions. Specify output paths or let it default to the input directory. `keep_active=True` prevents Word from closing after conversion. ```python from docx2pdf import convert # 1. Convert a single file in-place (output: report.pdf next to report.docx) convert("report.docx") # 2. Convert with an explicit output file path convert("report.docx", "exports/report.pdf") # 3. Convert a single file into an existing output directory convert("report.docx", "exports/") # → writes exports/report.pdf # 4. Batch-convert all .docx files in a folder in-place convert("docs/") # → each docs/*.docx becomes docs/*.pdf # 5. Batch-convert a folder and write PDFs to a separate output directory convert("docs/", "pdfs/") # → each docs/*.docx becomes pdfs/*.pdf # 6. Keep Microsoft Word open after conversion (avoids re-launch overhead # when calling convert() multiple times in the same process) convert("draft.docx", keep_active=True) convert("final.docx", keep_active=True) # Word remains open; call convert("last.docx") without keep_active to quit. # Error handling — Word not installed or wrong platform import sys try: convert("input.docx", "output.pdf") except NotImplementedError as e: # Raised on Linux: "docx2pdf is not implemented for linux …" print(f"Platform not supported: {e}", file=sys.stderr) sys.exit(1) except AssertionError as e: # Raised when paths are invalid (wrong extension, missing output dir, …) print(f"Path error: {e}", file=sys.stderr) sys.exit(1) ``` -------------------------------- ### Library Usage for docx2pdf Source: https://github.com/aljohri/docx2pdf/blob/main/README.md Shows how to use the convert function from the docx2pdf library in Python scripts to convert DOCX files to PDF. ```python from docx2pdf import convert convert("input.docx") ``` ```python convert("input.docx", "output.pdf") ``` ```python convert("my_docx_folder/") ``` -------------------------------- ### CLI — docx2pdf Source: https://context7.com/aljohri/docx2pdf/llms.txt The command-line interface for docx2pdf, which wraps the Python `convert` function and provides an `argparse` interface for direct use in the terminal. ```APIDOC ## CLI — `docx2pdf` ### Description The installed command-line entry-point wraps the Python `convert` function with an `argparse` interface. It supports all the same path patterns as the library and adds a `--keep-active` flag and a `--version` flag. ### Usage Examples - **Show help** ```bash docx2pdf --help ``` - **Show installed version** ```bash docx2pdf --version ``` - **Convert a single file in-place** ```bash docx2pdf myfile.docx # → myfile.pdf written next to myfile.docx ``` - **Convert with an explicit output file** ```bash docx2pdf input.docx output.pdf ``` - **Convert into a specific folder** ```bash docx2pdf input.docx output_dir/ ``` - **Batch-convert an entire folder in-place** ```bash docx2pdf myfolder/ ``` - **Batch-convert to a separate output folder** ```bash docx2pdf input_dir/ output_dir/ ``` ``` -------------------------------- ### Keep Word Running After Conversion CLI Source: https://context7.com/aljohri/docx2pdf/llms.txt Use the --keep-active flag to prevent Word from closing after conversion, which is useful for CI to avoid repeated launches. ```bash docx2pdf --keep-active report.docx ``` -------------------------------- ### Resolve Input/Output Paths with Python API Source: https://context7.com/aljohri/docx2pdf/llms.txt The `resolve_paths` function helps predict output locations without running Word. It normalizes paths and determines if the operation is a batch or single-file conversion. ```python from docx2pdf import resolve_paths # Single file, no explicit output → PDF placed next to source result = resolve_paths("reports/q1.docx", None) # result == { # "batch": False, # "input": "/abs/path/reports/q1.docx", # "output": "/abs/path/reports/q1.pdf" # } # Single file with explicit output directory result = resolve_paths("reports/q1.docx", "exports/") # result == { # "batch": False, # "input": "/abs/path/reports/q1.docx", # "output": "/abs/path/exports/q1.pdf" # } # Batch mode (input is a directory) result = resolve_paths("reports/", "exports/") # result == { # "batch": True, # "input": "/abs/path/reports", # "output": "/abs/path/exports" # must already exist # } # Predict the output path without running Word paths = resolve_paths("invoice.docx", None) print(f"PDF will be written to: {paths['output']}") ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.