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)
相关推荐
程序员龙叔9 小时前
编写高质量 Skill 系列 -- 如何设计需求分析与用例生成的 SKILL
自动化测试·软件测试·python·软件测试工程师·接口测试·性能测试·skill·ai测试
用户83562907805112 小时前
使用 Python 操作 Word 内容控件
后端·python
Non-existent98713 小时前
WPS批量清理单元格空白字符的4种方法-异常数字格式处理-实战
excel·wps
码云骑士14 小时前
32-慢查询排查全流程(下)-索引优化实战与最左前缀原则
python
闵孚龙14 小时前
《PyTorch 深度修炼》Dataset 和 DataLoader:数据如何喂给模型
人工智能·pytorch·python
goldenrolan14 小时前
A公司物料替代测试系统 v1.7:从需求到 exe/apk 的 AI 辅助全链路实践
android·自动化测试·软件测试·python·ai
菜板春15 小时前
jupyter入门-手册-特征探索
python·jupyter
weixin_3975740915 小时前
PDF复杂表格的1:1还原引擎:跨页表格自动拼接技术实战
大数据·人工智能·pdf
Metaphor69215 小时前
使用 Python 将 PDF 转换为 HTML
python·pdf·html