python+pdfplumber:提取和分析PDF中的表格、文本等数据,实现pdf转图片、CSV、JSON、dict

目录

提取文本内容

提取表格内容

提取图片信息

文本框信息数据

将对应页转为图片

转CSV、JSON、字典数据


安装:pip install pdfplumber -i  https://pypi.tuna.tsinghua.edu.cn/simple/

提取文本内容

from pdfplumber import open as op


def read_pdf(pdf_path):
    with op(pdf_path) as pdf:
        for page in pdf.pages:

            # 包含表格在内的所有文本信息
            text = page.extract_text()
            print(text)

 

提取表格内容

可将提取到的表格数据通过写表的方式插入到excel中或做其他处理

            for table in page.extract_tables():
                for row in table:
                    print(row)

提取图片信息

            for img in page.images:
                print(img)

文本框信息数据

            for wds in page.extract_words():   # 文本框位置及内容
                print(wds) 

将对应页转为图片

resolution参数为像素值,值越大,图片越清晰

page.to_image(resolution=500).save(rf'{pdf_path.split(".")[0]}-{page.page_number}.png')

转CSV、JSON、字典数据

            print(page.to_csv())  # 转csv数据
            print(page.to_json(indent=4))  # 转JSON数据
            print(page.to_dict())  # 转字典数据
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值