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
相关推荐
刘立军20 分钟前
使用pyHugeGraph查询HugeGraph图数据数据智能老司机4 小时前
精通 Python 设计模式——创建型设计模式数据智能老司机5 小时前
精通 Python 设计模式——SOLID 原则c8i6 小时前
django中的FBV 和 CBVc8i6 小时前
python中的闭包和装饰器葡萄城技术团队7 小时前
从100秒到10秒的性能优化,你真的掌握 Excel 的使用技巧了吗?这里有鱼汤9 小时前
小白必看:QMT里的miniQMT入门教程TF男孩19 小时前
ARQ:一款低成本的消息队列,实现每秒万级吞吐该用户已不存在1 天前
Mojo vs Python vs Rust: 2025年搞AI,该学哪个?站大爷IP1 天前
Java调用Python的5种实用方案:从简单到进阶的全场景解析