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
相关推荐
timeDifference₇3 小时前
Python Vibe Coding实操实验报告geovindu3 小时前
python:DecisionTree怒放的生命19915 小时前
代码知识库 RAG 问答系统 —— 从 0 到 1 实现教程geovindu5 小时前
python: Decision Tree Regressorfengkai45456 小时前
Python运维Fancy.7616 小时前
:Python条件与循环语句学习总结Wellijohn8 小时前
Python 容器选型实测:list.insert(0) 慢 925 倍,in 慢 6000 倍新*8 小时前
标题全变成一级?靠编号规律找回章节边界高洁0110 小时前
多源数据融合:把零散信号拼成一致图景前端fighter10 小时前
2026年前端面试题总结(pdf版)