简体   繁体   English

我们如何使用python从pdf中提取特定值?

[英]How can we extract the specific value from pdf using python?

有没有办法使用 NLP 或 python 库从 pdf 中获取特定文本

First you install PyPDF2 library using below command:首先使用以下命令安装 PyPDF2 库:

pip install PyPDF2

type this code:输入以下代码:

Import PyPDF2
mypdf=open(”/home/Desktop/sample.pdf”, mode=”rb”)
pdf_document=PyPDF2.PdfFileReader(mypdf) `

Now creating pdfobject as pdf_document, how much page in pdf then used pdf_document.numPages现在将pdfobject创建为pdf_document,pdf中有多少页面然后使用pdf_document.numPages

first_page=pdf_document.getPage(0) print( first_page.extractText() )

Now you can read pdf file.现在你可以阅读pdf文件了。

if you have any misunderstanding in my answer, please refer to the below link:如果您对我的回答有任何误解,请参考以下链接:

Python for NLP: Working with Text and PDF Files 用于 NLP 的 Python:处理文本和 PDF 文件

You can use either use tika, textract or PyPDF2您可以使用 tika、textract 或 PyPDF2

from tika import parser
data = parser.from_file('your_pdf.pdf')
print(data['text'])

Try pdfreader to extract texts (plain and containing PDF operators) from PDF document尝试pdfreader从 PDF 文档中提取文本(纯文本和包含 PDF 运算符)

Here is a sample code extracting all the above from all document pages.这是从所有文档页面中提取上述所有内容的示例代码。

from pdfreader import SimplePDFViewer, PageDoesNotExist

fd = open(you_pdf_file_name, "rb")
viewer = SimplePDFViewer(fd)

plain_text = ""
pdf_markdown = ""
try:
    while True:
        viewer.render()
        pdf_markdown += viewer.canvas.text_content
        plain_text += "".join(viewer.canvas.strings)
        viewer.next()
except PageDoesNotExist:
    pass

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM