Skip to main content

PDF2Markdown4LLM

PDF2Markdown4LLM is a Python library that converts PDF documents to Markdown format, specifically optimized for Large Language Models (LLMs). It intelligently preserves document structure, identifies headers based on font sizes, and handles tables while maintaining the original document flow.

Demo

demo

Features

  • Intelligent header detection based on font size analysis
  • Table extraction and conversion to Markdown format
  • Maintains document structure and flow
  • Handles nested content and complex layouts
  • Font size classification for consistent header levels
  • Clean table formatting with proper alignment
  • Optional header removal functionality
  • Robust error handling and validation

Installation

pip install pdf2markdown4llm

Dependencies

  • pdfplumber
  • dataclasses (Python 3.7+)
  • typing
  • collections
  • re

Usage

Basic usage example:

from pdf2markdown4llm import PDF2Markdown4LLM

def progress_callback(progress): 
    """Callback function to handle progress"""
    print(f"Phase: {progress.phase.value}, Page {progress.current_page}/{progress.total_pages}, Progress: {progress.percentage:.1f}%, Message: {progress.message}")


# Initialize converter
converter = PDF2Markdown4LLM(remove_headers=False, skip_empty_tables=True, table_header="### Table", progress_callback=progress_callback)


# Convert PDF to Markdown
markdown_content = converter.convert("input.pdf")

# Save to file
with open("output.md", "w", encoding="utf-8") as md_file:
    md_file.write(markdown_content)

Configuration Options

  • remove_headers: Boolean flag to remove existing markdown headers from text (default: False)
  • table_header: String to specify the header level for tables (default: "###")

Key Components

FontSizeClassifier

Analyzes font sizes throughout the document to determine appropriate header levels:

  • Automatically identifies the normal text size
  • Classifies larger fonts into appropriate header levels
  • Handles font size variations and inconsistencies

PDFContentExtractor

Extracts content while preserving structure:

  • Processes text and tables separately
  • Maintains original document flow
  • Validates table boundaries
  • Handles nested content

MarkdownConverter

Converts extracted content to clean Markdown:

  • Proper table formatting
  • Header level preservation
  • Clean text formatting

Error Handling

The library includes comprehensive error handling:

  • Validates table boundaries
  • Checks for invalid content
  • Provides detailed error messages
  • Includes stack traces for debugging

Contributing

Contributions are welcome! Please feel free to submit pull requests.

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/new-feature)
  3. Commit your changes (git commit -am 'Add new feature')
  4. Push to the branch (git push origin feature/new-feature)
  5. Create a Pull Request

License

This project is licensed under the MIT License - see the LICENSE file for details.

Author

[HawkClaws]

Acknowledgements

  • PDFPlumber team for the excellent PDF parsing library
  • Contributors and maintainers

Metadata

Release files for pdf2markdown4llm 0.1.9

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

Source distribution (sdist)

Source distribution for pdf2markdown4llm 0.1.9
File Size Uploaded Provenance
pdf2markdown4llm-0.1.9.tar.gz 11.2 kB Without provenance Details

Built distribution (wheel)

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

Total release size: 21.8 kB

Release files / pdf2markdown4llm-0.1.9.tar.gz

Download URL pdf2markdown4llm-0.1.9.tar.gz
Size 11.2 kB
Tags Source
SHA-256 checksum
How to use checksums
62adaa666df3bd02c9282ddb93d1c0df4fb3a1d4cf9ebfcd9b26e211f0d99da7
BLAKE2b-256 checksum
How to use checksums
1c6384ae2cdfc897ffcda354ec9e765dbb3c1f1b7b4c909784b245ff96b86d0a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.5

Release files / pdf2markdown4llm-0.1.9-py3-none-any.whl

Download URL pdf2markdown4llm-0.1.9-py3-none-any.whl
Size 10.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b189170a60ce440e2a9f55da39af14642d7c66c08d121db806453dbb556c01a2
BLAKE2b-256 checksum
How to use checksums
3e7ed2b3e3779dfbdede90480bf3f6187989400fa849e8d0ffaf8adf16676747
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.5

Security for pdf2markdown4llm 0.1.9

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.

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