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)
相关推荐
Python私教8 分钟前
多个项目怎么安全合并?先适配,再切换
后端·python·架构
北斗落凡尘2 小时前
LangGraph 入门实战(12)--使用MCP
后端·python·langchain
jdksjw3 小时前
同步与异步、阻塞与非阻塞、多线程、协程超详细讲解(Python并发编程从入门到精通)
开发语言·python
Mike_Zhang3 小时前
使用python统计FreeSWITCH呼叫并发
python·freeswitch
SL-staff3 小时前
技术实践:HR如何用JVS-Logic可视化编排实现考勤数据同步(含节点配置与异常处理)
开发语言·python·低代码·钉钉·可视化编排·jvs-logic·hr技术
zzzzzz3105 小时前
AI 助手最危险的不是报错,而是“看起来成功”:我给工作流加了四道保险
人工智能·python
用户8356290780515 小时前
如何使用 Python 将 PowerPoint 转换为 PDF 文档
后端·python
菜冻鱼5 小时前
Python-pytorch-高级技巧
开发语言·人工智能·pytorch·python·深度学习·神经网络·聚类
lpfasd1235 小时前
python webview打包版卡死、开发版正常
开发语言·python
用户8356290780515 小时前
如何使用 Python 为 PowerPoint 幻灯片添加切换效果
后端·python