PDF files are like treasure chests containing valuable data riches like text, tables, images, and more. But this data often lies trapped in PDFs, waiting to be unlocked and extracted for use in your Python applications.
In this comprehensive guide, you‘ll learn professional techniques to extract all kinds of data from PDF documents using Python.
I‘ll share code examples, best practices, handy tips and tricks I‘ve picked up over years of parsing tons of PDFs using Python at work and in side projects.
Whether you‘re an aspiring data scientist looking to expand your Python skills or an expert developer working on an exciting PDF data extraction project, you‘ll find this guide helpful. So let‘s get started!
Why Extract Data from PDFs?
Here are some of the most common reasons you may need to extract data and metadata from PDF documents:
-
Text Analysis – Extracting text from PDFs allows powerful text analysis using Python, from basic stuff like counting word frequencies to advanced machine learning tasks like sentiment analysis, topic modeling, and natural language processing.
-
Data Analysis – Tabular data trapped in PDFs can be extracted and exported into Excel, CSV or JSON formats for further cleaning, visualization and analysis using Python libraries like Pandas, Matplotlib and Seaborn.
-
Web Scraping – Useful information like phone numbers, emails, addresses and URLs can be extracted from PDF documents for web scraping projects.
-
Machine Learning – The extracted text, images and tables can be used to train ML models to classify documents, extract insights, improve search etc.
-
Search Engines – Indexing the plain text content improves PDF document retrieval in search engines like Elasticsearch and Sphinx.
-
Accessibility – Converting PDF text and images into HTML or JSON documents enhances accessibility for visually impaired users.
-
Archiving – Extracting images, text and metadata aids digital preservation of PDF documents in archives and repositories.
-
User Research – Human insights and feedback data trapped in PDF survey and test reports can be extracted for UX analysis.
So if you ever need to unlock data, text, tables or images from PDF documents, Python is up to the task!
Overview of PDF Data Extraction in Python
Here‘s a quick overview of the main steps involved in extracting data from PDFs using Python:
-
Install a PDF extraction library like PyPDF2, PyMuPDF, Camelot or Tika
-
Import the library into your Python program
-
Open the PDF file you want to extract data from
-
Use the library to extract text, images, links or tables from the opened PDF document
-
Store the extracted data in convenient Python data structures like strings, lists, dictionaries etc.
-
Clean and process the extracted data as required for your application
-
Analyze, visualize or export the extracted PDF data using Pandas, NLP libraries, ML frameworks etc.
Don‘t worry if some of these terms sound unfamiliar to you now. By the end of this guide, you‘ll have a solid understanding of how to perform each step.
Let‘s start exploring some code examples of unlocking data from PDFs using Python!
PDF Data Extraction Libraries for Python
There are several excellent Python libraries for extracting text, links, images, tables and metadata from PDF documents:
| Library | Description |
|---|---|
| PyPDF2 | A PDF toolkit to split, merge and transform PDF pages. Allows text and metadata extraction. |
| PyMuPDF | Powerful PDF extraction library. Supports images, tables, annotations, fonts etc. |
| pdfplumber | Designed for precise data extraction into Pandas DataFrames. |
| camelot | Specialized library to extract tables from PDFs. |
| Tika | Python wrapper for Apache Tika PDF extraction tool. |
| PDFQuery | Parses PDF structure using jQuery selectors for extraction. |
| pdfminer | Extracts text and images from PDF documents. |
These libraries make it easy to write Python scripts to unlock specific data from PDF files. Let‘s go through some hands-on examples using two of my favorite PDF parsing libraries – PyPDF2 and PyMuPDF.
Extracting Text from PDF Documents
Extracting text unlocks a goldmine of data from PDFs for further text analysis and NLP tasks in Python.
Here is how to extract text from a PDF file using PyPDF2:
Install PyPDF2
First, install PyPDF2 using pip:
pip install PyPDF2
This adds the PyPDF2 package to your Python environment.
Import PyPDF2
Next, import the PdfFileReader class from PyPDF2:
from PyPDF2 import PdfFileReader
This class can read and parse the text and metadata in PDF files.
Open the PDF File
Pass the PDF file path to PdfFileReader() to create a PDF reader object:
pdf_reader = PdfFileReader(‘report.pdf‘)
This opens ‘report.pdf‘ for reading and extraction.
Extract PDF Metadata
Let‘s first extract some metadata from the PDF like the number of pages:
print(pdf_reader.getNumPages())
This prints the total page count of the opened PDF document.
Extract Text from a Page
To extract text from page 1, get the page object and call extractText() on it:
page_one_text = pdf_reader.getPage(0).extractText()
Note: The page index is zero-based, so getPage(0) returns page 1.
Extract Text from All Pages
To extract text from the entire PDF, loop from 0 to total pages and accumulate the text:
full_text = ""
for page_num in range(pdf_reader.numPages):
page = pdf_reader.getPage(page_num)
page_text = page.extractText()
full_text += page_text
This gives us all the text data from the PDF in the full_text string!
The full code is:
from PyPDF2 import PdfFileReader
pdf_reader = PdfFileReader(‘report.pdf‘)
full_text = ""
for page_num in range(pdf_reader.numPages):
page = pdf_reader.getPage(page_num)
page_text = page.extractText()
full_text += page_text
print(full_text)
And that‘s how you can easily extract plain text content from any PDF file using PyPDF2!
Some pros of using PyPDF2 for text extraction:
- Simple and intuitive API
- Preserves textual order and layout
- Handles encrypted PDFs
- Extracts text in reading order, not visually
Let‘s next see how to extract embedded images from PDF files.
Extracting Images from PDF Documents
Apart from text, PDF files can contain images like logos, figures, diagrams, photos and more. Here‘s how to extract them using PyMuPDF:
Install PyMuPDF
First, install PyMuPDF using pip:
pip install PyMuPDF
Import Modules
Import the modules we need:
import fitz
import shutil
fitz is the PyMuPDF module and shutil helps save files.
Open the PDF
Use fitz.open() to open the PDF:
pdf_doc = fitz.open("report.pdf")
This opens "report.pdf" for reading and extraction.
Iterate Through Pages
Get the total page count and iterate through each page:
for page_num in range(len(pdf_doc)):
# Extract images from this page
page = pdf_doc[page_num]
images = page.getImageList()
# Process each image
for image_index, img in enumerate(images, start=1):
...
page.getImageList() returns the list of images contained in that page.
Extract Image Bytes
Get the image bytes using getImageBlob() and extract metadata:
base_image = img[0]
img_bytes = base_image.getImageBlob()
img_ext = base_image.ext
img_width = base_image.width
img_height = base_image.height
Save the Image
Save it locally using open() and shutil.copyfileobj():
image_name = f"image{image_index}.{img_ext}"
with open(image_name, "wb") as image_file, open(img_bytes, "rb") as img_data:
shutil.copyfileobj(img_data, image_file)
This saves the extracted images from the PDF!
The full code is:
import fitz
import shutil
pdf_doc = fitz.open("report.pdf")
for page_num in range(len(pdf_doc)):
page = pdf_doc[page_num]
images = page.getImageList()
if images:
print(f"[+] Found {len(images)} images in page {page_num}")
for image_index, img in enumerate(images, start=1):
base_image = img[0]
img_bytes = base_image.getImageBlob()
img_ext = base_image.ext
img_width = base_image.width
img_height = base_image.height
image_name = f"image{image_index}.{img_ext}"
with open(image_name, "wb") as image_file, open(img_bytes, "rb") as img_data:
shutil.copyfileobj(img_data, image_file)
print(f"[+] Saved image {image_name}")
This provides a complete way to extract all images inside a PDF. The extracted images can then be used for machine learning, web scraping, image processing and other cool projects in Python!
Some benefits of using PyMuPDF for image extraction:
- Extracts raster images, vector images and even thumbnail images
- Handles different image formats like JPEG, PNG, GIF etc.
- Preserves transparency and metadata
- Can extract images by region
- Works even for malformed PDFs
Next up, let‘s extract tables from PDFs into handy Pandas dataframes.
Extracting Tables from PDFs as Pandas Dataframes
Tables in PDF documents contain a wealth of structured data like financial reports, price sheets, inventory lists and more. The Camelot library provides a specialized API to extract tables from PDFs directly into Pandas dataframes for analysis.
Here is how to use Camelot to extract tabular data from PDFs:
Install Camelot
First, install Camelot using pip:
pip install camelot-py[cv]
The cv extras are needed for better table detection.
Import Camelot
Import camelot:
import camelot
Read the PDF
Pass the PDF file path to read_pdf():
tables = camelot.read_pdf(‘data.pdf‘)
This extracts all the tables found in ‘data.pdf‘.
Convert Tables to Dataframes
The tables are returned as a list. Convert to Pandas dataframe:
df1 = tables[0].df
df2 = tables[1].df
This gives us dataframes containing tabular data from the PDF!
We can now analyze, plot or export these dataframes.
The full code for table extraction is:
import camelot
tables = camelot.read_pdf(‘data.pdf‘)
print(f"Total Tables: {len(tables)}")
df1 = tables[0].df
df2 = tables[1].df
print(df1.head())
print(df2.head())
Camelot can also:
- Extract tables by page number or region
- Handle rotated, nested and poorly formatted tables
- Export tables as Excel, JSON or HTML
- Apply custom data cleaning and postprocessing
So Camelot provides a very handy Python interface to access tabular data locked away inside PDFs!
Up next, let‘s see how to extract URLs and links from PDF documents.
Extracting URLs and Links from PDFs
PDF files can contain clickable hyperlinks and URLs to external websites. Here‘s how to extract them using PyMuPDF:
Import Modules
Import the necessary modules:
import fitz
import pprint
pprint prints the data neatly.
Open the PDF
Open the PDF using fitz.open():
pdf_doc = fitz.open(‘report.pdf‘)
Extract Links from Each Page
Iterate through the pages and use page.getLinks():
links = [] # list to store links
for page in pdf_doc:
page_links = page.getLinks()
links.extend(page_links)
Print the Extracted URLs
Loop through the links and pprint the URL:
for link in links:
pprint(link[‘uri‘])
This prints all the URLs extracted from the PDF.
The full code is:
import fitz
import pprint
pdf_doc = fitz.open(‘report.pdf‘)
links = []
for page in pdf_doc:
page_links = page.getLinks()
links.extend(page_links)
for link in links:
pprint(link[‘uri‘])
This provides an easy way to extract a list of clickable URLs from any PDF file using Python!
The link data returned by page.getLinks() also contains useful metadata like:
- Page number
- Link rectangle coordinates
- Link rectangle width and height
- Top-left corner coordinates
These can help locate the links spatially on the PDF pages.
Enhancing PDF Data Extraction in Python
Here are some tips to handle real-world PDF documents and improve your Python PDF data extraction projects:
Handle Scanned PDFs Carefully
Scanned documents saved as PDFs contain images, not selectable text. OCR may be required using tools like Tesseract before attempting text extraction.
Try Different Libraries as Needed
For tables, Camelot or Tabula work best. For text, PyPDF2 and Tika are good options. Test which library delivers the best results.
Use Precise Extraction Options
Specify page ranges, extract by coordinates, set passwords for encrypted PDFs etc. Extract only what you need.
Validate and Clean Extracted Text
Extracted text may contain weird unicode characters. Normalize, replace and clean text before analysis.
Write Reusable Extraction Functions
Write reusable functions to extract text, tables or images. Makes your code modular.
Store Data in JSON/CSV Files
Save extracted data to files rather than variables. Makes it easy to load data during analysis.
Handle Non-Standard PDFs Carefully
Some PDFs have annotations, comments, forms etc. Handle them accordingly during extraction.
Use Tabula for Difficult Tables
If Camelot struggles, Tabula is another great option with machine learning based detection.
So with the right techniques, you can efficiently extract and unlock text, images, links, tables and other data from PDF files using Python!
The extracted information can then be fed into Pandas, NumPy, scikit-learn, Spark NLP and other Python libraries for exciting data science, NLP, ML and analytics applications.
I hope you found these PDF data extraction examples useful! Extracting rich data from PDFs opens up a lot of possibilities for your Python projects.
Here are some next steps you can try out:
- Build a script that extracts text and URLs from all PDFs in a folder
- Create a PDF table extraction pipeline with Camelot
- Train a Pytorch NLP model using extracted text data
- Extract images from PDFs to create a dataset for CV models
- Load extracted text data into Spark DataFrames on EMR clusters
Let me know in the comments if you have any other PDF data extraction use cases in mind!