Skip to main content

pd3f-core PyPI PyPI - Python Version PyPI - Downloads

Experimental, use with care.

pd3f-core is a Python package to reconstruct the original continuous text from PDFs with language models. It assumes your PDF is either text-based or already OCRd. pd3f-core is at the heart of pd3f, a Docker-based text extraction pipeline that includes OCR and a web interface.

pd3f-core first uses Parsr to chunk PDFs into lines and paragraphs. Then it uses dehyphen to reconstruct the paragraphs in the most probable way. The probability is derived from the perplexity of character-based Flair language models. Unnecessary hyphens are removed, and spaces or new lines are kept or dropped depending on the surrounding words.

It is mainly developed for German but works with other languages as well.

Documentation

Features

  • Dehyphenation of lines: join two lines by removing a hyphen ('-') if the language model prefers the joined word.
  • Joining lines: decide between a space (' ') and a new line ('\n') when joining lines.
  • Reversing page breaks (experimental): join the last paragraph of a page with the first paragraph of the next page.
  • Footnotes to endnotes (experimental): detect footnotes and move them to the end of the text, so the paragraphs around them can be joined.
  • Deduplication of headers and footers (experimental): headers and footers that repeat on every page are shown once, at the start and the end of the text. Similarity is judged by text (Jaccard distance) and by position on the page.

Installation

Requires Python 3.10 or newer.

pip install pd3f

pd3f-core sends every PDF to a Parsr server. The docker-compose.yml in this repo starts one on localhost:3001, plus the Parsr UI on localhost:8080:

docker compose up

The Parsr images are only published for amd64 and run emulated on ARM machines such as Apple Silicon, which is slow. scripts/remote_parsr.sh starts Parsr on a remote Docker host and tunnels both ports to localhost.

Usage

from pd3f import extract

text, tables = extract(
    "document.pdf",
    tables=False,
    experimental=False,
    force_gpu=False,
    lang="multi",
    fast=False,
    parsr_location="localhost:3001",
)
Parameter Meaning
tables Also extract tables. tables is then a list of CSV strings, ordered by page; otherwise it is None.
experimental Turn on the experimental features listed above.
force_gpu Raise a ValueError if CUDA is not available.
lang Flair language model: de, en, es, fr, multi and others; see dehyphen. Some have a smaller -fast variant, e.g. multi-v0-fast or en-fast.
fast Skip Parsr's drawing and image detection.
parsr_location Host and port of the Parsr server.

parsr_config and parsr_adjust_cleaner_config change the Parsr configuration; see the API documentation. Further keyword arguments go to Export, e.g. remove_hyphens=False.

Models, caches and GPU

The language models are downloaded on first use to ~/.flair, or to $FLAIR_CACHE_ROOT if set. The scores computed by dehyphen are cached in ~/.cache/pd3f/dehyphen, which is trimmed to 100 MB whenever pd3f is imported.

The models run on a CUDA GPU if PyTorch finds one, otherwise on the CPU. On x86-64 Linux, pip install torch installs the CUDA build; to save the download on machines without a GPU, install the CPU build first:

pip install torch --index-url https://download.pytorch.org/whl/cpu
pip install pd3f

Parsr configuration

pd3f-core builds on Parsr's JSON output; its base configuration is pd3f/pd3fConfig.json. See Parsr's module documentation for the individual cleaners.

  • Parsr's list and heading detection are off because their accuracy was too low. The extracted text is therefore flat: no headings, no formatting.
  • Drawing and image detection are on, because they help to decide which paragraph follows which. fast=True turns them off.
  • Table detection runs only if tables=True.
  • pageNumber in Parsr's JSON comes from the page-number detection, i.e. from the headers and footers. It can differ from the index of the page in the pages array; do not rely on it.
  • words-to-line-new has to come directly before reading-order-detection. The other order raises no error but lowers the accuracy.
  • Do not use Parsr's OCR. OCRmyPDF preprocesses the images and gives better results; pd3f uses it.

Development

poetry install
poetry run ruff check .
poetry run pytest

tests/test_pdfs.py compares the output for real PDFs (stored with Git LFS) with reference texts. It needs Parsr on localhost:3001 and downloads the multi-v0-fast models. pytest --ignore tests/test_pdfs.py runs everything else offline.

The API documentation is built with pdoc3 and published to GitHub Pages for every tag, see .github/workflows/publish_docs.yml.

development/ holds the notebooks and notes of the original research; they are not maintained.

License

Affero General Public License 3.0

Metadata

Release files for pd3f 0.5.0

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

Source distribution (sdist)

Source distribution for pd3f 0.5.0
File Size Uploaded Provenance Actions
pd3f-0.5.0.tar.gz 29.7 kB Without provenance Details for pd3f-0.5.0.tar.gz

Built distribution (wheel)

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

Total release size: 61.1 kB

Release files / pd3f-0.5.0.tar.gz

Download URL pd3f-0.5.0.tar.gz
Size 29.7 kB
Tags Source
SHA-256 checksum
How to use checksums
d7af208d52c567e3024f608236f46cffbdfc45434dfc9e607971690ec8fe5606
BLAKE2b-256 checksum
How to use checksums
a01c319c745b03ab27ec6003050d3a5807a048743a585a9b41a93576af71184f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.5.1 CPython/3.14.8 Darwin/24.6.0

Release files / pd3f-0.5.0-py3-none-any.whl

Download URL pd3f-0.5.0-py3-none-any.whl
Size 31.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
1b678b5814c0208be52f73ace1a35e676d37849782681798c8e8effa15953754
BLAKE2b-256 checksum
How to use checksums
132035d022059daf2cdbde17c94ec286500d089ae0a62af34da7e9da5c90748d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.5.1 CPython/3.14.8 Darwin/24.6.0

Security for pd3f 0.5.0

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.

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