Convert PDF to Text in Python

Convert PDF to text in Python

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.

Output:

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.

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_text() 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.

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! 

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *