PyMuPDF, LLM & RAG

Integrating PyMuPDF into your Large Language Model (LLM) framework and overall RAG (Retrieval-Augmented Generation) solution provides the fastest and most reliable way to deliver document data.

If you need to export to Markdown, structured JSON or TXT formats, PyMuPDF provides the necessary tools to achieve this efficiently in the Document object.

Converting to Markdown

doc = pymupdf.open("input.pdf")
md = doc.to_markdown()

See the API at: Document.to_markdown().

Converting to JSON

doc = pymupdf.open("input.pdf")
json = doc.to_json()

See the API at Document.to_json().

Converting to TXT

doc = pymupdf.open("input.pdf")
txt = doc.to_text()

See the API at: Document.to_text().

Batch conversion

You can convert multiple documents in a batch by iterating over them and applying the conversion methods accordingly. For example:

from pymupdf import convert_batch

inputs = ["report.pdf", "invoice.pdf", "manual.pdf"]
results = convert_batch(
    inputs,
    out_dir="./out",
    workers=1,
    backend="md"
)
for r in results:
    status = "OK" if r.success else f"FAILED: {r.error}"
    print(f"{r.input.name}: {status}")

Integration with LangChain

It is simple to integrate directly with LangChain by using their dedicated loader as follows:

from langchain_community.document_loaders import PyMuPDFLoader
loader = PyMuPDFLoader("example.pdf")
data = loader.load()

See LangChain Using PyMuPDF for full details.

Integration with LlamaIndex

Use the dedicated PyMuPDFReader from LlamaIndex 🦙 to manage your document loading.

from llama_index.readers.file import PyMuPDFReader
loader = PyMuPDFReader()
documents = loader.load(file_path="example.pdf")

See Building RAG from Scratch for more.

Preparing Data for Chunking

You can efficiently prepare your documents for chunking and subsequent processing with your LLM byt using the Document.to_chunks() method.

Create chunked documents as follows:

doc = pymupdf.open("input.pdf")
chunked_document = doc.to_chunks()

Basic queries with chunked documents can be made as follows:

len(chunked_document) # Count the number of Chunks
chunked_document[0], chunked_document[2:5] # a Chunk, a list of Chunks
chunked_document.index(chunked_document[3]) # 3 (Sequence protocol: iteration, slicing, index)
chunked_document.chunks  # the same chunks as a tuple
chunked_document.text # all chunk text joined (lazy)
chunked_document.get("c0") # get chunk by any public id: c{n}, t{n}, f{n}, s{n}, p{page}.b{box}

Using with PyMuPDF Office

For Office document support, PyMuPDF works seamlessly with PyMuPDF Office. Assuming you have PyMuPDF Office installed you will be able to work with Office documents as expected:

import pymupdf
import pymupdf.office
pymupdf.office.unlock()
md = pymupdf.office.to_markdown("sample.doc")

PyMuPDF & PyMuPDF Layout

By default PyMuPDF includes a layout analysis module to enhance output results. To disable this module you can do so by calling the pymupdf.use_layout() method.

PyMuPDF Layout uses ONNX Runtime purely as the inference engine for a small, pre-trained graph neural network (GNN) that performs layout analysis on document pages.

Note

Compatibility with ONNX Runtime and your Python version is required for the layout analysis module to function correctly.

If your Python version does not have a compatible ONNX Runtime your PyMuPDF installation will fail. For full information about what systems ONNX Runtime supports, see https://pypi.org/project/onnxruntime/#files.

After a new Python version is published in October each year, there may be a delay of several weeks before a compatible ONNX wheel becomes available.

For example on 2026-Oct-01, the latest version of Python was 3.15, but there is was no Python 3.15 ONNX Runtime package generally available.

If an official ONNX Runtime build is not yet available for your Python version, see this issue for more details: https://github.com/microsoft/onnxruntime/issues/26547. You only require to install the non-CUDA (CPU-only) version, because PyMuPDF Layout needs no GPU support.

Disabling PyMuPDF Layout

PyMuPDF Layout is enabled by default. To disable this module you can do so by calling the pymupdf.use_layout() method.