Skip to content

Repository files navigation

PyMuPDF

PyMuPDF

pymupdf%2FPyMuPDF | Trendshift

DocsPyPI VersionPyPI - Python VersionLicense AGPLPyPI DownloadsGithub StarsDiscordForumTwitterHugging FaceDemo

The PDF engine behind over 50 million monthly downloads, powering AI pipelines worldwide.

PyMuPDF is a high-performance Python library for data extraction, analysis, conversion, rendering and manipulation of PDF (and other) documents. Built on top of MuPDF — a lightweight, fast C engine — PyMuPDF gives you precise, low-level control over documents alongside high-level convenience APIs. No mandatory external dependencies.

Star on GitHub


Why PyMuPDF?

  • Fast — powered by MuPDF, a best-in-class C rendering engine
  • Accurate — pixel-perfect text extraction with font, color, and position metadata
  • Versatile — read, write, annotate, redact, merge, split, and convert documents
  • LLM-ready — native Markdown output via PyMuPDF4LLM for RAG and AI pipelines
  • No mandatory dependenciespip install pymupdf and you're done

Installation

pip install pymupdf

Wheels are available for Windows, macOS, and Linux on Python 3.10–3.14. If no pre-built wheel exists for your platform, pip will compile from source (requires a C/C++ toolchain).

Optional extras

PackagePurpose
pymupdf-fontsExtended font collection for text output
pymupdf4llmLLM/RAG-optimised Markdown and JSON extraction
pymupdfproAdds Office document support
tesseract-ocrOCR for scanned pages and images (separate install)
# More fonts
pip install pymupdf-fonts
# LLM-ready extraction
pip install pymupdf4llm
# Office support
pip install pymupdfpro
# OCR (Tesseract must be installed separately)# macOS
brew install tesseract
# Ubuntu / Debian
sudo apt install tesseract-ocr

Supported File Formats

Input

CategoryFormats
PDF & derivativesPDF, XPS, EPUB, CBZ, MOBI, FB2, SVG, TXT, MD
ImagesPNG, JPEG, BMP, TIFF, GIF, and more
Microsoft Office (Pro)DOC, DOCX, XLS, XLSX, PPT, PPTX
Korean Office (Pro)HWP, HWPX

Output

FormatNotes
PDFFull fidelity conversion from Office formats
SVGVector page rendering
Image (PNG, JPEG, …)Page rasterisation at any DPI
MarkdownStructure-aware, LLM-ready
JSONBounding boxes, layout data, per-element detail
Plain textFast, lightweight extraction

Quick start

Extract text

importpymupdfdoc=pymupdf.open("document.pdf")
forpageindoc:
print(page.get_text())

Extract text with layout metadata

importpymupdfdoc=pymupdf.open("document.pdf")
page=doc[0]
blocks=page.get_text("dict")["blocks"]
forblockinblocks:
ifblock["type"] ==0: # text blockforlineinblock["lines"]:
forspaninline["spans"]:
print(f"{span['text']!r} font={span['font']} size={span['size']:.1f}")

Extract tables

importpymupdfdoc=pymupdf.open("spreadsheet.pdf")
page=doc[0]
tables=page.find_tables()
fortableintables:
print(table.to_markdown())
# or get as Pandas DataFramedf=table.to_pandas()

Render a page to an image

importpymupdfdoc=pymupdf.open("document.pdf")
page=doc[0]
pixmap=page.get_pixmap(dpi=150)
pixmap.save("page_0.png")

OCR a scanned document

importpymupdfdoc=pymupdf.open("scanned.pdf")
page=doc[0]
# Requires Tesseract installed and on PATHtext=page.get_textpage_ocr(language="eng").extractText()
print(text)

Convert Markdown to PDF

importpymupdfmd_doc=pymupdf.open("example.md")
md_doc.save("example.pdf")

Convert to Markdown for LLMs

importpymupdf4llmmd=pymupdf4llm.to_markdown("report.pdf")
# Pass directly to your LLM or vector storeprint(md)

Annotate and redact

importpymupdfdoc=pymupdf.open("contract.pdf")
page=doc[0]
# Add a highlight annotationrect=pymupdf.Rect(72, 100, 400, 120)
page.add_highlight_annot(rect)
# Add a redaction and apply itpage.add_redact_annot(rect)
page.apply_redactions()
doc.save("contract_redacted.pdf")

Merge PDFs

importpymupdfmerger=pymupdf.open()
forpathin ["part1.pdf", "part2.pdf", "part3.pdf"]:
merger.insert_pdf(pymupdf.open(path))
merger.save("merged.pdf")

Convert an Office document to PDF

