How to Extract Text from PDF. Learn to use Python to extract text... | by Costas Andreou | Towards Data Science

In this blog, we are going to examine the most popular libraries for processing PDFs with Python. A lot of information is shared in the form of PDF, and often we need to extract some details for further processing.
To assist it in my research in identifying the most popular python libraries, I looked across StackOverflow, Reddit and generally lots of google searches. I identified numerous packages, each with its own strengths and weakness. Specifically, users across the internet seem to be using: PyPDF2, Textract, tika, pdfPlumber, pdfMiner.
During my research, however, for one reason or another, I was only able to get 3 of these libraries to work as expected. For some of these libraries, the set up was too complicated (missing dependencies, strange error messages, etc.)
Let us quickly review all these libraries anyway.
The good news with PyPDF2 was that it was a breeze to install. The documentation is somewhat lacking easy examples to follow, but pay close enough attention, and you can figure it out eventually.
The bad news, however, is that the results were not great.
As you can see, it identified the right text, but for some reason, it broke it up into multiple lines.
The code:
import PyPDF2fhandle = open(r'D:\examplepdf.pdf', 'rb')pdfReader = PyPDF2.PdfFileReader(fhandle)pagehandle = pdfReader.getPage(0)print(pagehandle.extractText())
Off to a promising start with the number of people raving about this library. The documentation is also good.
Unfortunately, the latest version has a bug which throws an error every time you try to extract text from a PDF. Following the bug through the library’s dev forum, there may be a fix in the works. Fingers crossed.
Apache Tika has a python library which apparently lets you extract text from PDFs. Installing the Python library is simple enough, but it will not work unless you have JAVA installed.
At least that is the theory. I did not want to install JAVA; hence I remained at: “RuntimeError: Unable to start Tika server.” error.
According to this medium blog (no affiliation), however, once you get it working, it is terrific. So, let’s go with 2/5 rating.
The code would apparently look something like:
from tika import parserfile = r'D:\examplepdf.pdf'file_data = parser.from_file(file)text = file_data['content']print(text)
Right when I started losing faith in the existence of a simple to use python library for mining text out of pdfs, across comes pdfPlumber.
The documentation is not too bad; within minutes, the whole thing gets going. The results are as good as they can be.
Worth noting, however, that the library does specifically say that it works best on machine-generated PDFs rather than scanned documents; which is what I used.
The code:
import pdfplumberwith pdfplumber.open(r'D:\examplepdf.pdf') as pdf:    first_page = pdf.pages[0]    print(first_page.extract_text())
I will be honest; in a typical pythonic way, I glanced at the documentation (twice!) and failed to understand how I was meant to run this package; this includes pdfMiner (not version 3 that I am reviewing here, as well). I even installed it and tried a few things with no success.
Alas, to my rescue comes a kind stranger in StackOverflow. Once you go through the example provided, it is actually easy to follow. Oh, and the results are as good as you would expect:
The code can be found in the linked StackOverflow post.
Another way that this problem could be addressed is by transforming the PDF file into an image. This could be done either programmatically or by taking a screenshot of each page.
Once you have the image files, you can use the tesseract library to extract the text out of them:
towardsdatascience.com
Before you go, if you liked this article, you may also like: