The Wayback Machine - https://web.archive.org/web/20240714022434/https://www.geeksforgeeks.org/extract-text-from-pdf-file-using-python/
Open In App

Extract text from PDF File using Python

Last Updated : 08 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

All of you must be familiar with what PDFs are. In fact, they are one of the most important and widely used digital media. PDF stands for Portable Document Format. It uses .pdf extension. It is used to present and exchange documents reliably, independent of software, hardware, or operating system.

We will extract text from pdf files using two Python libraries, pypdf and PyMuPDF, in this article. 

Extracting text from a PDF file using the pypdf library.

Python package pypdf can be used to achieve what we want (text extraction), although it can do more than what we need. This package can also be used to generate, decrypting and merging PDF files. Note: For more information, refer to Working with PDF files in Python

Installation

To install this package type the below command in the terminal.

pip install pypdf

Example: Input PDF: extract-pdf-text-python

Python3




# importing required modules
from pypdf import PdfReader
  
# creating a pdf reader object
reader = PdfReader('example.pdf')
  
# printing number of pages in pdf file
print(len(reader.pages))
  
# getting a specific page from the pdf file
page = reader.pages[0]
  
# extracting text from page
text = page.extract_text()
print(text)


Output: 

extract-pdf-python 

Let us try to understand the above code in chunks:

reader = PdfReader('example.pdf')
  • We created an object of PdfReader class from the pypdf module.
  • The PdfReader class takes a required positional argument of the path to the pdf file.
print(len(reader.pages))
  • pages property gives a List of PageObjects. So, here we can use the in-built len() function of python to get the number of pages in the pdf file.
page = reader.pages[0]
  • Now, as reader.pages is a list of PageObjects, we can get a specific Page of the pdf by tapping into the index of the page. In python list indexing starts from 0, so reader.pages[0] gives us the first page of the pdf file.
text = page.extract_text()
print(text)
  • Page object has function extract_text() to extract text from the pdf page.

Extracting text from a PDF file using the PyMuPDF library.

PyMuPDF is a Python library that supports file formats like XPS, PDF, CBR, and CBZ. But for now, in this article, we are going to concentrate on PDF (Portable Document Format) files.

Installation

pip install pymupdf
pip install fitz

To extract the text from the pdf, we need to follow the following steps:

  1. Importing the library
  2. Opening document
  3. Extracting text

Note: We are using the sample.pdf here; to get the pdf, use the link below.  

sample.pdf – Link

   1. Importing the library

Python3




import fitz


   2. Opening document

Python3




doc = fitz.open('sample.pdf')


Here we created an object called “doc,” and filename should be a Python string.

   3. Extracting text

Python3




for page in doc:
    text = page.get_text()
    print(text)


Here, we iterated pages in pdf and used the get_text() method to extract each page from the file.

All the Code to extract the text

Python3




import fitz
doc = fitz.open('sample.pdf')
text = ""
for page in doc:
   text+=page.get_text()
print(text)


Output:

Conclusion

We have seen two Python libraries, pypdf and PyMuPDF, that can extract text from a PDF file. Comment on your preferred library from the above two libraries.



Similar Reads

Convert Text and Text File to PDF using Python
PDFs are one of the most important and widely used digital media. PDF stands for Portable Document Format. It uses .pdf extension. It is used to present and exchange documents reliably, independent of software, hardware, or operating system. Converting a given text or a text file to PDF (Portable Document Format) is one of the basic requirements in
3 min read
Send PDF File through Email using pdf-mail module
pdf_mail module is that library of Python which helps you to send pdf documents through your Gmail account. Installing Library This module does not come built-in with Python. You need to install it externally. To install this module type the below command in the terminal. pip install pdf-mail Function of pdf_mail This module only comes with a singl
2 min read
Convert PDF File Text to Audio Speech using Python
Let us see how to read a PDF that is converting a textual PDF file into audio. Packages Used: pyttsx3: It is a Python library for Text to Speech. It has many functions which will help the machine to communicate with us. It will help the machine to speak to usPyPDF2: It will help to the text from the PDF. A Pure-Python library built as a PDF toolkit
2 min read
Extract numbers from a text file and add them using Python
Python too supports file handling and allows users to handle files i.e., to read and write files, along with many other file handling options, to operate on files. Data file handling in Python is done in two types of files: Text file (.txt extension) Binary file (.bin extension) Here we are operating on the .txt file in Python. Through this program
4 min read
How to convert PDF file to Excel file using Python?
In this article, we will see how to convert a PDF to Excel or CSV File Using Python. It can be done with various methods, here are we are going to use some methods. Method 1: Using pdftables_api Here will use the pdftables_api Module for converting the PDF file into any other format. It's a simple web-based API, so can be called from any programmin
2 min read
How to convert CSV File to PDF File using Python?
In this article, we will learn how to do Conversion of CSV to PDF file format. This simple task can be easily done using two Steps : Firstly, We convert our CSV file to HTML using the PandasIn the Second Step, we use PDFkit Python API to convert our HTML file to the PDF file format. Approach: 1. Converting CSV file to HTML using Pandas Framework. P
3 min read
How to convert a PDF file to TIFF file using Python?
This article will discover how to transform a PDF (Portable Document Format) file on your local drive into a TIFF (Tag Image File Format) file at the specified location. We'll employ Python's Aspose-Words package for this task. The aspose-words library will be used to convert a PDF file to a TIFF file. Aspose-Words: Aspose-Words for Python is a pot
3 min read
Python program to extract Email-id from URL text file
Prerequisite : Pattern Matching with Python Regex Given the URL text-file, the task is to extract all the email-ids from that text file and print the urllib.request library can be used to handle all the URL related work. Example : Input : Hello This is Geeksforgeeks review-team@geeksforgeeks.org review-team@geeksforgeeks.org GfG is a portal for gee
1 min read
Build an Application to extract URL and Metadata from a PDF using Python
The PDF (Portable Document Format) is the most common use platform-independent file format developed by Adobe to present documents. There are lots of PDF-related packages for Python, one of them is the pdfx module. The pdfx module is used to extract URL, MetaData, and Plain text from a given PDF or PDF URL. Features: Extract references and metadata
3 min read
How to extract paragraph from a website and save it as a text file?
Perquisites: Beautiful soupUrllib Scraping is an essential technique which helps us to retrieve useful data from a URL or a html file that can be used in another manner. The given article shows how to extract paragraph from a URL and save it as a text file. Modules Needed bs4: Beautiful Soup(bs4) is a Python library used for getting data from HTML
2 min read
Practice Tags :
three90RightbarBannerImg