Search code examples
pythonpdfpython-3.7pypdfpdf-extraction

How to extract text from pdf in Python 3.7


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.

from PyPDF2 import PdfReader

reader = PdfReader("January2019.pdf")
page = reader.pages[0]
print(page.extract_text())

This prints empty strings when it should be printing the contents of the page

edit: This question was asked for a very old PyPDF2 version. New versions of PyPDF2 have improved text extraction a lot


Solution

  • Using tika worked for me!

    from tika import parser
    
    rawText = parser.from_file('January2019.pdf')
    
    rawList = rawText['content'].splitlines()
    

    This made it really easy to extract separate each line in the bank statement into a list.