importpymupdf.propymupdf.pro.unlock("YOUR-LICENSE-KEY")
doc=pymupdf.open("presentation.pptx")
pdf_bytes=doc.convert_to_pdf()
withopen("output.pdf", "wb") asf:
f.write(pdf_bytes)

Extract LLM-ready Markdown from a Word document

importpymupdf4llmimportpymupdf.propymupdf.pro.unlock("YOUR-LICENSE-KEY")
md=pymupdf4llm.to_markdown("document.docx")
print(md)

Features

Core capabilities

FeatureDescription
Text extractionPlain text, rich dict (font, size, color, bbox), HTML, XML, raw blocks
Table detectionfind_tables() — locate, extract, and export tables as Markdown or structured data
Image extractionExtract embedded images and render any page to a high-resolution Pixmap
RenderingRender PDF pages to images or Pixmap data for use in UI or other workflows
OCRTesseract integration — full-page or partial OCR, configurable language
AnnotationsRead and write highlights, underlines, squiggly lines, sticky notes, free text, ink, stamps
RedactionAdd and permanently apply redaction annotations
FormsRead and fill PDF AcroForm fields
PDF creationCreate PDFs directly with the API or quickly convert from Markdown files
PDF editingInsert, delete, and reorder pages; set metadata; merge and split documents
DrawingDraw lines, curves, rectangles, and circles; insert HTML boxes
EncryptionOpen password-protected PDFs; save with RC4 or AES encryption
LinksExtract hyperlinks, internal cross-references, and URI targets
BookmarksRead and write the outline / table of contents tree
MetadataTitle, author, creation date, producer, subject, and custom entries
Color spacesRGB, CMYK, greyscale; color space conversion

LLM & AI output (via PyMuPDF4LLM)

OutputAPI
Markdownpymupdf4llm.to_markdown(path)
JSONpymupdf4llm.to_json(path)
Plain textpymupdf4llm.to_text(path)

Supports multi-column layouts, natural reading order and page chunking.

Demo


Supported Python versions

Python 3.10 – 3.14 (as of v1.27.x). Wheels ship for:

  • manylinux x86_64 and aarch64
  • musllinux x86_64
  • macOS x86_64 and arm64
  • Windows x86 and x86_64

Performance

PyMuPDF is built on MuPDF — one of the fastest PDF rendering engines available. Typical benchmarks against pure-Python PDF libraries show 10–50× speed improvements for text extraction and 100× or more for page rendering, with a minimal memory footprint.

For AI workloads, PyMuPDF4LLM processes documents without a GPU, cutting infrastructure costs significantly compared to vision-based LLM approaches.


Recipes

Extract all images from a PDF
importpymupdffrompathlibimportPathdoc=pymupdf.open("document.pdf")
out=Path("images")
out.mkdir(exist_ok=True)
forpage_index, pageinenumerate(doc):
forimg_index, imginenumerate(page.get_images()):
xref=img[0]
pix=pymupdf.Pixmap(doc, xref)
ifpix.n>4: # convert CMYKpix=pymupdf.Pixmap(pymupdf.csRGB, pix)
pix.save(out/f"page{page_index}_img{img_index}.png")
Search for text across a document
importpymupdfdoc=pymupdf.open("document.pdf")
needle="confidential"forpageindoc:
hits=page.search_for(needle)
ifhits:
print(f"Page {page.number}: {len(hits)} occurrence(s)")
forrectinhits:
page.add_highlight_annot(rect)
doc.save("highlighted.pdf")
Split a PDF into individual pages
importpymupdfdoc=pymupdf.open("document.pdf")
fori, pageinenumerate(doc):
out=pymupdf.open()
out.insert_pdf(doc, from_page=i, to_page=i)
out.save(f"page_{i+1}.pdf")
Insert a watermark on every page
importpymupdfdoc=pymupdf.open("document.pdf")
forpageindoc:
page.insert_text(
point=pymupdf.Point(72, page.rect.height/2),
text="DRAFT",
fontsize=72,
color=(0.8, 0.8, 0.8),
rotate=45,
)
doc.save("watermarked.pdf")

Office Document Processing

PyMuPDF can be extended with PyMuPDF Pro. This adds a conversion layer that handles Microsoft and Korean Office formats natively — no Office installation, no COM interop, no LibreOffice subprocess.

Once unlocked, pymupdf.open() accepts Office files exactly like PDFs:

importpymupdf.propymupdf.pro.unlock("YOUR-LICENSE-KEY")
# Works identically regardless of formatforfmtin ["contract.docx", "data.xlsx", "deck.pptx", "report.hwpx"]:
doc=pymupdf.open(fmt)
forpageindoc:
print(page.get_text())

Get a trial license key for PyMuPDF Pro

What you can do with Office documents:

  • Extract text and images page-by-page
  • Convert to PDF with doc.convert_to_pdf()
  • Rasterise pages to PNG/JPEG for visual inspection
  • Feed directly into PyMuPDF4LLM for AI-ready output

