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.
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:
Then extract text page by page:
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.
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.
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.
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
pypdffor general text extraction - use
pdfplumberif 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
pypdffirst for digital PDFs with real text objects. - Use
pdfplumberwhen 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
- How to extract the decision rules from scikit-learn decision-tree?
- How to filter a dictionary according to an arbitrary condition function?
- How to filter Pandas dataframe using 'in' and 'not in' like in SQL
- How to filter Pandas dataframe using 'in' and 'not in' like in SQL
- How to filter rows containing a string pattern from a Pandas dataframe
- How to filter rows in pandas by regex
- How to find a Python package's dependencies
- How to find all occurrences of an element in a list
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack 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.