Skip to main content

Convert raw documents into AI-understandable context with intelligent text extraction, table detection, and semantic chunking

Project description

xgen-doc2chunk

xgen-doc2chunk is a document processing library that converts raw documents into AI-understandable context. It analyzes, restructures, and normalizes content so that language models can reason over documents with higher accuracy and consistency.

Features

  • Multi-format Support: Process a wide variety of document formats including:

    • PDF (with table detection, OCR fallback, and complex layout handling)
    • Microsoft Office: DOCX, DOC, PPTX, PPT, XLSX, XLS
    • Korean documents: HWP, HWPX (Hangul Word Processor)
    • Text formats: TXT, MD, RTF, CSV, HTML
    • Code files: Python, JavaScript, TypeScript, and 20+ languages
  • Intelligent Text Extraction:

    • Preserves document structure (headings, paragraphs, lists)
    • Extracts tables as HTML with proper rowspan/colspan handling
    • Handles merged cells and complex table layouts
    • Extracts and processes inline images
  • OCR Integration:

    • Pluggable OCR engine architecture
    • Supports OpenAI, Anthropic, Google Gemini, and vLLM backends
    • Automatic OCR fallback for scanned documents or image-based PDFs
  • Smart Chunking:

    • Semantic text chunking with configurable size and overlap
    • Table-aware chunking that preserves table integrity
    • Protected regions for code blocks and special content
  • Metadata Extraction:

    • Extracts document metadata (title, author, creation date, etc.)
    • Formats metadata in a structured, parseable format

Installation

pip install xgen-doc2chunk

Or using uv:

uv add xgen-doc2chunk

Quick Start

Basic Usage

from xgen_doc2chunk import DocumentProcessor

# Create processor instance
processor = DocumentProcessor()

# Extract text from a document
text = processor.extract_text("document.pdf")
print(text)

# Extract text and chunk in one step
result = processor.extract_chunks(
    "document.pdf",
    chunk_size=1000,
    chunk_overlap=200
)

# Access chunks
for i, chunk in enumerate(result.chunks):
    print(f"Chunk {i + 1}: {chunk[:100]}...")

# Save chunks to markdown file
result.save_to_md("output/chunks.md")

With OCR Processing

from xgen_doc2chunk import DocumentProcessor
from xgen_doc2chunk.ocr.ocr_engine.openai_ocr import OpenAIOCREngine

# Initialize OCR engine
ocr_engine = OpenAIOCREngine(api_key="sk-...", model="gpt-4o")

# Create processor with OCR
processor = DocumentProcessor(ocr_engine=ocr_engine)

# Extract text with OCR processing enabled
text = processor.extract_text(
    "scanned_document.pdf",
    ocr_processing=True
)

Supported Formats

Category Extensions
Documents .pdf, .docx, .doc, .pptx, .ppt, .hwp, .hwpx
Spreadsheets .xlsx, .xls, .csv, .tsv
Text .txt, .md, .rtf
Web .html, .htm, .xml
Code .py, .js, .ts, .java, .cpp, .c, .go, .rs, and more
Config .json, .yaml, .yml, .toml, .ini, .env

Architecture

libs/
├── core/
│   ├── document_processor.py    # Main entry point
│   ├── processor/               # Format-specific handlers
│   │   ├── pdf_handler.py       # PDF processing with V4 engine
│   │   ├── docx_handler.py      # DOCX processing
│   │   ├── ppt_handler.py       # PowerPoint processing
│   │   ├── excel_handler.py     # Excel processing
│   │   ├── hwp_processor.py     # HWP 5.0 OLE processing
│   │   ├── hwpx_processor.py    # HWPX (ZIP/XML) processing
│   │   └── ...
│   └── functions/
│       └── img_processor.py     # Image handling utilities
├── chunking/
│   ├── chunking.py              # Main chunking interface
│   ├── text_chunker.py          # Text-based chunking
│   ├── table_chunker.py         # Table-aware chunking
│   └── page_chunker.py          # Page-based chunking
└── ocr/
    ├── base.py                  # OCR base class
    ├── ocr_processor.py         # OCR processing utilities
    └── ocr_engine/              # OCR engine implementations
        ├── openai_ocr.py
        ├── anthropic_ocr.py
        ├── gemini_ocr.py
        └── vllm_ocr.py

Requirements

  • Python 3.12+
  • Required dependencies are automatically installed (see pyproject.toml)

System Dependencies

For full functionality, you may need:

  • Tesseract OCR: For local OCR fallback
  • LibreOffice: For DOC/RTF conversion (optional)
  • Poppler: For PDF image extraction

Configuration

# Custom configuration
config = {
    "pdf": {
        "extract_images": True,
        "ocr_fallback": True,
    },
    "chunking": {
        "default_size": 1000,
        "default_overlap": 200,
    }
}

processor = DocumentProcessor(config=config)

License

Apache License 2.0 - see LICENSE for details.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

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

xgen_doc2chunk-0.2.20.tar.gz (306.1 kB view details)

Uploaded Source

Built Distribution

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

xgen_doc2chunk-0.2.20-py3-none-any.whl (408.5 kB view details)

Uploaded Python 3

File details

Details for the file xgen_doc2chunk-0.2.20.tar.gz.

File metadata

  • Download URL: xgen_doc2chunk-0.2.20.tar.gz
  • Upload date:
  • Size: 306.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for xgen_doc2chunk-0.2.20.tar.gz
Algorithm Hash digest
SHA256 b523cb096cd4ccfa40d602aedde7d74ccb4218fdf0f68fb9f73cc61e7cb62a16
MD5 3611a0dd29f877f9cfd69d394219a726
BLAKE2b-256 69a575da468c46dbfceb14326363672775d005da469b21cc75914b9fc7814d86

See more details on using hashes here.

Provenance

The following attestation bundles were made for xgen_doc2chunk-0.2.20.tar.gz:

Publisher: publish.yml on master0419/xgen_doc2chunk

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file xgen_doc2chunk-0.2.20-py3-none-any.whl.

File metadata

  • Download URL: xgen_doc2chunk-0.2.20-py3-none-any.whl
  • Upload date:
  • Size: 408.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for xgen_doc2chunk-0.2.20-py3-none-any.whl
Algorithm Hash digest
SHA256 5ec5434ce3b60ee9923bb14b2e9e87ca59d98906e9204de7e0142f9ff77350a2
MD5 558bae2a21bfb1fe6dec02680f3b16da
BLAKE2b-256 56500b2586932ab2fc57396c20be67603ebffcddca102e29d70c1672ede0eab1

See more details on using hashes here.

Provenance

The following attestation bundles were made for xgen_doc2chunk-0.2.20-py3-none-any.whl:

Publisher: publish.yml on master0419/xgen_doc2chunk

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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