Skip to main content

Function Signature

Source: pageindex/page_index.py:1058

Description

The page_index_main() function is the core processing function that generates a hierarchical tree structure from PDF documents. Unlike page_index(), it requires an explicit configuration object (opt) rather than individual parameters. This function orchestrates the entire PageIndex generation pipeline:
  1. Validates input (PDF file path or BytesIO object)
  2. Extracts text and tokens from PDF pages
  3. Detects and processes table of contents (if present)
  4. Generates hierarchical structure
  5. Optionally adds node IDs, summaries, and descriptions

Parameters

str or BytesIO
required
Path to PDF file or BytesIO object containing PDF data. Must be:
  • A valid file path ending in .pdf, OR
  • A BytesIO object containing PDF data
Raises ValueError if input type is unsupported.
config (SimpleNamespace)
default:"None"
Configuration object containing processing options. If None, default configuration is loaded from config.yaml. The config object should contain:
  • model: OpenAI model name
  • toc_check_page_num: Pages to check for TOC
  • max_page_num_each_node: Max pages per node
  • max_token_num_each_node: Max tokens per node
  • if_add_node_id: Whether to add node IDs (“yes”/“no”)
  • if_add_node_summary: Whether to generate summaries (“yes”/“no”)
  • if_add_doc_description: Whether to generate doc description (“yes”/“no”)
  • if_add_node_text: Whether to include text (“yes”/“no”)

Return Value

dict
Dictionary containing the PageIndex structure:

Example Usage

Using ConfigLoader

Custom Configuration

Processing BytesIO

Minimal Configuration

With Full Features

Processing Pipeline

The function executes these steps internally:
  1. Validation: Checks if input is a valid PDF file or BytesIO object
  2. Text Extraction: Parses PDF and extracts text with token counts per page
  3. TOC Detection: Searches for table of contents in first N pages
  4. Structure Generation: Creates hierarchical tree using:
    • Detected TOC with page numbers, OR
    • Detected TOC without page numbers, OR
    • AI-generated structure (no TOC found)
  5. Verification: Validates generated structure accuracy
  6. Recursive Subdivision: Splits large nodes exceeding thresholds
  7. Enrichment: Adds node IDs, summaries, and descriptions if requested

Error Handling

Performance Considerations

  • Processing time: ~1-5 minutes for typical documents (50-200 pages)
  • Memory usage: Proportional to document size and enabled features
  • API costs: Higher with summaries enabled; scales with document length
  • Large nodes (>10 pages, >20k tokens) are recursively subdivided

See Also