Reading PDF files in Python

Sharath Kumar
2 min readFeb 13, 2024

How to Work With a PDF in Python
Working with PDFs in Python can be a valuable skill for tasks such as extracting information, manipulating content, or creating new documents. In this guide, we’ll explore the basic steps and some popular Python libraries to help you get started with PDF operations.

1. How to Understand PDFs in Python :

Before diving into the code, it’s essential to have a basic understanding of how PDFs work. PDFs (Portable Document Format) files, are a standardized format for document exchange. They can contain text, images, hyperlinks, forms, and more. In Python, several libraries simplify the process of working with PDFs.

2. Installing PDF Libraries :

To begin, you need to install a PDF manipulation library. Two commonly used libraries are PyPDF2 and PyMuPDF. You can install them using the following commands:

pip install PyPDF2
pip install pymupdf

3. Extracting Text from a PDF :

Using PyPDF2:

# importing required modules in the beginning
import PyPDF2

# creating a pdf file object. Have to give the pdf file path
pdfFileObj = open(‘example.pdf’, ‘rb’)

# creating a pdf reader object
pdfReader = PyPDF2.PdfReader(pdfFileObj)

# printing number of pages in pdf file
print(len(pdfReader.pages))

# creating a page object
pageObj = pdfReader.pages[0]

# extracting text from page
print(pageObj.extract_text())

# closing the pdf file object
pdfFileObj.close()

Using PyMuPDF:

import fitz # PyMuPDF

def extract_text_from_pdf(pdf_path):
doc = fitz.open(pdf_path)
text = “”
for page_number in range(doc.page_count):
page = doc[page_number]
text += page.get_text()
doc.close()
return text

pdf_path = ‘r’C:\Users\shara\Downloads\voters_data.pdf’’
text = extract_text_from_pdf(pdf_path)
print(text)

Conclusion:

Working with PDFs in Python involves selecting the right library for your task and leveraging its features to manipulate or extract information from PDF documents.

Thank you. Happy Learning!

--

--