pdfplumber提取文字
import pdfplumber
with pdfplumber.open(‘XXX.Pdf’) as pd:
#pdfplumber.open(PDF路径)
first_page=pdf.pages[0]
#pdf.pages[页数]
print(first_page.extract_text())
pdfplumber提取表格
with pdfplumber.open(‘XXX.Pdf’) as pd:
table_page=pdf.pages[0]
table=table_page.extract_table()
print(table)
#提取多个表格
for table in table_page.extract_tables():
print(table) #table信息为列表类型
提取表格时的设定
table_page.extract_table(
table_settings={
‘vertical_strategy’:‘text’
‘horizontal_strategy’:‘text’
})
写入excel表格文件中
from openpyxl import Workbook
workbook=Workbook()
sheet=workbook.active
for row in table:
sheet.append(row)
workbook.save(filename=‘XXX.xlsx’) #存在空行和将单词分到多个不同列的问题
去除空行,将每个元素连成一个字符串,如果还是一个空字符串那么肯定是空行
new_table=[]
for row in table:
if not ‘’.join([str(item) for item in row])==’’:
#’ ,’.join(S):将列表S中的元素以,连接起来
将被分割的单词所在列内容合成字符串,再合成一个列表
new_row=[]

本文介绍了如何使用python的pdfplumber库来提取PDF文件中的文字和表格,并展示了如何处理提取的表格数据,将其写入Excel文件。通过示例代码,演示了从PDF中获取第一页面的文字,以及提取和处理表格的方法,包括设置提取策略和去除空行。最后,提供了进一步学习的资源链接。
最低0.47元/天 解锁文章
1152

被折叠的 条评论
为什么被折叠?



