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)
相关推荐
程序员佳佳1 天前
2025年大模型终极横评:GPT-5.2、Banana Pro与DeepSeek V3.2实战硬核比拼(附统一接入方案)
服务器·数据库·人工智能·python·gpt·api
刘某的Cloud1 天前
列表、元组、字典、集合-组合数据类型
linux·开发语言·python
ys~~1 天前
git学习
git·vscode·python·深度学习·学习·nlp·github
Mqh1807621 天前
day46 Grad-CAM
python
郝学胜-神的一滴1 天前
Python魔法函数一览:解锁面向对象编程的奥秘
开发语言·python·程序人生
白露与泡影1 天前
使用systemd,把服务装进 Linux 心脏里~
linux·运维·python
yaoh.wang1 天前
力扣(LeetCode) 100: 相同的树 - 解法思路
python·程序人生·算法·leetcode·面试·职场和发展·跳槽
Sunsets_Red1 天前
2025 FZYZ夏令营游记
java·c语言·c++·python·算法·c#
guslegend1 天前
第2章:LangChain大模型工具开发(Agent工具能力)
python
草帽lufei1 天前
Ubuntu中为AI Agent相关开发配置Python环境
python·agent·ai编程