Skip to main content

Overview

By default, PyMuPDF4LLM processes every page in a document. The pages parameter lets you specify exactly which pages to extract — as a list of zero-based page indices. It is supported by to_markdown(), to_json(), and to_text().

Zero-Based Indexing

Page numbers in PyMuPDF4LLM are zero-based — the first page of a document is page 0, the second is page 1, and so on.
Passing a page index that doesn’t exist in the document will raise an error. Always check the document’s page count (doc.page_count) before constructing a dynamic page list.

Common Patterns

First N Pages

Last N Pages

A Specific Range

Non-Contiguous Pages

Every Other Page


Getting the Page Count

Use PyMuPDF directly to inspect a document’s page count before building your pages list:

Page Selection with Page Chunks

When using page_chunks=True, the returned list will only contain chunks for the pages you specified. Chunk metadata preserves the original page number from the document:
The page value in chunk metadata reflects the original document page number, not the position in the returned list. Page 4 in the document is always reported as 4, regardless of how many pages were skipped.

Page Selection with to_json() and to_text()

The pages parameter works identically across all three extraction functions:

Processing a Document in Batches

For very large documents, you may want to process pages in batches to manage memory usage:

Skipping Blank or Cover Pages

Combine page selection with a quick content check to skip pages that return no meaningful text:

The pages parameter is supported by to_markdown(), to_json(), and to_text(). For full API signatures see the API Reference.

Next Steps

Saving Output

Write extracted pages to .md, .json, and .txt files.

Extract Markdown

Full walkthrough of to_markdown() with all common options.

Extract JSON

Bounding boxes and layout data for custom pipelines.

OCR

Control automatic OCR behaviour and adaptors.