Skip to content

API Reference parrot.knowledge.pageindex.pdf_to_markdown

github-actions[bot] edited this page Dec 2, 2026 · 4 revisions

parrot.knowledge.pageindex.pdf_to_markdown

View source

PDF → per-page markdown extraction for PageIndex.

A thin, opinionated wrapper around pymupdf4llm.to_markdown(path, page_chunks=True) so the result lines up with :func:parrot.knowledge.pageindex.utils.get_page_tokens (1-based, every physical page represented). The output is consumed by :func:parrot.knowledge.pageindex.builder.build_page_index to emit per-node markdown via start_index/end_index slicing.

Choice of extractor: PageIndex already uses pymupdf4llm indirectly via :class:parrot_loaders.pdf.PDFLoader. Calling it directly keeps the page indexing aligned with get_page_tokens (no implicit reordering, no page-filtering side effects).

Class PageImage

One raster image placed on a PDF page, with its bbox in page points.

Fields:

Field Type Default Description
page int 1-based physical page
index int order of the image on its page
path Path
bbox tuple[float, float, float, float]
width int
height int
sha256 str

Function build_node_markdown_map

Walk a node tree and return {node_id: concatenated_markdown}.

Uses start_index/end_index semantics identical to :func:parrot.knowledge.pageindex.utils.add_node_text (1-based, inclusive range). Folder/synthetic nodes without page ranges contribute the empty string.

build_node_markdown_map(structure: object, pages: list[tuple[int, str]]) -> dict[str, str]

Parameter Type Default Description
structure object
pages list[tuple[int, str]]

Returns: dict[str, str]

Function extract_markdown_per_page

Extract per-physical-page markdown from a PDF.

extract_markdown_per_page(pdf_path: str | Path, *, images_dir: str | Path | None = None, image_format: str = 'png', dpi: int = 150, image_size_limit: float = 0.05, hdr_info: Any = None) -> list[tuple[int, str]]

Parameter Type Default Description
pdf_path str | Path Path to the source PDF on disk.
images_dir str | Path | None None When set, pymupdf4llm also writes every raster image it finds to this directory and the returned markdown carries ![](…) references to the written files. When None (the default) the call is byte-identical to the pre-existing behaviour — no images are written and no extra kwargs are forwarded to pymupdf4llm.to_markdown.
image_format str 'png' Image file format forwarded to pymupdf4llm when images_dir is set. Ignored otherwise.
dpi int 150 Render resolution forwarded to pymupdf4llm when images_dir is set. Ignored otherwise.
image_size_limit float 0.05 Minimum image-to-page area ratio forwarded to pymupdf4llm when images_dir is set. Ignored otherwise.
hdr_info Any None Header detector forwarded to pymupdf4llm.to_markdown as hdr_info (an object with get_header_id(span, page=None), e.g. LayoutHeaders). Forwarded only when not None; with None the call is byte-identical to the pre-existing behaviour.

Returns: list[tuple[int, str]] — [(physical_page_1based, markdown_text), ...] covering every physical page in the document. Empty pages are emitted as (page_num, "") so the returned list is dense and the index space matches :func:get_page_tokens.

Raises:

  • FileNotFoundError — If pdf_path does not exist.
  • ImportError — If pymupdf / pymupdf4llm are not installed.
  • ValueError — If the page count emitted by pymupdf4llm does not match pymupdf.open(path).page_count. Mis-alignment here would silently mis-slice every downstream node.

Function extract_page_images

Extract every sufficiently large page image with its bbox.

Uses Page.get_image_info(xrefs=True) per page to locate raster images and their bbox, then renders a clipped pixmap of that bbox so the figure is captured exactly as laid out (including vector overlays such as callout numbers). Keeps the (page, text) contract of :func:extract_markdown_per_page intact — this is a sibling function, never called by it.

extract_page_images(pdf_path: str | Path, images_dir: str | Path, *, dpi: int = 150, min_area_ratio: float = 0.05) -> list[PageImage]

Parameter Type Default Description
pdf_path str | Path Source PDF.
images_dir str | Path Directory the PNG files are written to (created if missing).
dpi int 150 Render resolution for the clipped pixmap.
min_area_ratio float 0.05 Images whose bbox area is below this fraction of the page area are skipped.

Returns: list[PageImage] — Images in (page, index) order.

Raises:

  • ImportError — pymupdf missing.
  • FileNotFoundError — pdf_path missing.

Attribute logger

Alias pymupdf

Re-exported from parrot.knowledge.pageindex._pymupdf.pymupdf.

Architecture

Clients

Bots & Agents

Memory

Knowledge

Tools & Toolkits

Flows

Experts

Skills

Stores & Loaders

MCP & A2A

Server & APIs

Integrations

Outputs & A2UI

Prompts

Security

Operations

Migration

SDD Tooling

LLM Wiki

Deployment

Decisions

API Reference

Clone this wiki locally