Python应用—从pdf文件中提取表格,并且保存在excel中

复制代码
import pdfplumber
import pandas as pd


def extract_tables_to_excel(pdf_path, excel_path):
    # 打开PDF文件
    with pdfplumber.open(pdf_path) as pdf:
        # 创建一个空的DataFrame列表,用于存储所有表格数据
        all_tables = []

        # 遍历PDF的每一页
        for page in pdf.pages:
            # 提取当前页的表格
            tables = page.extract_tables()

            # 将每页的表格转换为DataFrame,并添加到all_tables列表中
            for table in tables:
                df = pd.DataFrame(table)
                all_tables.append(df)

        # 将所有表格数据合并为一个DataFrame
        combined_tables = pd.concat(all_tables, ignore_index=True)

        # 将合并后的表格数据保存到Excel文件中
        combined_tables.to_excel(excel_path, index=False)


# PDF文件路径
pdf_path = '1.pdf'
# Excel文件路径
excel_path = 'output_tables.xlsx'

# 调用函数
extract_tables_to_excel(pdf_path, excel_path)
相关推荐
A尘埃9 分钟前
大模型应用python+Java后端+Vue前端的整合
java·前端·python
A尘埃13 分钟前
LLM大模型评估攻略
开发语言·python
一晌小贪欢37 分钟前
【Python办公】处理 CSV和Excel 文件操作指南
开发语言·python·excel·excel操作·python办公·csv操作
檀越剑指大厂2 小时前
【Python系列】fastapi和flask中的阻塞问题
python·flask·fastapi
humors2212 小时前
服务端开发案例(不定期更新)
java·数据库·后端·mysql·mybatis·excel
YoungHong19923 小时前
【Python进阶】告别繁琐Debug!Loguru一键输出异常日志与变量值
python·debug·异常处理·日志·loguru·log·logger
AiXed3 小时前
PC微信协议之nid算法
python·网络协议·算法·微信
小李哥哥4 小时前
基于数据的人工智能建模流程及源码示例
python
APIshop4 小时前
实战解析:苏宁易购 item_search 按关键字搜索商品API接口
开发语言·chrome·python
蓝桉~MLGT4 小时前
Python学习历程——Python面向对象编程详解
开发语言·python·学习