Skip to main content

Overview

to_markdown() is the primary extraction function in PyMuPDF4LLM. It reads a document and returns its content as a Markdown string, preserving headings, lists, tables, code blocks, images, and reading order as closely as possible.

Common Options

Page Selection

Extract only specific pages by passing a list of zero-based page indices:
Extract every other page by slicing the page list:
For large documents, limiting extraction to the pages you need can dramatically reduce processing time — especially when OCR is involved.

Page Chunks

Return a list of per-page dictionaries instead of a single concatenated string. Each chunk includes the page’s Markdown text and associated metadata:
This is the recommended mode for RAG pipelines and LLM ingestion workflows. See Chunk Schema for more details on the structure of the returned dictionaries.

Headers and Footers

PyMuPDF4LLM can detect and exclude repeating page headers and footers to keep the output clean:

Images

To extract embedded images and reference them inline in the Markdown output:
Image references are embedded as standard Markdown image syntax:
See Images & Graphics for a full breakdown of image options.

Tables

Table extraction is enabled by default. PyMuPDF4LLM renders detected tables as GitHub-flavoured Markdown tables:
See Tables for more detail on table extraction and edge cases.

Full Example

A more complete call combining several options:

For the full API signature including all parameters and return types, see the to_markdown() API reference.

Next Steps

Extract JSON

Bounding boxes and layout data for custom pipelines.

Extract Text

Get clean, plain text output.

Tables

Table extraction explained.

Saving Output

Write out data to file with pathlib.