python提取pdf文本内容_python操作PDF------提取PDF文字内容

本文介绍如何使用Python库pdfplumber从PDF文件中提取文本和表格数据。通过实例演示了基本的用法,包括设置表格提取参数以提高数据提取准确性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#安装 pip install pdfplumber

importpdfplumber#利用pdfplumber提取文字

with pdfplumber.open('基于python的网页爬虫.pdf') as pdf:

first_page=pdf.pages[0]print(first_page.extract_text())#利用pdfplumber单个提取表格

with pdfplumber.open('基于python的网页爬虫.pdf') as pdf:

first_page=pdf.pages[0]print(first_page.extract_table())#利用pdfplumber多个提取表格

with pdfplumber.open('基于python的网页爬虫.pdf') as pdf:

first_page=pdf.pages[0]for table infirst_page.extract_tables():print(table)#利用pdfplumber单个提取财报 table_settings: 提取表格是的设定

with pdfplumber.open('基于python的网页爬虫.pdf') as pdf:

first_page=pdf.pages[0]

table=first_page.extract_tables(

table_settings={'vertical_strategy': 'text','horizontal_strategy': 'text'}

)

new_table=[]for row intable:

new_row=[]#如果不是空行

if not ''.join([str(item) for item in row]) == '':#合并单词

new_row.append(''.join([str(item) if item else '' for item in row[:3]]))

new_row+= row[3:]

new_table.append(new_row)print(new_table)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值