Restrictions Without a License Key

When pymupdf.pro.unlock() is called without a key, the following restrictions apply:

RestrictionDetail
Page limitOnly the first 3 pages of any document are accessible
Time limitEvaluation period — functionality expires after a set duration

All other Pro features work normally within these constraints, making it straightforward to prototype before purchasing a license.


Frequently Asked Questions

Can I use PyMuPDF, PyMuPDF4LLM and PyMuPDF Pro without sending data to the cloud?

Yes, absolutely — and this is one of PyMuPDF's most significant advantages.

PyMuPDF runs entirely locally. It is a native Python library built on top of the MuPDF C engine. When you call pymupdf.open(), page.get_text(), page.find_tables(), or any other method, everything executes in-process on your own machine. No data is transmitted anywhere.

There are no telemetry calls, no licence validation callbacks, no cloud dependencies of any kind in the open-source AGPL build or the commercial build. Once the package is installed, it works fully air-gapped.

This makes PyMuPDF well-suited for:

  • Regulated industries — healthcare (HIPAA), finance, legal, government, where documents cannot leave a controlled environment
  • On-premise deployments — servers with no outbound internet access
  • Air-gapped systems — classified or sensitive environments
  • Self-hosted RAG pipelines — processing confidential documents locally before feeding an on-premise LLM
  • Saving on token costs for document pre-processing before sending data to your LLM

The only thing you need an internet connection for is the initial pip install. After that, the package and all its capabilities are entirely self-contained.

Should I import pymupdf or import fitz?

Use import pymupdf. The fitz name is a legacy alias that still works as of v1.24.0+, but import pymupdf is the recommended and future-proof approach. The two are interchangeable in existing code:

importpymupdf# recommended# import fitz # legacy alias — still works but avoid for new code

Does PyMuPDF work with Korean, Japanese, or Chinese documents?

Yes — PyMuPDF has solid CJK support

How do I extract Markdown from PDF for LLM?

Let PyMuPDF4LLM do everything (recommended for RAG).

PyMuPDF4LLM is a high-level wrapper that outputs standard text and table content together in an integrated Markdown-formatted string across all document pages PyMuPDF — tables are detected, converted to GitHub-compatible Markdown, and interleaved with surrounding text in the correct reading order. This is the best starting point for feeding an LLM or building a RAG pipeline.

importpymupdf4llmmd=pymupdf4llm.to_markdown("report.pdf")
print(md)
# Tables appear as Markdown | col1 | col2 | ... inline with the text

Text extraction returns garbled characters or empty output. Why?

This usually means the PDF uses custom font encodings without a proper character map (CMAP). The font's glyphs are present but cannot be mapped back to Unicode. In these cases:

  • Use OCR as a fallback (page.get_textpage_ocr())
  • Consider that scanned PDFs will always need OCR — text extraction on scans returns nothing

How do I extract text from a specific area of a page?

Pass a clip rectangle to get_text():

importpymupdfdoc=pymupdf.open("input.pdf")
page=doc[0]
# Define the area you want (x0, y0, x1, y1) in pointsclip=pymupdf.Rect(50, 100, 400, 300)
text=page.get_text("text", clip=clip)

How do I search for text and find its location on the page?

importpymupdfdoc=pymupdf.open("input.pdf")
page=doc[0]
# Returns a list of Rect objects surrounding each matchlocations=page.search_for("invoice number")
forrectinlocations:
print(rect) # e.g. Rect(72.0, 120.5, 210.0, 134.0)

get_images shows no images but I can clearly see charts in the PDF. Why?

Charts and diagrams created by tools like matplotlib, Excel, or R are typically rendered as vector graphics (PDF drawing commands), not raster images. get_images only lists embedded raster image objects and will not detect vector graphics. To capture these, rasterise the entire page with page.get_pixmap().

How does OCR work in PyMuPDF? Does it require a separate Tesseract installation?

PyMuPDF uses MuPDF's built-in Tesseract-based OCR support, so there is no Python-level pytesseract dependency. However, PyMuPDF still needs access to the Tesseract language data files (tessdata), and automatic tessdata discovery may invoke the tesseract executable (for example, to list available languages) if you do not explicitly provide a tessdata path. In practice, the recommended setup is to either install Tesseract so discovery works automatically, or configure the tessdata location yourself via the tessdata parameter or the TESSDATA_PREFIX environment variable. Over 100 languages are supported.

importpymupdfdoc=pymupdf.open("scanned.pdf")
page=doc[0]
# Get a text page using OCRtp=page.get_textpage_ocr(language="eng")
text=page.get_text(textpage=tp)
print(text)

How do I run OCR on a standalone image file (not a PDF)?

