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
相关推荐
李昊哲小课1 小时前
第1章-PySide6 基础认知与环境配置2401_894241922 小时前
用Pygame开发你的第一个小游戏Zzzz_my3 小时前
正则表达式(RE)天天鸭3 小时前
前端仔写了个 AI Agent,才发现大模型只干了 10% 的活setmoon2144 小时前
使用Scikit-learn构建你的第一个机器学习模型2401_833197734 小时前
为你的Python脚本添加图形界面(GUI)敏编程5 小时前
一天一个Python库:tomlkit - 轻松解析和操作TOML配置2401_879693875 小时前
使用Python进行图像识别:CNN卷积神经网络实战yunyun321235 小时前
机器学习模型部署:将模型转化为Web API团子和二花6 小时前
openclaw平替之nanobot源码解析(七):Gateway与多渠道集成