RaV*_*LLi -1 python pdf pdf-extraction pypdf2
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())
Run Code Online (Sandbox Code Playgroud)
This prints empty strings when it should be printing the contents of the page
小智 43
我尝试了很多方法但都失败了,包括 PyPDF2 和 Tika。我终于找到了对我有用的模块pdfplumber,你也可以试试。
希望这对你有帮助。
import pdfplumber
pdf = pdfplumber.open('pdffile.pdf')
page = pdf.pages[0]
text = page.extract_text()
print(text)
pdf.close()
Run Code Online (Sandbox Code Playgroud)
使用蒂卡对我有用!
from tika import parser
rawText = parser.from_file('January2019.pdf')
rawList = rawText['content'].splitlines()
Run Code Online (Sandbox Code Playgroud)
这使得将银行对帐单中的每一行分别提取到一个列表中非常容易。
如果您正在寻找维护的更大的项目,请查看PyMuPDF。安装它pip install pymupdf并像这样使用它:
import fitz
def get_text(filepath: str) -> str:
with fitz.open(filepath) as doc:
text = ""
for page in doc:
text += page.getText().strip()
return text
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5849 次 |
| 最近记录: |