Python
PDF
text extraction
programming
tutorial

How to extract text from a PDF file via python?

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

Extracting text from a PDF in Python is straightforward when the PDF already contains selectable text. It becomes much harder when the file is scanned, heavily formatted, or built from images instead of text objects.

That distinction matters more than the library you choose. A “bad extraction” often means the PDF does not actually store text in the way you expect, not that Python failed.

Start With a Text-Based PDF Library

For normal digital PDFs, a lightweight library such as pypdf is a good first choice. It reads the PDF structure and returns the text objects that the document contains.

Install it with:

bash
python3 -m pip install pypdf

Then extract text page by page:

python
1from pathlib import Path
2from pypdf import PdfReader
3
4
5def extract_text(pdf_path: str) -> str:
6    reader = PdfReader(pdf_path)
7    parts = []
8    for page in reader.pages:
9        parts.append(page.extract_text() or "")
10    return "\n".join(parts)
11
12
13if __name__ == "__main__":
14    text = extract_text("sample.pdf")
15    Path("output.txt").write_text(text, encoding="utf-8")
16    print(text[:500])

This is the simplest working solution for many reports, invoices, and generated documents.

Why PDF Extraction Sometimes Looks Broken

A PDF is a layout format, not a semantic text document. The file may store characters in drawing order, not reading order. It may also split words into separate fragments or place columns in positions that confuse a plain text extractor.

That is why extraction often produces:

  • missing spaces
  • broken line order
  • duplicated headers and footers
  • table text that loses its structure

If your real goal is table extraction, searching, or structured parsing, you may need a higher-level tool than a generic text extractor.

Use pdfplumber When Layout Matters

pdfplumber builds on lower-level parsing tools and is often better when you need more control over page layout.

bash
python3 -m pip install pdfplumber
python
1import pdfplumber
2
3with pdfplumber.open("sample.pdf") as pdf:
4    first_page = pdf.pages[0]
5    text = first_page.extract_text()
6    print(text)

This is useful when pypdf returns text but the order is messy. pdfplumber can also help with tables and page coordinates, which makes it a better fit for documents that follow a visual template.

Detect Scanned PDFs Early

If the page is just an image, no text-extraction library can recover text objects that do not exist. In that case, you need OCR.

A practical heuristic is to try normal extraction first. If every page comes back empty or nearly empty, the PDF is probably scanned.

python
1from pypdf import PdfReader
2
3reader = PdfReader("scan.pdf")
4for index, page in enumerate(reader.pages, start=1):
5    text = page.extract_text() or ""
6    print(f"page {index}: {len(text.strip())} characters")

If the counts are close to zero, switch to an OCR pipeline using a tool such as Tesseract together with page image conversion.

Save, Clean, and Post-Process the Output

Real extraction jobs usually need cleanup after parsing. You may want to normalize whitespace, remove repeated headers, or merge wrapped lines.

python
1import re
2from pypdf import PdfReader
3
4
5def cleaned_text(pdf_path: str) -> str:
6    reader = PdfReader(pdf_path)
7    raw = "\n".join(page.extract_text() or "" for page in reader.pages)
8    raw = re.sub(r"[ \t]+", " ", raw)
9    raw = re.sub(r"\n{3,}", "\n\n", raw)
10    return raw.strip()
11
12
13print(cleaned_text("sample.pdf"))

Keep this cleanup conservative. If you normalize too aggressively, you can destroy useful structure such as paragraph boundaries or list formatting.

Choosing the Right Tool

Use a simple decision rule:

  • start with pypdf for general text extraction
  • use pdfplumber if layout order or tables matter
  • use OCR if the PDF is scanned or image-based

That workflow saves time because it matches the actual document type instead of treating every PDF the same way.

Common Pitfalls

The most common mistake is assuming every PDF contains machine-readable text. Many scanned PDFs do not, so normal extraction returns empty strings.

Another mistake is judging a library only by one badly formatted document. PDFs vary widely. A library that works perfectly on generated reports may struggle with brochures, forms, or multi-column academic papers.

People also forget to open files with UTF-8 when saving extracted text. That can corrupt output for non-ASCII content.

Finally, do not expect raw text extraction to preserve tables automatically. If the document’s meaning depends on coordinates and cell boundaries, plain text output is often the wrong target format.

Summary

  • Use pypdf first for digital PDFs with real text objects.
  • Use pdfplumber when reading order or layout matters more.
  • If extraction returns almost nothing, the PDF is likely scanned and needs OCR.
  • PDF is a layout format, so text order and spacing are often imperfect even when extraction succeeds.
  • Plan for light cleanup after extraction, but avoid over-normalizing the result.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.