Skip to main content

Python bindings for MuPDF library.

Project description

Summary

  • Python bindings for the MuPDF PDF library.

  • A python module called mupdf.

  • Generated from the MuPDF C++ API, which is itself generated from the MuPDF C API.

  • Provides Python functions that wrap most fz_ and pdf_ functions.

  • Provides Python classes that wrap most fz_ and pdf_ structs.

    • Class methods provide access to most of the underlying C API functions (except for functions that don’t take struct args such as fz_strlcpy()).

  • MuPDF’s setjmp/longjmp exceptions are converted to Python exceptions.

  • Functions and methods do not take fz_context arguments. (Automatically-generated per-thread contexts are used internally.)

  • Wrapper classes automatically handle reference counting of the underlying structs (with internal calls to fz_keep_*() and fz_drop_*()).

  • Support for MuPDF function pointers with SWIG Director classes, allowing MuPDF to call Python callbacks.

  • Provides a small number of extensions beyond the basic C API:

    • Some generated classes have extra support for iteration.

    • Some custom class methods and constructors.

    • Simple ‘POD’ structs have __str__() methods, for example mupdf.Rect is represented like: (x0=90.51 y0=160.65 x1=501.39 y1=215.6).

Example usage

Minimal Python code that uses the mupdf module:

import mupdf
document = mupdf.Document('foo.pdf')

A simple example Python test script (run by scripts/mupdfwrap.py -t) is:

  • scripts/mupdfwrap_test.py

More detailed usage of the Python API can be found in:

  • scripts/mutool.py

  • scripts/mutool_draw.py

Here is some example code that shows all available information about document’s Stext blocks, lines and characters:

#!/usr/bin/env python3

import mupdf

def show_stext(document):
    '''
    Shows all available information about Stext blocks, lines and characters.
    '''
    for p in range(document.count_pages()):
        page = document.load_page(p)
        stextpage = mupdf.StextPage(page, mupdf.StextOptions())
        for block in stextpage:
            block_ = block.m_internal
            log(f'block: type={block_.type} bbox={block_.bbox}')
            for line in block:
                line_ = line.m_internal
                log(f'    line: wmode={line_.wmode}'
                        + f' dir={line_.dir}'
                        + f' bbox={line_.bbox}'
                        )
                for char in line:
                    char_ = char.m_internal
                    log(f'        char: {chr(char_.c)!r} c={char_.c:4} color={char_.color}'
                            + f' origin={char_.origin}'
                            + f' quad={char_.quad}'
                            + f' size={char_.size:6.2f}'
                            + f' font=('
                                +  f'is_mono={char_.font.flags.is_mono}'
                                + f' is_bold={char_.font.flags.is_bold}'
                                + f' is_italic={char_.font.flags.is_italic}'
                                + f' ft_substitute={char_.font.flags.ft_substitute}'
                                + f' ft_stretch={char_.font.flags.ft_stretch}'
                                + f' fake_bold={char_.font.flags.fake_bold}'
                                + f' fake_italic={char_.font.flags.fake_italic}'
                                + f' has_opentype={char_.font.flags.has_opentype}'
                                + f' invalid_bbox={char_.font.flags.invalid_bbox}'
                                + f' name={char_.font.name}'
                                + f')'
                            )

document = mupdf.Document('foo.pdf')
show_stext(document)

More information

https://mupdf.com/r/C-and-Python-APIs

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mupdf-1.21.1.20230112.1504.tar.gz (83.1 MB view hashes)

Uploaded Source

Built Distributions

mupdf-1.21.1.20230112.1504-cp311-none-win_amd64.whl (31.1 MB view hashes)

Uploaded CPython 3.11 Windows x86-64

mupdf-1.21.1.20230112.1504-cp311-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (38.4 MB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.17+ x86-64

mupdf-1.21.1.20230112.1504-cp310-none-win_amd64.whl (31.1 MB view hashes)

Uploaded CPython 3.10 Windows x86-64

mupdf-1.21.1.20230112.1504-cp310-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (38.4 MB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ x86-64

mupdf-1.21.1.20230112.1504-cp39-none-win_amd64.whl (31.1 MB view hashes)

Uploaded CPython 3.9 Windows x86-64

mupdf-1.21.1.20230112.1504-cp39-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (38.4 MB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ x86-64

mupdf-1.21.1.20230112.1504-cp38-none-win_amd64.whl (31.1 MB view hashes)

Uploaded CPython 3.8 Windows x86-64

mupdf-1.21.1.20230112.1504-cp38-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (38.4 MB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.17+ x86-64

mupdf-1.21.1.20230112.1504-cp37-none-win_amd64.whl (31.1 MB view hashes)

Uploaded CPython 3.7 Windows x86-64

mupdf-1.21.1.20230112.1504-cp37-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (38.4 MB view hashes)

Uploaded CPython 3.7 manylinux: glibc 2.17+ x86-64

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page