pypandoc

repository·master·Indexed 22 days ago

https://github.com/jessicategner/pypandoc

A thin Python wrapper for the Pandoc universal document converter that enables programmatic document conversion between various formats. It provides functions for converting files and text, managing Pandoc binary installations via download_pandoc(), and integrating with TinyTeX for PDF conversion. The library includes a CLI for version checking and binary management, and supports custom Pandoc options through extra_args and filters.

Tokens
4.3K
Snippets
21
Records
23
Agent score
76%

What's inside pypandoc

  1. Install pandoc using pypandoc

    master

    If you have installed pypandoc but not pandoc, you can download and install pandoc programmatically. This works on Windows, Mac OS X, and Linux (64-bit Intel). By default, it installs the latest version to a location that is automatically added to the search path.

    from pypandoc.pandoc_download import download_pandoc
    
    # Download the latest version to the default path
    download_pandoc()
    
    # Download a specific version
    download_pandoc(version='1.19.1')

    Alternatively, use the CLI:

    # Install latest pandoc to default path
    pypandoc download
    
    # Download a specific version
    pypandoc download --version 3.6
  2. Convert to PDF using TinyTeX

    master

    Converting to PDF requires a LaTeX engine. Pypandoc integrates with pytinytex to automate this setup. When converting to PDF, pypandoc will automatically add TinyTeX to the PATH and attempt to install missing LaTeX packages (up to 3 retries) if compilation fails.

    # Install pypandoc with tinytex support
    pip install pypandoc[tinytex]
    
    # Download TinyTeX once
    pytinytex download
    import pypandoc
    # PDF conversion now works seamlessly
    pypandoc.convert_file('document.md', 'pdf', outputfile='document.pdf')
  3. Install pypandoc via pip

    master

    You can install pypandoc using pip. Note that pypandoc is a thin wrapper and requires a pandoc installation on your system. If you want pandoc included automatically, install the pypandoc_binary package instead.

    # Install only the wrapper (requires manual pandoc installation)
    pip install pypandoc
    
    # Install the wrapper with pandoc included out of the box
    pip install pypandoc_binary
  4. Platform-specific installation behavior

    master

    The download_pandoc function uses different unpacking logic based on the operating system:

    • Linux: Uses ar and tar to extract .deb packages and copies pandoc and pandoc-citeproc to the target folder.
    • macOS (Darwin): Uses pkgutil to expand .pkg files and extracts binaries from the Payload.
    • Windows: Uses msiexec with the /a (administrative installation) flag to extract .msi files to a temporary directory.
  5. Authenticate GitHub requests using GITHUB_TOKEN

    master
    The download utility uses the GitHub API to fetch release assets. To avoid rate limiting (increasing from 60 to 1,000 requests per hour), you can set the GITHUB_TOKEN environment variable. The utility automatically adds the Authorization: token <token> header to requests directed at github.com or api.github.com.
  6. Specify the location of pandoc binaries

    master

    If pandoc is not in your PATH, or if you need to point to a specific binary (e.g., for a web server with restricted user permissions), set the PYPANDOC_PANDOC environment variable to the full path of the binary. If this variable is set, pypandoc will only search there.

    # Via shell
    export PYPANDOC_PANDOC=/home/x/whatever/pandoc
    # Via Python runtime
    import os
    os.environ.setdefault('PYPANDOC_PANDOC', '/home/x/whatever/pandoc')
  7. Run pypandoc using Docker Compose

    master

    You can run the pypandoc environment using a Docker Compose configuration. The setup includes a binding mount that synchronizes the current host directory with the container's /pypandoc/ directory, allowing code changes on the host to be immediately reflected in the running container. The container is configured to stay running (using tail -F) so that you can interact with it using docker compose exec.

    services:
      app:
        image: pypandoc
        container_name: pypan_container
        build: .
        restart: always
        volumes:
        - .:/pypandoc/
        command: tail -F ./anything
  8. Configure pypandoc logging

    master

    Pypandoc uses the standard Python logging library. Messages from Pandoc are sent to the console by default. To mute logs, add a NullHandler to the pypandoc logger before calling any conversion functions.

    import logging
    # Mute all pypandoc logging
    logging.getLogger('pypandoc').addHandler(logging.NullHandler())
  9. Retrieve Pandoc metadata and version

    master

    Use these utility functions to inspect the environment and available capabilities of the installed Pandoc binary.

    import pypandoc
    
    print(pypandoc.get_pandoc_version())  # Get version string
    print(pypandoc.get_pandoc_path())     # Get path to binary
    print(pypandoc.get_pandoc_formats())  # Get supported formats
  10. Use extra arguments and filters in pypandoc

    master

    You can pass arbitrary Pandoc options using the extra_args parameter (a list of strings) and apply Pandoc filters using the filters parameter (a list of strings).

    # Using extra_args for Pandoc options
    output = pypandoc.convert_text(
        '<h1>Primary Heading</h1>',
        'md', 
        format='html', 
        extra_args=['--atx-headers']
    )
    
    # Using filters (must be a list)
    filters = ['pandoc-citeproc']
    pdoc_args = ['--mathjax', '--smart']
    output = pypandoc.convert_file(
        'filename.md', 
        to='html5', 
        format='md', 
        extra_args=pdoc_args, 
        filters=filters
    )
  11. Convert files and text with pypandoc

    master

    Pypandoc supports two primary workflows: converting files and converting strings (text).

    • convert_file: Infers input format from the filename unless specified via the format argument. To write directly to a file (required for formats like docx, pdf, or epub), use the outputfile argument.
    • convert_text: Requires the input format to be explicitly defined via the format argument. Accepts unicode or UTF-8 encoded bytes.
    import pypandoc
    
    # Convert a file (format inferred from extension)
    output = pypandoc.convert_file('somefile.md', 'rst')
    
    # Convert a file with an explicit input format
    output = pypandoc.convert_file('somefile.txt', 'rst', format='md')
    
    # Convert a string (format must be specified)
    output = pypandoc.convert_text('# some title', 'rst', format='md')
    
    # Convert to a file (output returns an empty string)
    pypandoc.convert_file('somefile.md', 'docx', outputfile='somefile.docx')