DEV Community

IronSoftware
IronSoftware

Posted on

PyMuPDF Alternative in Python for Text, Images and Splits

A clinic's lab system exports one PDF a night holding every patient's report, a few pages each, with result tables and an image of each ECG trace. A script has to read PDF text in Python from that batch, pull out the images for the cardiology viewer and split the file into one report per patient, and each of those steps is its own piece of code.

PyMuPDF, installed as pymupdf, is Artifex's Python toolkit for extracting, rendering and editing PDFs and other documents, powered by the MuPDF engine. Each step of the batch gets its PyMuPDF calls below, and then IronPDF for Python processes the same file.

Reading the batch page by page in PyMuPDF

Text extraction runs page by page, with one call for each page. Reading the whole export takes a loop and no setup object.

import pymupdf

# Open the lab batch export
doc = pymupdf.open("lab-batch-2026-11-25.pdf")

# Print each page's text as the loop reaches it
for page in doc:
    print(page.get_text())
Enter fullscreen mode Exit fullscreen mode

The default call returns plain text, and the same method takes other options, such as "words", for scripts that want each word with its coordinates.

What do the images and the split take?

Two more loops and a second document. Images are found by cross-reference number, so the script lists each page's images with page.get_images(), passes each number to doc.extract_image() and writes the returned bytes to a file with the extension the dictionary reports. The image recipes add that one image may be referenced by several pages, so a script that extracts per page may want to skip numbers it has already seen.

Splitting works by copying. A new empty document receives a patient's pages through insert_pdf with from_page and to_page, and save writes it, with encryption, owner_pw and user_pw arguments for reports that need a password. Every one of those calls ran on a test batch with PyMuPDF 2.2, and together they ask for three separate pieces of code in one nightly job.

PyMuPDF's compact API and quick releases

A script that only needs page text stays a few lines long, as the loop above shows. The same package renders pages to images, which suits a review tool that shows a report beside its extracted values.

Releases arrive quickly. Version 2.0 reached PyPI on October 19, 2026, 2.1 on November 2 and 2.2 on November 25, each requiring Python 3.11 or later. PyMuPDF and MuPDF are offered under the AGPL or a commercial license from Artifex, and the documentation asks teams to confirm their use case complies before choosing the open-source terms.

One document object for text, images and splits

IronPDF for Python reads the text, extracts the images and splits the file from one object. IronPDF documents extracting text page by page and pulling embedded images out, the two reads the batch needs.

from ironpdf import *

# Open the lab batch
batch = PdfDocument.FromFile("lab-batch-2026-11-25.pdf")

# Read the first page on its own, since it carries the patient line
first_page = batch.ExtractTextFromPage(0)
print(first_page.splitlines()[0])

# Pull out every embedded image, including the ECG traces, for the cardiology viewer
images = batch.ExtractAllImages()
for index, image in enumerate(images):
    image.SaveAs(f"ecg-trace-{index}.png")

# Copy pages 0 to 2, the first patient's, into their own report
report = batch.CopyPages(0, 2)
report.SaveAs("patient-report-0001.pdf")
Enter fullscreen mode Exit fullscreen mode

The page index is zero-based, so 0 is the first page of the batch, and ExtractAllText returns the whole file as one string when a search index needs everything at once. IronPDF's split example covers copying page ranges into new documents, so the per-patient reports come from the same object that was read, with no cross-reference numbers to track.

Reports that leave the clinic carry personal data. Each patient file can take passwords and permissions before it is sent, and a protected batch opens with its password passed to FromFile as the second argument.

A rollout for the lab exports

The nightly batch moves first, because it reads text, pulls images and splits files from one export. Per-patient delivery follows with passwords on each split file, and the archive search that only needs ExtractAllText comes last.

IronPDF handled the batch in our run, which used ironpdf 2026.11.0.1 under Python 3.13 on 27 November 2026. The listing read a six-page test batch, printed Patient 0001, Lab report, saved both embedded trace images and wrote a three-page report for the first patient. One real night's batch, checked file by file against the lab system's records, is a sensible first trial. The first night's batch can run under a trial key from the license key guide.

PyMuPDF, MuPDF, Artifex and Python are trademarks of their respective owners, we have no affiliation with any of them, and the facts above are based on the PyMuPDF documentation, PyPI records and IronPDF for Python documentation.

Top comments (0)