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
相关推荐
吃我一窝jio26 分钟前
Python与Go线程模型深度对比zwd20051 小时前
Manim 怎么放大画面的某一部分?屿华AI2 小时前
AUTOSAR 实战-诊断工具 | UDS 0x27 安全访问的配置化实现:种子密钥算法插件化设计RobinDevNotes2 小时前
Claude Code 剪视频,不上传、不要 API Key如夏倾城3 小时前
VRRP(虚拟路由器冗余协议Virtual Router Redundancy Protocol)IvanCodes4 小时前
LangChain(04):Runnable 与 LCEL 的执行与组合机制特创数字科技4 小时前
纯本地离线运行的文档脱敏工具(技术分享)东莞市云毅网络有限公司5 小时前
用 Playwright 搭一套问答平台引用监测的最小骨架Thanks_ks5 小时前
【第 010 讲】全面掌握 Python 变量与常量:变量的定义与赋值 | 标识符与命名规范 | 变量的底层机制 | 常量与魔术数字飞Link6 小时前
灵巧手是什么?从机械结构、驱动方式到控制原理,认识机器人的精细操作能力