Skip to main content

Fast, lightweight PDF structure parser to hierarchical JSON & Markdown section trees.

Project description

pdf-section-tree 🌳

PyPI version Python License: MIT

Fast, lightweight Python library that turns a PDF into a hierarchical Markdown or JSON section tree, using typographic heuristics instead of heavy ML models.

Most PDF tools fall into two camps: low-level extractors (pypdf, pdfplumber, PyMuPDF) that hand you raw characters and (x, y) coordinates with no document structure, or full layout-analysis frameworks (unstructured, layoutparser, marker) that rebuild structure but drag in gigabytes of PyTorch/OCR dependencies and a GPU. pdf-section-tree sits in between: it reconstructs the heading hierarchy from font size, weight and layout signals alone, in milliseconds, with a single lightweight dependency.

Installation

pip install pdf-section-tree

Quickstart

from pdf_section_tree import parse_pdf

# Parse a PDF into a hierarchical tree
doc = parse_pdf("sample.pdf")

# Export to Markdown
print(doc.to_markdown())

# Export to a nested dict (e.g. for RAG chunking)
data = doc.to_dict()

What it does

  • Heading hierarchy from typography — font size and boldness are used to infer H1..H6 nesting, with heuristics to merge split titles ("1" + "Introduction""1 Introduction") and to avoid promoting numeric table cells or one-off oversized paragraphs to fake headings.
  • Boilerplate removal — running headers/footers and page numbers, detected by page-margin position and repetition across pages, are stripped from the output.
  • Figure & diagram detection — embedded images and dense native vector diagrams (charts, heatmaps) large enough to be real figures are replaced with a *[Figure]* marker; their caption is kept, the text baked into them is dropped, and decorative page-margin images (logos, banners) are silently ignored.
  • Out-of-context text filtering — text much smaller than the document's body size (typically labels or captions rendered as part of an inserted image/screenshot) is dropped so only the main content remains.
  • Ligature repair — broken ligature glyphs from subsetted fonts (Dol�Dolfi) are decoded where the font data allows it.

Known limitations

  • Tables are extracted as flattened text, not reconstructed into rows/columns.
  • Sparse vector diagrams (few drawn elements) may fall under the density threshold and not be recognized as a figure.
  • Heuristics are tuned on typical reports/books/papers; unusual layouts (dense multi-column magazines, forms) aren't specifically optimized for.

License

MIT

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

pdf_section_tree-0.1.0.tar.gz (10.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pdf_section_tree-0.1.0-py3-none-any.whl (11.3 kB view details)

Uploaded Python 3

File details

Details for the file pdf_section_tree-0.1.0.tar.gz.

File metadata

  • Download URL: pdf_section_tree-0.1.0.tar.gz
  • Upload date:
  • Size: 10.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.1

File hashes

Hashes for pdf_section_tree-0.1.0.tar.gz
Algorithm Hash digest
SHA256 d52786ebff52ee245cde13ee3ab210b187e8ce0f308fffa3d9d4b57246183262
MD5 30e912198ddc5f3eafaeaadd04c01eba
BLAKE2b-256 6e6d4dcd6cccbff6b19c249e069148d195333d1a62114a5aa7e09c210706836b

See more details on using hashes here.

File details

Details for the file pdf_section_tree-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for pdf_section_tree-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 6d575607441a05ac8960bcf4bae5048ee1e6abf3e49db9ace85dbb96ba34a18d
MD5 c8e130926c272a43887bd970f473c527
BLAKE2b-256 f18d1602a4a5ae893d88230feb7f1fe1b05dae60e28560855ccc414e78b433d8

See more details on using hashes here.

Supported by

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