Skip to main content

PDFSchemaParsing

A resilient, schema-driven Python parser for PDF and Markdown documents. Given a user-defined schema of sections (and optional nested subsections), pdfschemaparsing segments documents into structured JSON containing canonical section titles and cleaned inner texts.

Key Features

  • Resilient to Whitespace & Unicode Quirks: Normalizes compatibility characters (NFKC), ligatures (fi -> fi), non-breaking spaces (\u00a0), zero-width spaces (\u200b), soft hyphens, and erratic line/tab spacing.
  • Resilient to Casing, Numbering & Punctuation: Automatically handles UPPERCASE, Title Case, lowercase, Markdown adornments (#, **, __, Setext ===/---), leading numbering (1.2., II., (A), Section 1:), and & vs and.
  • Resilient to Layout Shifts:
    • Y-Tolerance Line Reconstruction (PDF): Groups horizontally scattered words into visual lines even when vertical coordinates jitter ($\pm 3\text{ pt}$).
    • Multi-Line Wrapped Headings: Detects headings wrapped across two lines.
    • Inline Headings: Splits bold/delimited inline headers (**Abstract:** This study...) into the matched section and its body text.
    • Running Header & Footer Removal (PDF): Strips repeated page headers/footers and standalone page numbers across multi-page PDFs.
    • Footnote Filtering & Extraction: Automatically excludes Markdown and bottom-of-page PDF footnotes (and inline [^id] references) from main section inner texts, and provides parser.extract_footnotes(...) / extract_footnotes(...) to retrieve them separately.
    • Consecutive Repeating Sections: Supports consecutive headings ("Chapter 1", "Chapter 2", "Chapter III - Applications", etc.) sharing the same subsections via placeholder syntax ("Chapter {n}" / "Chapter *") or repeatable=True.
    • Direct PDF-to-Markdown Conversion: Converts PDFs directly into clean Markdown via pdf_to_markdown(...) (without a schema) or parser.to_markdown(...) (with a schema).
    • Fuzzy Matching: Tolerates minor OCR artifacts or typos in headings via configurable similarity thresholds.

Examples

1. Basic Markdown Parsing (Noisy Casing, Numbering & Whitespace)

You can pass a schema as a list of titles, a shorthand {"Canonical Title": ["Alias 1", "Alias 2"]} dictionary, or a full configuration dictionary.

from pdfschemaparsing import parse_markdown

markdown_text = """
I.   EXECUTIVE\u00a0\u00a0SUMMARY
================================
Our Q3 deployment reduced latency by 35%   across all edge nodes.

### Section 2: Materials & Methods:
We benchmarked   1,200 requests per second using synthetic workloads.

**3) Key   Findings**
Throughput remained stable under peak load.

---
**Conclusion:** The new architecture is ready for production.
"""

# Shorthand schema: {"Canonical Title": ["Alias 1", "Alias 2"]}
schema = {
    "Executive Summary": ["Overview", "Summary"],
    "Methodology": ["Materials and Methods", "Methods"],
    "Results": ["Key Findings", "Experimental Results"],
    "Conclusion": ["Concluding Remarks"],
}

# 1a. Flat dictionary mapping {title: inner_text} (as JSON string)
flat_json = parse_markdown(markdown_text, schema, as_flat_dict=True, as_json=True)
print(flat_json)

Output (as_flat_dict=True):

{
  "Executive Summary": "Our Q3 deployment reduced latency by 35% across all edge nodes.",
  "Methodology": "We benchmarked 1,200 requests per second using synthetic workloads.",
  "Results": "Throughput remained stable under peak load.",
  "Conclusion": "The new architecture is ready for production."
}

If you omit as_flat_dict=True (the default), you get the structured list format with matched header metadata:

structured_json = parse_markdown(markdown_text, schema, as_json=True)
print(structured_json)

Output (default structured format):

{
  "sections": [
    {
      "title": "Executive Summary",
      "original_heading": "I.   EXECUTIVE\u00a0\u00a0SUMMARY",
      "content": "Our Q3 deployment reduced latency by 35% across all edge nodes.",
      "matched_header": "I. EXECUTIVE SUMMARY",
      "subsections": []
    },
    {
      "title": "Methodology",
      "original_heading": "### Section 2: Materials & Methods:",
      "content": "We benchmarked 1,200 requests per second using synthetic workloads.",
      "matched_header": "### Section 2: Materials & Methods:",
      "subsections": []
    },
    {
      "title": "Results",
      "original_heading": "**3) Key   Findings**",
      "content": "Throughput remained stable under peak load.",
      "matched_header": "**3) Key Findings**",
      "subsections": []
    },
    {
      "title": "Conclusion",
      "original_heading": "Conclusion",
      "content": "The new architecture is ready for production.",
      "matched_header": "Conclusion",
      "subsections": []
    }
  ]
}

2. Hierarchical Subsections, Wrapped Headers & Fuzzy Typo Matching

When documents have nested sections, minor OCR typos in headings (e.g., "Methodolgy"), or headings wrapped across two lines, configure DocumentSchema (or a dict) with subsections and fuzzy_threshold:

from pdfschemaparsing import SchemaParser

doc = """
Document Preamble: Internal Technical Report v2.4

# 1. Abstrct
This study evaluates layout-resilient document parsing.

# 2. System Architecture
and Implementation
High-level overview of the ingestion pipeline.

## 2.1 PDF Line   Reconstruction
Words are clustered vertically within y_tolerance.

## 2.2 Fuzzy   Header Matching
Handles minor OCR errors in section titles.

# 3. Final   Remarks
All benchmarks passed.
"""

schema = {
    "capture_preamble": True,
    "fuzzy_threshold": 0.82,  # Tolerates "Abstrct" -> "Abstract"
    "sections": [
        {"title": "Abstract", "required": True},
        {
            "title": "System Architecture and Implementation",
            "subsections": [
                {"title": "PDF Line Reconstruction"},
                {"title": "Fuzzy Header Matching"},
            ],
        },
        {"title": "Conclusion", "aliases": ["Final Remarks"]},
    ],
}

parser = SchemaParser(schema)
print(parser.parse_markdown(doc, as_flat_dict=True, as_json=True))

Output:

{
  "_preamble": "Document Preamble: Internal Technical Report v2.4",
  "Abstract": "This study evaluates layout-resilient document parsing.",
  "System Architecture and Implementation": {
    "content": "High-level overview of the ingestion pipeline.",
    "subsections": {
      "PDF Line Reconstruction": "Words are clustered vertically within y_tolerance.",
      "Fuzzy Header Matching": "Handles minor OCR errors in section titles."
    }
  },
  "Conclusion": "All benchmarks passed."
}

3. Parsing a PDF File (Resilient to Vertical Jitter & Running Headers/Footers)

parse_pdf (or SchemaParser.parse) accepts a file path (str or Path), raw bytes, or a file-like binary stream (BytesIO). It automatically clusters words with vertical jitter (y_tolerance), strips repeated running headers/footers across pages, and rejoins hyphenated line wraps ("expec-\ntations" → "expectations").

from pathlib import Path
from pdfschemaparsing import SchemaParser, parse_pdf

schema = {
    "y_tolerance": 3.5,            # Group words within +/- 3.5pt vertical jitter into the same line
    "strip_running_headers": True, # Strip repeated top/bottom headers & page numbers ("Page 1 of 5")
    "include_missing": True,       # Emit null for schema sections absent from the PDF
    "sections": [
        {"title": "Executive Summary", "aliases": ["Summary"]},
        {"title": "Financial Performance", "aliases": ["Financials", "Revenue & Earnings"]},
        {"title": "Risk Factors", "aliases": ["Risks"]},
        {"title": "Appendix"},     # Will be null if not present in the PDF
    ],
}

# Option A: Direct convenience function
result_dict = parse_pdf(Path("annual_report.pdf"), schema, as_flat_dict=True)

# Option B: Reusing a SchemaParser instance across multiple PDFs or Markdown files
parser = SchemaParser(schema)
json_output = parser.parse("annual_report.pdf", as_json=True)
print(json_output)

4. Custom Regex Patterns for Specialized Headings

If a section title follows a dynamic pattern (such as "Item 1A. Risk Factors" in SEC 10-K filings or numbered clinical trial endpoints), you can supply a pattern regex on any SectionSpec:

from pdfschemaparsing import DocumentSchema, SectionSpec, SchemaParser

schema = DocumentSchema(
    ordered=True,
    sections=[
        SectionSpec(
            title="Business Overview",
            pattern=r"^item\s+1[\.\:\s]+business",
            required=True,
        ),
        SectionSpec(
            title="Risk Factors",
            pattern=r"^item\s+1a[\.\:\s]+risk\s+factors",
            required=True,
        ),
    ],
)

parser = SchemaParser(schema)
filing_text = """
ITEM 1. BUSINESS
We design and manufacture modular energy storage systems.

ITEM 1A. RISK FACTORS
Fluctuations in lithium carbonate supply could impact margins.
"""

print(parser.parse(filing_text, as_flat_dict=True, as_json=True))

Output:

{
  "Business Overview": "We design and manufacture modular energy storage systems.",
  "Risk Factors": "Fluctuations in lithium carbonate supply could impact margins."
}

5. Ignoring Footnotes in Main Text & Extracting Footnotes Separately

Footnotes in both Markdown ([^1]: ..., ¹ ..., [1] ...) and PDF documents (bottom-of-page smaller-font footnotes) are automatically excluded from the returned section inner texts. You can retrieve all footnotes using parser.extract_footnotes(...) or the standalone extract_footnotes(...) function:

from pdfschemaparsing import SchemaParser, extract_footnotes

md_with_footnotes = """
# 1. Executive Summary
Revenue increased by 24% year-over-year[^1] driven by enterprise adoption.

[^1]: Audited figures exclude Q4 divestitures.
    Additional accounting details are available in Appendix B.

# 2. Methodology
We evaluated 10,000 samples across three regions[^note-b].
² Regional weights were calibrated using 2025 census data.

---
[^note-b]: Europe, North America, and APAC.
"""

parser = SchemaParser(["Executive Summary", "Methodology"])

# 1. Main section text automatically ignores footnotes and strips inline [^...] markers
print(parser.parse_markdown(md_with_footnotes, as_flat_dict=True, as_json=True))

# 2. Extract footnotes via SchemaParser method or standalone extract_footnotes()
print(parser.extract_footnotes(md_with_footnotes, as_flat_dict=True, as_json=True))
# Or as a structured list with page numbers:
# print(extract_footnotes("annual_report.pdf", as_json=True))

Main Text Output (footnotes excluded):

{
  "Executive Summary": "Revenue increased by 24% year-over-year driven by enterprise adoption.",
  "Methodology": "We evaluated 10,000 samples across three regions."
}

Extracted Footnotes Output (parser.extract_footnotes(..., as_flat_dict=True)):

{
  "1": "Audited figures exclude Q4 divestitures.\nAdditional accounting details are available in Appendix B.",
  "2": "Regional weights were calibrated using 2025 census data.",
  "note-b": "Europe, North America, and APAC."
}

6. Consecutive Repeating Headings Sharing the Same Subsections (Chapter 1, Chapter 2, ...)

For consecutive sections like "Chapter 1", "Chapter 2: Advanced Layout Recovery", "Chapter III - Applications" that all share the same internal subsection template, specify "title": "Chapter {n}" (or "Chapter *", or set "repeatable": True on "title": "Chapter"):

from pdfschemaparsing import parse_markdown

book_md = """
# Preface
Welcome to the textbook.

# Chapter 1
Introductory text for Chapter 1.

## 1.1 Motivation
Why we study resilient parsing.

## 1.2 Summary
Key takeaways of Chapter 1.

# Chapter 2: Advanced Layout Recovery
Introductory text for Chapter 2.

## 2.1 Motivation
Handling PDF coordinate jitter.

## 2.2 Summary
Key takeaways of Chapter 2.

# Chapter III - Applications
Introductory text for Chapter 3.

## 3.1 Motivation
Real-world deployment scenarios.

## 3.2 Summary
Key takeaways of Chapter 3.
"""

schema = {
    "ordered": True,
    "sections": [
        {"title": "Preface"},
        {
            "title": "Chapter {n}",  # Or {"title": "Chapter", "repeatable": True}
            "subsections": [
                {"title": "Motivation", "aliases": ["Background"]},
                {"title": "Summary", "aliases": ["Takeaways"]},
            ],
        },
    ],
}

print(parse_markdown(book_md, schema, as_flat_dict=True, as_json=True))

Output:

{
  "Preface": "Welcome to the textbook.",
  "Chapter 1": {
    "content": "Introductory text for Chapter 1.",
    "subsections": {
      "Motivation": "Why we study resilient parsing.",
      "Summary": "Key takeaways of Chapter 1."
    }
  },
  "Chapter 2: Advanced Layout Recovery": {
    "content": "Introductory text for Chapter 2.",
    "subsections": {
      "Motivation": "Handling PDF coordinate jitter.",
      "Summary": "Key takeaways of Chapter 2."
    }
  },
  "Chapter III - Applications": {
    "content": "Introductory text for Chapter 3.",
    "subsections": {
      "Motivation": "Real-world deployment scenarios.",
      "Summary": "Key takeaways of Chapter 3."
    }
  }
}

7. Converting a PDF Document to Markdown Text

You can convert a PDF to clean Markdown in two ways:

  1. Without a schema (pdf_to_markdown): Reconstructs jittered lines using y_tolerance, strips running headers/footers and page numbers, de-hyphenates wrapped words, excludes footnotes (or appends them at the bottom with include_footnotes_at_end=True), and infers # / ## / ### Markdown headings from PDF font sizes and bolding.
  2. With a schema (parser.to_markdown): Segments the PDF according to your schema and renders the canonical section hierarchy as # Section / ## Subsection Markdown.
from pdfschemaparsing import SchemaParser, pdf_to_markdown

# Option A: Direct schema-free PDF -> Markdown conversion
md_text = pdf_to_markdown(
    "annual_report.pdf",
    y_tolerance=3.5,
    strip_running_headers=True,
    ignore_footnotes=True,
    include_footnotes_at_end=False,  # Set True to append [^id]: ... at the bottom
)
print(md_text)

# Option B: Schema-normalized PDF -> Markdown conversion
parser = SchemaParser(
    {
        "sections": [
            {"title": "Executive Summary", "aliases": ["Summary"]},
            {"title": "System Architecture and Design"},
            {"title": "Conclusion", "aliases": ["Concluding Remarks"]},
        ]
    }
)
clean_schema_md = parser.to_markdown("annual_report.pdf", include_footnotes=False)
print(clean_schema_md)

Running Tests

In the textparse conda environment:

conda run -n textparse pytest -v

License

This project is licensed under the MIT License.

Optional PDF Backends

  • pypdf (pip install ".[pdf]"): Licensed under the permissive BSD-3-Clause license. This is the default backend when backend="auto".
  • pymupdf (pip install ".[pymupdf]"): Licensed under AGPL-3.0 (or a commercial license from Artifex). It is strictly optional and only used if backend="pymupdf" is specified or if pypdf is unavailable in backend="auto" mode. Users who opt into pymupdf are responsible for complying with its AGPL-3.0 or commercial licensing terms.

Metadata

Release files for pdf-schema-parsing 0.0.1

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-schema-parsing 0.0.1
File Size Uploaded Provenance Actions
pdf_schema_parsing-0.0.1.tar.gz 40.1 kB Without provenance Details for pdf_schema_parsing-0.0.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for pdf-schema-parsing 0.0.1
File Interpreter ABI Platform Actions
pdf_schema_parsing-0.0.1-py3-none-any.whl Python 3 none any Details for pdf_schema_parsing-0.0.1-py3-none-any.whl

Total release size: 77.4 kB

Release files / pdf_schema_parsing-0.0.1.tar.gz

Download URL pdf_schema_parsing-0.0.1.tar.gz
Size 40.1 kB
Tags Source
SHA-256 checksum
How to use checksums
ba70afca67b167699e9cac41f0da05bf33033e192e7a392908f2443e8df84af1
BLAKE2b-256 checksum
How to use checksums
29098ab5ab21d1213e6dc9fa4f634d5fef6b0657ad1e75211408240172ab79e6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.16

Release files / pdf_schema_parsing-0.0.1-py3-none-any.whl

Download URL pdf_schema_parsing-0.0.1-py3-none-any.whl
Size 37.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
2237d5cfa9a24eb6b4f03f08e4feb37625e9fd84a31122f9cb24810823397b4c
BLAKE2b-256 checksum
How to use checksums
904b474f7924547beb020f368d82f71d22931be1df04fc6a34eedb1343762375
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.16

Security for pdf-schema-parsing 0.0.1

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.0.1 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