Skip to main content

Guthman's Document Parsing Utilities

A collection of utilities for document content extraction and conversion, including:

  • PDF document processing and content extraction
  • EPUB to HTML/TXT/PDF conversion
  • Support for AI-assisted document content extraction
  • Hierarchical document structure extraction with page ranges

🔒 Security

This repository uses Gitleaks to prevent accidentally committing secrets. See SECURITY.md for our security policy and GITLEAKS_SETUP.md for setup instructions.

Before contributing: Install Gitleaks to scan for secrets automatically. See INSTALLATION_INSTRUCTIONS.md for details.

Installation

Requires Python 3.11+.

# Core features (PDF/EPUB processing with native extraction) - no Google/AI dependencies
pip install git+https://github.com/Guthman/doc-parse-convert.git

# With AI-assisted extraction (Gemini via the Gemini API or Vertex AI)
pip install "doc_parse_convert[ai] @ git+https://github.com/Guthman/doc-parse-convert.git"

# For development installation (from local clone), with all optional features
pip install -e ".[all]"

Optional extras

Extra Installs Needed for
ai google-genai, google-auth, pydantic, tenacity ExtractionStrategy.AI, AIClient, ImageContentExtractor
gcs google-auth Uploading to Google Cloud Storage (HTML to Markdown pipeline)
all everything above

Using an AI feature without the ai extra raises an ImportError explaining what to install.

System Dependencies

In addition to Python dependencies, this library requires the following external tools for certain functionality:

Required for EPUB/PDF/HTML Conversion

  • Pandoc: Used for EPUB to PDF conversion

    • Windows: Install from pandoc.org/installing.html or using choco install pandoc
    • macOS: Install using Homebrew: brew install pandoc
    • Linux: Install using package manager: apt-get install pandoc or yum install pandoc
  • wkhtmltopdf: Used for HTML to PDF conversion

    • Windows: Install from wkhtmltopdf.org/downloads.html or using choco install wkhtmltopdf
    • macOS: Install using Homebrew: brew install wkhtmltopdf
    • Linux: Install using package manager: apt-get install wkhtmltopdf or yum install wkhtmltopdf

Feature Dependency Matrix

Feature Required System Dependencies
PDF content extraction (native) None
PDF content extraction (AI) None (requires the ai extra and Gemini credentials)
EPUB to HTML conversion None
EPUB to TXT conversion None
EPUB to PDF conversion Pandoc
HTML to PDF conversion wkhtmltopdf
HTML to Markdown conversion None (requires the gcs extra, a GCS bucket and Jina API credentials)

Configuration

The utilities require various configuration values and credentials. These can be provided in several ways:

  1. Environment Variables: Create a .env file in your project root with the following variables:

    JINA_API_KEY=your_jina_api_key
    GCP_SERVICE_ACCOUNT=your_service_account_json
    AI_DEBUG_DIR=path/to/debug/directory  # For saving debug information when AI extraction fails
    
  2. Processing Configuration: When using the document processors, provide a ProcessingConfig object with your settings:

    from doc_parse_convert import ProcessingConfig, ExtractionStrategy
    
    # Vertex AI (Google Cloud project)
    config = ProcessingConfig(
        project_id="your-project-id",
        vertex_ai_location="your-location",
        gemini_model_name="gemini-2.5-flash",
        use_application_default_credentials=True,
        toc_extraction_strategy=ExtractionStrategy.NATIVE,
        content_extraction_strategy=ExtractionStrategy.AI
    )
    
    # Or the Gemini Developer API (API key, no Google Cloud project needed)
    config = ProcessingConfig(
        gemini_api_key="your-gemini-api-key",
        gemini_model_name="gemini-2.5-flash",
        content_extraction_strategy=ExtractionStrategy.AI
    )
    
  3. Required Tools:

    • Pandoc: For EPUB to PDF conversion
    • wkhtmltopdf: For HTML to PDF conversion Make sure these tools are installed and available in your system PATH.

Debugging AI Extraction

If you encounter issues with AI extraction, you can enable debugging by setting the AI_DEBUG_DIR environment variable:

# On Windows
$env:AI_DEBUG_DIR = "C:\path\to\debug\directory"

# On Linux/Mac
export AI_DEBUG_DIR=/path/to/debug/directory

When set, the library will save:

  • Timestamped debug directories for each error
  • Problematic images that caused API errors
  • Error details and request information
  • Complete API error diagnostics

This helps troubleshoot issues with the Gemini API, particularly "INVALID_ARGUMENT" (HTTP 400) errors related to image sizes or content.

Usage

PDF Content Extraction

