玩转python轻松过二级pdf文件,教你用python玩转PDF文档

爱被打了一巴掌 2023-01-17 08:25 279阅读 0赞

点击上方“Python3X”,选择“置顶或者星标”

第一时间收到精彩推送!

python作为一种具有相对简单语法的高级解释语言,即使对于那些没有编程经验的人来说,Python也是简单易操作的。强大的Python库让你事半功倍。在处理文本信息时,通常我们需要从word、PDF文档中提取出信息,而PDF是最重要和最广泛使用的用来呈现和交换文件的数字媒体之一,。PDF包含有用的信息,链接和按钮,表单域,音频,视频和业务逻辑。python库很好地集成并提供处理非结构化数据源。运用python可以轻松从PDF中提取有用信息后,您可以轻松地将该数据用于任何机器学习或自然语言处理模型。

常见的Python库以下是可用于处理PDF文件的一些Python库PDFMiner :一个从PDF文档中提取信息的工具。与其他PDF相关工具不同,它完全专注于获取和分析文本数据。

PyPDF2 :一个纯python PDF库,能够分割,合并,裁剪和转换PDF文件的页面。它还可以向PDF文件添加自定义数据,查看选项和密码。它可以从PDF中检索文本和元数据,以及将整个文件合并在一起。

Tabula-py:一个 tabula-java的简单Python包装器,它可以读取PDF表。您可以从PDF读取表格并转换为pandas的DataFrame。tabula-py还允许您将PDF文件转换为CSV / TSV / JSON文件。

Slate:PDFMiner的包装器实现

PDFQuery:pdfminer,lxml和pyquery的轻量级包装器。它旨在使用尽可能少的代码可靠地从PDF集合中提取数据。

xpdf:xpdf的 Python包装器(目前只是“pdftotext”实用程序)

从pdf中提取文本使用PyPDF2从pdf中提取简单文本,示例代码如下:import PyPDF2# pdf file object# you can find find the pdf file with complete code in belowpdfFileObj = open(‘example.pdf’, ‘rb’)# pdf reader objectpdfReader = PyPDF2.PdfFileReader(pdfFileObj)# number of pages in pdfprint(pdfReader.numPages)# a page objectpageObj = pdfReader.getPage(0)# extracting text from page.# this will print the text you can also save that into Stringprint(pageObj.extractText())

# pdf file object

# you can find find the pdf file with complete code in below

pdfFileObj = open(‘example.pdf’, ‘rb’)

# pdf reader object

pdfReader = PyPDF2.PdfFileReader(pdfFileObj)

# number of pages in pdf

print(pdfReader.numPages)

# a page object

pageObj = pdfReader.getPage(0)

# extracting text from page.

# this will print the text you can also save that into String

print(pageObj.extractText())

从pdf中读取表格数据使用Pdf中的Table数据,我们可以使用Tabula-py,示例代码如下:import tabula# readinf the PDF file that contain Table Data# you can find find the pdf file with complete code in below# read_pdf will save the pdf table into Pandas Dataframedf = tabula.read_pdf(“offense.pdf”)# in order to print first 5 lines of Tabledf.head()

# readinf the PDF file that contain Table Data

# you can find find the pdf file with complete code in below

# read_pdf will save the pdf table into Pandas Dataframe

df = tabula.read_pdf(“offense.pdf”)

# in order to print first 5 lines of Table

df.head()

如果您的Pdf文件包含多个表,可以进行如下设置:df = tabula.read_pdf(“crime.pdf”,multiple_tables = True)

还可以从任何特定PDF页面的特定部分提取信息tabula.read_pdf(“crime.pdf”,area =(126,149,212,462),pages = 1)149,212,462),pages = 1)

设置读取输出为JSON格式

tabula.read_pdf(“crime.pdf”,output_format =“json”)

将Pdf导出到Excel使用以下代码将PDF数据转换为Excel或CSVtabula.convert_into(“crime.pdf”,“crime_testing.xlsx”,output_format =“xlsx”)

更多参考资料

python提取pdf信息:

https://www.geeksforgeeks.org/working-with-pdf-files-in-python/

原文:

https://towardsdatascience.com/python-for-pdf-ef0fac2808b0

PyPDF2库文档:

https://automatetheboringstuff.com/chapter13/

以上,便是今天的分享,希望大家喜欢,觉得内容不错的。欢迎点击「在看」支持,谢谢各位。

366355

感谢您的阅读

366355

发表评论

表情:
评论列表 (有 0 条评论,279人围观)

还没有评论,来说两句吧...

相关阅读

    相关 HelloWorld

    我们知道,要运行 java 程序,必须要配置 Java 开发环境,对吧。这次我们不使用开发工具,用命令行将HelloWorld编译运行出来,将其中的原理搞清楚。 1)将下载好