I am trying to extract text from a PDF file using Python. My main goal is I am trying to create a program that reads a bank statement and extracts its text to update an excel file to easily record monthly spendings. Right now I am focusing just extracting the text from the pdf file but I don't know how to do so.
What is currently the best and easiest way to extract text from a PDF file into a string? What library is best to use today and how can I do it?
I have tried using PyPDF2 but everytime I try to extract text from any page using extractText(), it returns empty strings. I have tried installing textract but I get errors because I need more libraries I think.
import PyPDF2
pdfFileObj = open("January2019.pdf", 'rb')
pdfReader = PyPDF2.PdfFileReader(pdfFileObj)
pageObj = pdfReader.getPage(0)
print(pageObj.extractText())
This prints empty strings when it should be printing the contents of the page
I have tried many methods but failed, include PyPDF2 and Tika. I finally found the module pdfplumber that is work for me, you also can try it.
Hope this will be helpful to you.
PyPDF2 is highly unreliable for extracting text from pdf . as pointed out here too. it says :
You could instead install and use pdfminer using
pip install pdfminer
or you can use another open source utility named
pdftotext
by xpdfreader. instructions to use the utility is given on the page.you can download the command line tools from here and could use the pdftotext.exe utility using
subprocess
.detailed explanation for using subprocess is given hereTry pdfreader. You can extract either plain text or decoded text containing "pdf markdown":
PyPDF2 does not read whole pdf correctly. You must use this code.
Using tika worked for me!
This made it really easy to extract separate each line in the bank statement into a list.