Skip to main content

pdf-diagram-extract

PyPI version Python 3.10+ License: MIT

Extract diagram and graph entities from PDFs as structured JSON — no LLM, pure code.

📖 Documentation

Features

  • Vector extraction — parses native PDF drawing primitives (rectangles, lines, curves) via PyMuPDF
  • Raster fallback — renders scanned/image-based pages and uses OpenCV contour detection
  • Shape classification — identifies rectangles, circles, diamonds, ellipses, polygons
  • Native text extraction — precise assignment of text to shapes using Intersection and Gravity physics
  • Virtual text nodes — detects floating text and dynamically generates text_block shapes so nothing is lost
  • Edge detection — detects lines, arrows, and curves with directionality
  • Relationship resolution — links edges to shapes via spatial proximity
  • Chart detection — heuristically identifies bar, pie, line, and scatter charts
  • JSON output — returns a fully structured, serializable result

Installation

pip install pdf-diagram-extract

Or install from source:

git clone https://github.com/GT0096/pdf-diagram-extract.git
cd pdf-diagram-extract
pip install -e .

Quick Start

import diagramextract
import json

result = diagramextract.extract("path/to/document.pdf")

# Get JSON output
data = result.to_dict()
print(json.dumps(data, indent=2))

# Access specific pages
for page in result.pages:
    print(f"Page {page.page_number}: {len(page.diagrams)} diagram(s)")
    for diagram in page.diagrams:
        print(f"  Type: {diagram.diagram_type}")
        print(f"  Shapes: {len(diagram.shapes)}")
        print(f"  Edges: {len(diagram.edges)}")
        print(f"  Charts: {len(diagram.charts)}")

API Reference

diagramextract.extract(pdf_path, *, pages=None, vector_threshold=5, dpi=200)

Extract entities from all diagrams/graphs in a PDF.

Parameters:

  • pdf_path (str): Path to the PDF file
  • pages (list[int] | None): Specific page numbers to process (0-indexed). None = all pages.
  • vector_threshold (int): Minimum vector paths on a page to prefer vector extraction over raster fallback. Default: 5.
  • dpi (int): Resolution for raster rendering when falling back to image-based extraction. Default: 200.

Returns: ExtractionResult with .to_dict() for JSON serialization.

Output Structure

{
  "source_file": "document.pdf",
  "total_pages": 3,
  "pages": [
    {
      "page_number": 0,
      "width": 612.0,
      "height": 792.0,
      "diagrams": [
        {
          "diagram_type": "flowchart",
          "shapes": [...],
          "edges": [...],
          "charts": [...]
        }
      ]
    }
  ]
}

Dependencies

Package Purpose
PyMuPDF PDF parsing, vector paths, page rendering
opencv-python-headless Contour detection, shape classification
numpy Numerical operations

License

MIT

Release files for pdf-diagram-extract 1.0.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 pdf-diagram-extract 1.0.0
File Size Uploaded
pdf_diagram_extract-1.0.0.tar.gz 698.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pdf-diagram-extract 1.0.0
File Interpreter ABI Platform
pdf_diagram_extract-1.0.0-py3-none-any.whl Python 3 none any Details

Total release size: 728.4 kB

Release files / pdf_diagram_extract-1.0.0.tar.gz

Download URL pdf_diagram_extract-1.0.0.tar.gz
Size 698.0 kB
Tags Source
SHA-256 checksum
How to use checksums
645b5c04f3a25941cad24fee0bb76f3e16cf8abbac2e9efddf3d191087b4141a
BLAKE2b-256 checksum
How to use checksums
236713ee744af66ee0222a5de67147110bbbc03ccb3c74ac7324edd0e7407647
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Aug 25, 2026.

Transparency log

Release files / pdf_diagram_extract-1.0.0-py3-none-any.whl

Download URL pdf_diagram_extract-1.0.0-py3-none-any.whl
Size 30.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c4521d22ea02c6ddb43680222b2bbb2338aed1cfbbb436f77e11e69a4159c234
BLAKE2b-256 checksum
How to use checksums
4974d565e2b1d97c8bbfb841b8266b0acbcb10ecf5970f0e0fe85490b4a466f9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Aug 25, 2026.

Transparency log

Release history Release notifications | RSS feed

This release

1.0.0 This release

2 release files

0.2.0

2 release files

0.1.0

2 release files

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