问题描述
我正在尝试使用Python从PDF文件中提取文本.我的主要目标是尝试创建一个读取银行对帐单并提取其文本以更新Excel文件以轻松记录每月支出的程序.现在,我只专注于从pdf文件中提取文本,但是我不知道该怎么做.
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.
当前将PDF文件中的文本提取为字符串的最佳和最简便的方法是什么?今天最适合使用哪种库,我该怎么办?
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?
我尝试使用PyPDF2,但是每次尝试使用extractText()从任何页面提取文本时,它都会返回空字符串.我曾尝试安装textract,但由于我需要更多的库而出现错误.
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
推荐答案
使用tika为我工作!
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.
这篇关于如何在python 3.7.3中从pdf提取文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!