# PyMuPDF

> Fast Python library for extracting and rendering PDF and document content

- Category: Retrieval & Knowledge (RAG) / Parsing & Ingestion
- Language: Python
- Repository: https://github.com/pymupdf/PyMuPDF
- Website: https://pymupdf.readthedocs.io
- Canonical page: https://ai-tldr.dev/tools/pymupdf/

## In short

PyMuPDF is a Python library for reading, extracting, and rendering content from PDFs and other document formats. It is built on top of MuPDF, a small C engine, and exposes both low-level access to document internals and higher-level helper methods. It sits in the Parsing & Ingestion category of the Retrieval & Knowledge (RAG) tools and is open-source, written in Python.

## Overview

PyMuPDF is a Python library for reading, extracting, and rendering content from PDFs and other document formats. It is built on top of MuPDF, a small C engine, and exposes both low-level access to document internals and higher-level helper methods. It installs with a single pip command and has no mandatory external dependencies.

It is aimed at developers who need to pull text, tables, images, or layout metadata out of documents, or who need to render, annotate, redact, merge, and split PDFs. Beyond plain text, it can return per-span detail like font, size, color, and bounding boxes, which is useful when document structure matters.

Within parsing and ingestion for RAG pipelines, PyMuPDF handles the step of turning raw PDFs into usable data. The companion pymupdf4llm package converts documents to structure-aware Markdown that you can pass straight to an LLM or a vector store.

## What it does

- Text extraction as plain text or a rich dict with font, size, color, and bounding-box metadata
- Table detection with find_tables(), exporting to Markdown or a Pandas DataFrame
- Page rendering to images at any DPI, plus embedded image extraction
- Tesseract-based OCR for scanned pages, with configurable language
- Annotation, redaction, form filling, and PDF editing (merge, split, reorder pages)
- LLM-ready Markdown and JSON output via the pymupdf4llm extra

## Getting started

Install the package with pip and open a document to start extracting content. No external dependencies are required for the core library.

1. Install PyMuPDF

   Wheels are available for Windows, macOS, and Linux on Python 3.10 to 3.14. If no wheel exists for your platform, pip compiles from source and needs a C/C++ toolchain.

```bash
pip install pymupdf
```

2. Extract text from a PDF

   Open the document and read text page by page.

```python
import pymupdf

doc = pymupdf.open("document.pdf")
for page in doc:
    print(page.get_text())
```

3. Extract tables

   Use find_tables() to locate tables and export them as Markdown or a Pandas DataFrame.

```python
import pymupdf

doc = pymupdf.open("spreadsheet.pdf")
page = doc[0]

tables = page.find_tables()
for table in tables:
    print(table.to_markdown())
    df = table.to_pandas()
```

4. Convert to Markdown for LLMs

   Install the pymupdf4llm extra to get structure-aware Markdown you can pass to an LLM or vector store.

```bash
pip install pymupdf4llm

# then in Python:
# import pymupdf4llm
# md = pymupdf4llm.to_markdown("report.pdf")
```

## When to use it

- Ingesting PDFs into a RAG pipeline by converting them to clean Markdown for chunking and embedding
- Pulling structured tables out of reports or spreadsheets-as-PDF into Pandas for analysis
- Extracting text with layout metadata (font, size, position) when document structure matters
- Rendering pages to images, OCRing scanned documents, or redacting sensitive content before sharing
