Skip to main content

pdfdol

Data Object Layer for PDF data: read the pdfs of a folder as a dict-like object of their text, pull out figures, tables and quoted passages, and convert ebooks and documents to pdf.

pip install pdfdol

Documentation

Examples

Pdf "Stores"

Get a dict-like object to list and read the pdfs of a folder, as text:

>>> from pdfdol import PdfFilesReader
>>> from pdfdol.tests import get_test_pdf_folder
>>> folder_path = get_test_pdf_folder()
>>> pdfs = PdfFilesReader(folder_path)
>>> sorted(pdfs)
['sample_pdf_1', 'sample_pdf_2']
>>> assert pdfs['sample_pdf_2'] == [
...     'Page 1\nThis is a sample text for testing Python PDF tools.'
... ]

See that the values of a PdfFilesReader are lists of pages. If you need strings (i.e. all the pages together) you can add a decoder like so:

from dol import add_decoder
page_separator = '---------------------'
pdfs = add_decoder(pdfs, decoder=page_separator.join)

If you need this at the level of the class, just do this:

from dol import add_decoder
page_separator = '---------------------'
FilesReader = add_decoder(PdfFilesReader, decoder=page_separator.join)
# and then
pdfs = FilesReader(folder_path)
# ...

If you need to concatinate a bunch of pdfs together, you can do so in many ways. Here's one:

from dol import Files
from pdfdol import concat_pdfs

s = Files('~/Downloads/cosmograph_documentation_pdfs/')
concat_pdfs(s, key_order=sorted)

Figures, tables and quoted passages (optional PyMuPDF)

To illustrate commentary about a paper: crop a figure or a table by its caption, and find where a quote sits on its page so it can be highlighted. Needs PyMuPDF: pip install pymupdf (the figures extra).

from pdfdol import figure, figure_box, find_passage

png = figure('paper.pdf', 'Figure 3:')               # PNG bytes of the figure, caption included
png = figure('paper.pdf', 'Table 2')                 # a table (caption above or below it)
page, box, source = figure_box('paper.pdf', 'Figure 3:')  # where it is, and how it was found

p = find_passage('paper.pdf', 'the results hold across models')
p.page, p.boxes, p.source   # 0-based page, (x0, y0, x1, y1) boxes in points, 'text'

A figure is the graphics above its caption, else the short text blocks above it (a figure drawn as text), else the graphics beside it (side captions); it never grows across another caption or a body paragraph. A table is the table PyMuPDF detects next to its caption, else its rules (booktabs), else the nearer graphics. source says which, and 'caption-only' or 'fallback' says nothing better was found. Quotes match regardless of case, curly quotes, attached punctuation and words broken at a line end.

PyMuPDF is licensed AGPL-3.0, so whoever distributes or serves pdfdol together with it takes on the AGPL's terms; pdfdol itself stays MIT and imports it only when these functions are called.

On a scanned page the text layer comes from OCR and its boxes can sit off the glyphs, so find_passage returns the page with no boxes (source='none') unless you give it ocr_words, a function reading word boxes off the rendered page (pdfdol.figures.ocracy_words uses ocracy). A figure on a scanned page has no drawings to find it by, so it is read off the pixels: the ink above its caption, up to the prose or the caption above it (source='pixels'). That is still best effort; figure_box(..., scan_strategy=...) takes your own (page, caption_box, *, max_up, max_gap) -> box function, or 'layout' to treat the scan like any page.

Converting ebooks and documents to PDF (optional Calibre integration)

pdfdol natively converts images, HTML, and Markdown to PDF. For additional formats -- EPUB, MOBI, DOCX, ODT, DJVU, RTF, and many more -- install Calibre, which provides the ebook-convert command-line tool.

pdfdol does not depend on Calibre; it auto-detects the tool at runtime and uses it only for formats that have no built-in converter.

Installing ebook-convert:

Platform Command
macOS (Homebrew) brew install --cask calibre
Debian / Ubuntu sudo apt install calibre
Fedora / RHEL sudo dnf install calibre
Linux servers (headless) sudo -v && wget -nv -O- https://download.calibre-ebook.com/linux-installer.sh | sudo sh /dev/stdin

The Linux CLI installer is self-contained (no GUI/X11 needed) and recommended for servers. See https://calibre-ebook.com/download for all options.

from pdfdol import ebook_convert_to_pdf, find_ebook_convert

# Check whether Calibre is available
if find_ebook_convert():
    pdf_bytes = ebook_convert_to_pdf("book.epub")

You can also go through the usual get_pdf entry point -- it will automatically route to ebook-convert when it recognises the file extension:

from pdfdol import get_pdf
pdf_bytes = get_pdf("book.epub")                     # returns PDF bytes
get_pdf("book.epub", egress="book.pdf")              # saves to file

What cannot be converted

DRM-protected ebooks (Amazon KFX/AZW, Adobe-DRM EPUB) are encrypted, so neither Calibre nor pdfdol can read them; the DRM has to be removed first, where you are legally entitled to do so. Image-only sources (CBZ/CBR, DJVU, scanned PDF) do convert, but carry no text layer -- extracting their words needs OCR, which pdfdol does not do.

Custom converters

pdfdol maintains a format converter registry that maps file extensions to converter functions. You can register your own:

from pdfdol import register_format_converter, supported_extensions

def my_custom_converter(source):
    """source is a filepath (str) or raw bytes; must return PDF bytes."""
    ...

register_format_converter('.xyz', my_custom_converter)

# See everything that's currently supported
print(supported_extensions())

Get pdf from various sources

Example with a URL

pdf_data = get_pdf("https://pypi.org", src_kind="url")
print("Got PDF data of length:", len(pdf_data))

Example with HTML content

html_content = "<html><body><h1>Hello, PDF!</h1></body></html>"
pdf_data = get_pdf(html_content, src_kind="html")
print("Got PDF data of length:", len(pdf_data))

Example saving to file

filepath = get_pdf("https://pypi.org", egress="output.pdf", src_kind="url")
print("PDF saved to:", filepath)

Metadata

Release files for pdfdol 0.1.38

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pdfdol 0.1.38
File Size Uploaded Provenance Actions
pdfdol-0.1.38.tar.gz 69.1 kB Without provenance Details for pdfdol-0.1.38.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for pdfdol 0.1.38
File Interpreter ABI Platform Actions
pdfdol-0.1.38-py3-none-any.whl Python 3 none any Details for pdfdol-0.1.38-py3-none-any.whl

Total release size: 144.5 kB

Release files / pdfdol-0.1.38.tar.gz

Download URL pdfdol-0.1.38.tar.gz
Size 69.1 kB
Tags Source
SHA-256 checksum
How to use checksums
aaf3716e6c0812bcf3ee22158c3011264858b42b7e196e6e37e0b70b8bfe0f29
BLAKE2b-256 checksum
How to use checksums
73e3d021b27c43ecfa61339dfa0f353a51a5a15d61afde50bbf6dbdd80e4b4a6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.13.9 {"installer":{"name":"uv","version":"0.13.9","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / pdfdol-0.1.38-py3-none-any.whl

Download URL pdfdol-0.1.38-py3-none-any.whl
Size 75.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
2c75ef3a824cc377bcdc63186d9b241bf7070836af8d98cd8d6cf18f0cd76c01
BLAKE2b-256 checksum
How to use checksums
59bd09b5142fc1a4fbc0263babb24ab305132527688765710cee53b29aeecf96
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.13.9 {"installer":{"name":"uv","version":"0.13.9","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Security for pdfdol 0.1.38

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.38 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page