importpymupdfpix=pymupdf.Pixmap("image.png")
ifpix.alpha:
pix=pymupdf.Pixmap(pix, 0) # remove alpha channel — required for OCR# Wrap in a 1-page PDF and OCR itdoc=pymupdf.open()
page=doc.new_page(width=pix.width, height=pix.height)
page.insert_image(page.rect, pixmap=pix)
tp=page.get_textpage_ocr()
text=page.get_text(textpage=tp)

How do I highlight text in a PDF?

importpymupdfdoc=pymupdf.open("input.pdf")
page=doc[0]
# Use quads=True for accurate highlights on non-horizontal textquads=page.search_for("important term", quads=True)
page.add_highlight_annot(quads)
doc.save("highlighted.pdf")

PyMuPDF supports all standard PDF text markers: highlight, underline, strikeout, and squiggly.

How do I permanently redact (remove) content from a PDF?

Redaction is a deliberate two-step process so you can review before committing:

importpymupdfdoc=pymupdf.open("input.pdf")
page=doc[0]
# Step 1: Mark the area(s) to redactrect=page.search_for("confidential")[0]
page.add_redact_annot(rect, fill=(1, 1, 1)) # white fill# Step 2: Apply — permanently removes the underlying contentpage.apply_redactions()
doc.save("redacted.pdf")

After apply_redactions(), the original content is gone. It cannot be recovered from the saved file.

How do I read form field values from a PDF?

importpymupdfdoc=pymupdf.open("form.pdf")
page=doc[0]
forfieldinpage.widgets():
print(f"{field.field_name}: {field.field_value}")

How do I fill in a PDF form programmatically?

importpymupdfdoc=pymupdf.open("form.pdf")
page=doc[0]
forfieldinpage.widgets():
iffield.field_name=="First Name":
field.field_value="Ada"field.update()
doc.save("filled_form.pdf")

Can I use multithreading with PyMuPDF?

No. PyMuPDF does not support multithreaded use, even with Python's newer free-threading mode. The underlying MuPDF library only provides partial thread safety, and a fully thread-safe PyMuPDF implementation would still impose a single-threaded overhead — negating the benefit.

Use multiprocessing instead. Each process opens the file independently and works on its own page range:

frommultiprocessingimportPoolimportpymupdfdefprocess_pages(args):
path, start, end=argsdoc=pymupdf.open(path) # each process opens its own handleresults= []
foriinrange(start, end):
results.append(doc[i].get_text())
returnresultswithPool(4) aspool:
chunks= [("input.pdf", 0, 25), ("input.pdf", 25, 50), ...]
all_results=pool.map(process_pages, chunks)

How can I speed up repeated text extraction on the same page?

Reuse a TextPage object. Creating a TextPage is the expensive part — once created, switching between extraction formats is cheap:

importpymupdfpage=doc[0]
tp=page.get_textpage() # create oncetext=page.get_text("text", textpage=tp)
words=page.get_text("words", textpage=tp)
data=page.get_text("dict", textpage=tp)

This can reduce execution time by 50–95% for repeated extractions on the same page.

How do I read and write PDF metadata?

importpymupdfdoc=pymupdf.open("input.pdf")
# Readprint(doc.metadata)
# {'title': '...', 'author': '...', 'subject': '...', 'keywords': '...', ...}# Writedoc.set_metadata({
"title": "Annual Report 2025",
"author": "Finance Team",
"keywords": "annual, finance, 2025"
})
doc.save("output.pdf")

How do I read or set the table of contents / bookmarks?

importpymupdfdoc=pymupdf.open("input.pdf")
# Read — returns a list of [level, title, page_number] entriestoc=doc.get_toc()
forlevel, title, pageintoc:
print(" "*level, title, "→ page", page)
# Writenew_toc= [
[1, "Introduction", 1],
[1, "Methods", 5],
[2, "Data sources", 6],
]
doc.set_toc(new_toc)
doc.save("output.pdf")

Documentation

Full installation guide, API reference, cookbook, and tutorial at pymupdf.readthedocs.io.


Related projects

ProjectDescription
PyMuPDF4LLMLLM/RAG-optimised Markdown and JSON extraction
PyMuPDF ProAdds Office and HWP document support
pymupdf-fontsExtended font collection for PyMuPDF text output

Licensing

PyMuPDF and MuPDF are maintained by Artifex Software, Inc.

  • Open sourceGNU AGPL v3. Free for open-source projects.
  • Commercial — separate commercial licences available from Artifex for proprietary applications.

Contributing

Contributions are welcome. Please open an issue before submitting large pull requests.

⭐ Support this project

If you find this useful, please consider giving it a star — it helps others discover it!

Star on GitHub

Releases

Packages

Used by

Contributors

Languages