from doc_parse_convert import ProcessingConfig, ExtractionStrategy, PDFProcessor

# Configure the processor
config = ProcessingConfig(
    toc_extraction_strategy=ExtractionStrategy.NATIVE,
    content_extraction_strategy=ExtractionStrategy.NATIVE
)

# Process a PDF file
processor = PDFProcessor(config)
processor.load("document.pdf")

# Extract table of contents
chapters = processor.get_table_of_contents()
for chapter in chapters:
    print(f"{chapter.title} (pages {chapter.start_page+1}-{chapter.end_page+1})")

# Extract content from a specific chapter
chapter_content = processor.extract_chapter_text(chapters[0])

# Don't forget to close the processor when finished
processor.close()

EPUB Conversion

from doc_parse_convert import convert_epub_to_html, convert_epub_to_txt, convert_epub_to_pdf

# Convert EPUB to HTML
html_content = convert_epub_to_html("book.epub")

# Convert EPUB to TXT
text_content = convert_epub_to_txt("book.epub")

# Convert EPUB to PDF
pdf_path = convert_epub_to_pdf("book.epub", output_folder="output_folder")

Converting PDF Pages to Images

from doc_parse_convert.utils.image import ImageConverter

# Using context manager (recommended)
with ImageConverter('document.pdf', format='png') as converter:
    for page_number, page_data in converter:
        with open(f'document_page_{page_number+1}.png', 'wb') as f:
            f.write(page_data)

# Alternative approach
converter = ImageConverter('document.pdf', format='jpg')
try:
    for page_number, page_data in converter:
        with open(f'document_page_{page_number+1}.jpg', 'wb') as f:
            f.write(page_data)
finally:
    converter.close()

Document Structure Extraction

from doc_parse_convert import ProcessingConfig, PDFProcessor, DocumentStructureExtractor

# Configure the processor
config = ProcessingConfig(
    toc_extraction_strategy=ExtractionStrategy.NATIVE
)

# Process a PDF file
processor = PDFProcessor(config)
processor.load("document.pdf")

# Extract hierarchical document structure with page ranges
structure_extractor = DocumentStructureExtractor(processor)
document_structure = structure_extractor.extract_structure()

# Export structure in different formats
json_structure = structure_extractor.export_structure("json")
xml_structure = structure_extractor.export_structure("xml")

# Extract text by sections
section_texts = structure_extractor.extract_text_by_section("output_folder")

Using the Processor Factory

from doc_parse_convert import ProcessingConfig, ProcessorFactory

# Configure processing options
config = ProcessingConfig()

# Automatically create the appropriate processor based on file type
processor = ProcessorFactory.create_processor("document.pdf", config)

# Use the processor
chapters = processor.get_table_of_contents()

# Always close the processor when done
processor.close()

Examples

See the examples/ directory for detailed usage examples:

  • usage_example.ipynb: Jupyter notebook with example code and configuration
  • image_converter_example.py: Example of converting PDF pages to PNG and JPG images

Metadata

Release files for doc-parse-convert 0.8.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 doc-parse-convert 0.8.0
File Size Uploaded Provenance Actions
doc_parse_convert-0.8.0.tar.gz 57.1 kB With provenance Details for doc_parse_convert-0.8.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for doc-parse-convert 0.8.0
File Interpreter ABI Platform Actions
doc_parse_convert-0.8.0-py3-none-any.whl Python 3 none any Details for doc_parse_convert-0.8.0-py3-none-any.whl

Total release size: 103.6 kB

Release files / doc_parse_convert-0.8.0.tar.gz

Download URL doc_parse_convert-0.8.0.tar.gz
Size 57.1 kB
Tags Source
SHA-256 checksum
How to use checksums
69d122082e2f347a03345cf18187b58a066f10222582259ec4f830088eff570c
BLAKE2b-256 checksum
How to use checksums
10a6f38c4b3971e6585f5b9046012b3902fb160f84467620f54c38db02099599
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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 Nov 30, 2026.

Transparency log

Release files / doc_parse_convert-0.8.0-py3-none-any.whl

Download URL doc_parse_convert-0.8.0-py3-none-any.whl
Size 46.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
3c965ea2e850edafed7933eff26c446e41831dd89bbe49739ea27307270a6d51
BLAKE2b-256 checksum
How to use checksums
7c106ed37e6b49e39e4cca007b3c1fe54609ea826930ed162ca03617272b4738
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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 Nov 30, 2026.

Transparency log

Security for doc-parse-convert 0.8.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.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for doc_parse_convert-0.8.0.tar.gz

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 Nov 30, 2026.

Transparency log

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