How to extract text from a PDF in Python

Updated · Tested with Python 3.13, pypdf 7.2.0, PyMuPDF 2.2, pdfplumber 0.11.11, pdfminer.six 20260107

In short

Use pypdf for a quick start with nothing to install but the package, PyMuPDF when speed matters, and pdfplumber when you need the position of each word. All three read the text stored inside a PDF. None of them can read a scanned page, which holds a picture and no text: that needs OCR.

Every example below runs on two small files you can download: company-extract.pdf, a one-page document with labels and values, and report-two-columns.pdf, a page of text in two columns.

pypdf: the simplest start

pypdf is written in pure Python, so it installs anywhere with nothing else to set up.

Shell
pip install pypdf
Python
from pypdf import PdfReader

reader = PdfReader("company-extract.pdf")
for page in reader.pages:
    print(page.extract_text())
Output
COMPANY EXTRACT
Company name Harbor Lane Logistics Ltd
Company number 51-482913-6
Status Active
Registered 14 March 2019
Registered office 22 Quay Street, Haifa
Share capital 1,250.50
Directors
Dana Levi, Chairperson
Omer Cohen, Director
Charges
2 active, 1 released

Each label and its value come out on one line with a space between them. To keep the gap between the two columns as the page shows it, pass extraction_mode="layout" to extract_text().

PyMuPDF: the fastest

PyMuPDF wraps the MuPDF engine, which is written in C. It is by far the quickest of the three.

Shell
pip install pymupdf
Python
import pymupdf

with pymupdf.open("company-extract.pdf") as doc:
    for page in doc:
        print(page.get_text())
Output
COMPANY EXTRACT
Company name
Harbor Lane Logistics Ltd
Company number
51-482913-6
Status
Active

PyMuPDF returns text block by block, so here each label and each value is a line of its own. Call page.get_text(sort=True) to get them side by side, with the gap between them kept as spaces.

Older tutorials write import fitz. That name still works but now prints a deprecation warning; import pymupdf is the current one. Check PyMuPDF's licence before you ship it: it is AGPL, with a paid licence for closed-source use. pypdf and pdfplumber are under permissive licences.

pdfplumber: when you need positions

pdfplumber gives you every word with its place on the page, which is what you need to find a value by where it sits.

Shell
pip install pdfplumber
Python
import pdfplumber

with pdfplumber.open("company-extract.pdf") as pdf:
    page = pdf.pages[0]
    print(page.extract_text())
    for word in page.extract_words()[2:6]:
        print(round(word["x0"], 1), round(word["top"], 1), word["text"])

The text is the same as pypdf's. The words come with coordinates in points, measured from the top left of the page:

Output
68.4 133.0 Company
120.5 133.0 name
204.4 133.0 Harbor
244.1 133.0 Lane

Two columns: where the libraries differ

On report-two-columns.pdf, pypdf and PyMuPDF read the left column to the bottom and then the right column, which is what a person does. pdfplumber reads straight across the page and joins the two columns line by line:

Output
Harbor Lane Logistics moved 4,180 kept the increase below the rise in fuel
containers in the third quarter, eight prices.

To fix that in pdfplumber, cut the page in two and read each half:

Python
import pdfplumber

with pdfplumber.open("report-two-columns.pdf") as pdf:
    page = pdf.pages[0]
    middle = page.width / 2
    left = page.crop((0, 0, middle, page.height)).extract_text()
    right = page.crop((middle, 0, page.width, page.height)).extract_text()
    print(left + "\n" + right)

The guide to keeping columns and lines in order goes further.

How fast they are

We repeated the first sample to make a 200-page file and timed each library reading all of it, on one laptop core:

LibraryTime for 200 pages
PyMuPDF0.09 s
pypdf0.56 s
pdfminer.six0.92 s
pdfplumber1.21 s

Your numbers will differ with your files and machine, but the order rarely does. The comparison of the four libraries has the rest.

What none of them can read: scans

A scanned PDF is a picture of a page. There is no text in the file to extract, so all three libraries return an empty string for company-scan.pdf:

Python
from pypdf import PdfReader

print(repr(PdfReader("company-scan.pdf").pages[0].extract_text()))
Output
''

Reading a scan needs OCR. You can run it yourself, as shown in how to OCR a scanned PDF in Python, or send the file to an API that does it.

When an API is the simpler choice

For a text PDF like the first sample, the libraries above are all you need. An API earns its place when you would rather not choose and tune a library for each kind of file, when your files include right-to-left or complex scripts, or when you want named values instead of a page of text.

100sands reads PDFs and HTML files that contain text, with one call:

Python
import os

import requests

API_KEY = os.environ["SANDS_API_KEY"]  # sand_live_...

with open("company-extract.pdf", "rb") as f:
    response = requests.post(
        "https://api.100sands.com/v1/text",
        headers={"Authorization": f"Bearer {API_KEY}"},
        files={"file": f},
    )
response.raise_for_status()
print(response.json()["text"])

The response has this shape:

JSON
{
  "pages": 1,
  "text": "COMPANY EXTRACT\nCompany name\tHarbor Lane Logistics Ltd\nCompany number\t51-482913-6\nStatus\tActive\n..."
}

Lines come in reading order. Where a label and its value are set apart on one line, they are separated by a tab, and pages are separated by a form feed (\f).

It does not read scans. Sent company-scan.pdf, it answers with status 422 and the code NO_TEXT_IN_FILE instead of an empty string. In a file that mixes text pages with scanned ones, the scanned pages come back blank. For scans you still need OCR, as above. The Free plan reads 100 documents a month, and a document is one file, whatever its page count.

Questions

Which Python library is best for extracting text from a PDF?

For most text PDFs, start with pypdf: it is pure Python and returned clean lines on our samples. Use PyMuPDF if you process many pages, since it was about six times faster in our test, and pdfplumber if you need the coordinates of each word.

Why does extract_text() return an empty string?

The page is almost certainly a scan: a picture of text with no text stored in the file. No PDF library can extract what is not there. Run OCR on the page first, or send the file to a service that does.

Is PyPDF2 still the library to use?

No. PyPDF2 was merged back into pypdf, which is the maintained package. Install pypdf and import from pypdf.

How do I extract text from a two-column PDF in the right order?

pypdf and PyMuPDF read our two-column sample column by column. pdfplumber reads across the page, so crop the page into a left and a right half and extract each one.

Try it on your own documents

A free plan every month, no credit card. See the PDF to text API.

Start free