How to extract text from pdf in python 3.7.3

2020-06-22 21:50发布

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

7条回答
等我变得足够好
2楼-- · 2020-06-22 22:47
import pdftables_api
import os

c = pdftables_api.Client('MY-API-KEY')

file_path = "C:\\Users\\MyName\\Documents\\PDFTablesCode\\"

for file in os.listdir(file_path):
    if file.endswith(".pdf"):
        c.xlsx(os.path.join(file_path,file), file+'.xlsx')

Go to https://pdftables.com to get an API key.

CSV, format=csv

XML, format=xml

HTML, format=html

XLSX, format=xlsx-single, format=xlsx-multiple

查看更多
登录 后发表回答