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)
相关推荐
lifloveyou1 天前
table接口结构
python
2601_961194021 天前
27考研刘晓艳单词pdf
linux·sql·ubuntu·华为·pdf·.net
AI刀刀1 天前
智谱清言保存 pdf 显示该页的尺寸超出范围,AI 导出鸭智能适配页面尺寸稳定导出 PDF
人工智能·pdf·ai导出鸭
Warson_L1 天前
class 扩展
python
前端与小赵1 天前
Python 数据结构陷阱与复数运算优化:列表、元组、字典成员操作辨析及 NumPy 高效实践
python
天天进步20151 天前
Python全栈项目--基于深度学习的视频目标跟踪系统
python·深度学习·音视频
天天进步20151 天前
Python全栈项目--Python自动化运维工具开发
运维·python·自动化
(●—●)橘子……1 天前
力扣第503场周赛练习理解
python·学习·算法·leetcode·职场和发展·周赛
爱吃羊的老虎1 天前
【JAVA】python转java:Spring Boot 入门
java·spring boot·python
GIS从业者1 天前
免费的PDF工具PDF24工具箱
pdf