Python is a feature-rich programming language. We can perform different operations on files in python using the different modules and libraries. In this article, we will discuss different ways to convert a pdf file to text in python.
Convert pdf to text using pypdf2
To convert a pdf to text in python, we can use the PyPDF2 module. You can install this module using PIP by executing the following command in the command prompt.
pip3 install PyPDF2
To convert the pdf file to text, we will first open the file using the open()
function in the “rb
” mode. i.e.Instead of the file contents, we will read the file in binary mode. The open()
function takes the filename as the first input argument and the mode as the second input argument. After opening the file, it returns a file object that we assign to the variable myFile
.
After getting the file object, we will create a pdfFileReader
object using the PdfFileReader()
function defined in the PyPDF2
module. The PdfFileReader()
function accepts the file object containing the pdf file as the input argument and returns a pdfFileReader
object. Using the pdfFileReader
, we can convert the PDF file to text.
Also, we will open a file “output.txt
” in write mode to save the text data extracted from the pdf file using the open()
function. We will assign this file object to a variable output_file
.
To create the text file from the PDF file, we will first find the number of pages in the PDF file. For this, we will use the numPages
attribute of the pdfFileReader
object.
1 2 3 4 5 6 7 8 9 |
import PyPDF2 myFile = open("/home/aditya1117/Downloads/ImpactofCovid-19onsectorsofIndianeconomyandbusinesssurvivalstrategies.pdf", "rb") output_file = open("output.txt", "w") pdfReader = PyPDF2.PdfFileReader(myFile) numOfPages = pdfReader.numPages print("The number of pages in the pdf file is:", numOfPages) |
Output:
1 2 3 |
The number of pages in the pdf file is: 1 |
You can view the pdf file used in this example here.
After getting the number of pages in the PDF file, we will use a for loop
to process all the pages of the pdf file. In the for loop, we will extract each page from the PDF file using the getPage()
method. The getPage()
method, when invoked on a pdfFileReader
object, accepts the page number as an input argument and returns a pageObject
containing data from the specified page of the PDF file.
After getting the pageObject
, we will use the extractText()
method to extract text from the current page. After that, we will write the extracted text to the output text file.
After extracting the text from all the pages in pdf, we will close both the text file and the pdf file. Otherwise, the changes will not be saved.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
import PyPDF2 myFile = open("/home/aditya1117/Downloads/ImpactofCovid-19onsectorsofIndianeconomyandbusinesssurvivalstrategies.pdf", "rb") output_file = open("/home/aditya1117/PycharmProjects/pythonProject/output.txt", "w") pdfReader = PyPDF2.PdfFileReader(myFile) numOfPages = pdfReader.numPages for i in range(numOfPages): page = pdfReader.getPage(i) text = page.extractText() output_file.write(text) output_file.close() myFile.close() |
Output:
Convert pdf to text using PDFminer
Instead of using PyPDF2, we can use the PDFminer.six
module to convert a pdf file to a text file. You can install the PDFminer.six
module as follows.
pip3 install pdfminer.six
The PDFminer.six
module provides us with the extract_text()
function that we can use to convert the PDF file to a text file. The extract_text()
function accepts a file object representing the PDF file as an input argument and returns the text data in the file.
After opening the PDF file and the output text file, we can extract the text from the PDF file using the extract_te
xt() function. Then, we will save the text data to the output file. Don’t forget to close the files before the termination of the program.
1 2 3 4 5 6 7 8 |
from pdfminer.high_level import extract_text text = extract_text("/home/aditya1117/Downloads/ImpactofCovid-19onsectorsofIndianeconomyandbusinesssurvivalstrategies.pdf") output_file = open("/home/aditya1117/PycharmProjects/pythonProject/output.txt", "w") output_file.write(text) output_file.close() |
Output:
Conclusion
In this article, we have discussed two ways to convert pdf to a text file in python. Out of all these, the approach using the PyPDF2 module is the fastest in terms of execution speed. However, you can use any approach at your convenience.
I hope you enjoyed reading this article. Stay tuned for more informative articles.
Happy Learning!