pypdf vs PyMuPDF: Which Python PDF Library Is Right for Your Project?
pypdf vs PyMuPDF
When you need to work with PDFs in Python, two libraries come up in almost every discussion: pypdf and PyMuPDF. They both extract text, but they make very different architectural choices — and those choices matter when you are building a PDF to Markdown pipeline for RAG, LLM context injection, or document automation.
This guide cuts through the noise. You will get a clear comparison on speed, licensing, installation, and LLM-readiness, plus concrete guidance on when to reach for each library.
What Is pypdf?
pypdf (the successor to the deprecated PyPDF2) is a pure-Python library for reading, writing, and manipulating PDF files. "Pure Python" means it has no compiled C extensions — every line of code runs in the Python interpreter.
Key characteristics:
- License: BSD-3-Clause — fully permissive, safe for closed-source and commercial SaaS products
- Install:
pip install pypdf— no system dependencies - Strengths: splitting, merging, rotating, cropping, encrypting, and basic text extraction
- Weaknesses: slower text extraction; limited layout-awareness; no image rendering
pypdf is the library you reach for when you need to manipulate the structure of a PDF (merge two files, extract page 3, fill a form field). For heavy text extraction at volume, its pure-Python execution speed becomes a bottleneck.
What Is PyMuPDF?
PyMuPDF (also imported as fitz) is a Python binding for MuPDF — a battle-tested C rendering engine developed by Artifex. Because the heavy lifting happens in native C code, PyMuPDF extracts text 10–50× faster than pure-Python alternatives in typical benchmarks.
Key characteristics:
- License: AGPL-3.0 — commercial-use or SaaS products must either open-source their entire stack or purchase a commercial license from Artifex
- Install:
pip install pymupdf— includes prebuilt wheels for most platforms; Lambda/Cloud Run deployments occasionally need attention to binary size - Strengths: fast text extraction, page rendering to images, span-level font and style metadata, layout-aware extraction, scanned PDF support via PyMuPDF4LLM
- Weaknesses: AGPL licensing is a blocker for many commercial products; binary dependency adds deployment complexity
Side-by-Side Comparison
| Dimension | pypdf | PyMuPDF |
|---|---|---|
| License | BSD-3-Clause (permissive) | AGPL-3.0 (copyleft) or commercial |
| Text extraction speed | Baseline | 10–50× faster |
| Pure Python | Yes | No (C via MuPDF) |
| Image rendering | No | Yes |
| Layout-aware extraction | Limited | Yes (bounding boxes, spans) |
| LLM/Markdown output | Manual | Via PyMuPDF4LLM extension |
| Serverless deploy | Simple | Works; watch binary size |
| Table extraction | Weak | Better; still imperfect |
| Scanned PDF (OCR) | No | With PyMuPDF4LLM + OCR add-on |
When to Choose pypdf
Use pypdf when:
- Your project is commercial and you cannot absorb AGPL risk. BSD-3-Clause lets you ship pypdf inside any product without opening your source code. AGPL requires disclosure — full stop.
- You are doing structural manipulation, not extraction. Splitting, merging, watermarking, encrypting, and form-filling are pypdf's home turf.
- Your deployment environment is restrictive. Lambda functions, minimal Docker images, and Pyodide (Python-in-browser) all work cleanly with a pure-Python library.
- Your document volume is low. If you are processing dozens of PDFs per hour rather than thousands, pypdf's speed ceiling is not a practical problem.
from pypdf import PdfReader
reader = PdfReader("report.pdf")
for page in reader.pages:
print(page.extract_text())
When to Choose PyMuPDF
Use PyMuPDF when:
- Speed matters at scale. If you are ingesting thousands of PDFs per day into a vector store or document pipeline, the 10–50× throughput advantage compounds quickly.
- You need layout metadata. PyMuPDF exposes character-level bounding boxes, font names, sizes, and colors. This is essential for reconstructing headings, detecting tables, and preserving document structure.
- You are building RAG pipelines. The PyMuPDF4LLM extension converts PDFs to clean, LLM-friendly Markdown, preserving headers, bullets, and code blocks. This is the right foundation for chunking documents for vector databases.
- Your license situation allows it. If you are building an internal tool, a research project, or an open-source product, AGPL is not a blocker.
import pymupdf # or: import fitz
doc = pymupdf.open("report.pdf")
for page in doc:
text = page.get_text("text")
print(text)
For Markdown output specifically:
import pymupdf4llm
md_text = pymupdf4llm.to_markdown("report.pdf")
print(md_text)
The LLM and RAG Angle
Both libraries extract text, but neither was designed with LLM pipelines in mind. If your end goal is clean Markdown for feeding into a RAG pipeline, you will run into real-world rough edges with both:
- pypdf extracts text in reading order but loses headings, table structure, and multicolumn layout. The result is a wall of text that forces your chunking strategy to work harder.
- PyMuPDF gives you better layout fidelity and the PyMuPDF4LLM extension produces proper Markdown — but it still struggles with complex tables, scanned pages, and visually-rich research papers.
For PDFs with mixed content (text, tables, images, headers), the most reliable path is to use a dedicated conversion service. file2markdown.ai handles the conversion server-side and returns clean Markdown you can drop directly into your chunker, without managing library versions or tuning extraction parameters. You can also call it programmatically via the PDF to Markdown API.
This is especially useful when you need to automate PDF to Markdown at scale with Python — the API handles the edge cases so your pipeline code stays simple.
Quick Decision Guide
| Scenario | Reach for |
|---|---|
| Merge/split PDFs in a commercial product | pypdf |
| High-volume text extraction for a research tool | PyMuPDF |
| Building a commercial SaaS document processor | pypdf (or file2markdown.ai API) |
| RAG ingestion pipeline, open-source project | PyMuPDF + PyMuPDF4LLM |
| Complex PDFs with tables and mixed layouts | file2markdown.ai |
| Pure Python, zero binary deps | pypdf |
Frequently Asked Questions
Is PyMuPDF faster than pypdf?
Yes, significantly. PyMuPDF is built on the MuPDF C engine and benchmarks consistently show 10–50× faster text extraction compared to pure-Python libraries like pypdf. The gap widens on large documents or high-volume batch jobs.
Can I use PyMuPDF in a commercial SaaS product?
Only if you either release your entire application's source code under AGPL-3.0 or purchase a commercial license from Artifex. Many teams avoid the AGPL constraint entirely by using pypdf for manipulation tasks and a dedicated conversion API for Markdown output.
What happened to PyPDF2?
PyPDF2 was deprecated in 2022 and all active development moved to pypdf (version 3 onward, now at version 6+). If you see PyPDF2 in older code, replace it with pypdf — the API is compatible for most common use cases.
Which library is better for extracting tables from PDFs?
Neither is great out of the box. PyMuPDF provides more raw layout data, making table detection feasible with extra code. For production-quality table extraction to Markdown, dedicated tools that use vision models or layout-aware ML produce far better results than either library alone. See our guide to extract tables from PDF to Markdown for a deeper comparison of approaches.
The choice between pypdf and PyMuPDF comes down to two things: license and throughput. pypdf is the safe, permissive choice for commercial products and structural manipulation. PyMuPDF is the performance choice for high-volume pipelines where you control the licensing context. For either library, when you need genuinely clean Markdown output from real-world PDFs, file2markdown.ai handles the hard cases so you do not have to.
The Markdown Memo
A fortnightly note for lawyers, researchers, accountants, and anyone else drowning in PDFs, scans, and decks. No spam.