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应用—从pdf文件中提取表格,并且保存在excel中
翻车吧奥斯卡2024-07-21 21:45
相关推荐
金銀銅鐵14 小时前
[Python] 从《千字文》中随机挑选汉字cup1118 小时前
[技术复盘] Windows Python 打包实战:Nuitka 环境踩坑总结与 CI 自动化构建全指南aqi0020 小时前
15天学会AI应用开发(七)有了大模型为什么还要引入RAG金銀銅鐵1 天前
用 Python 实现 Take-Away 游戏copyer_xyf1 天前
Agent 流程编排copyer_xyf1 天前
Agent RAGcopyer_xyf1 天前
【RAG】向量数据库:milvuscopyer_xyf1 天前
Agent 记忆管理星云穿梭2 天前
用Python写一个带图形界面的学生管理系统——完整教程