You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Python从多个PDF中提取含电压电流等指定信息的表格数据

实现方案

你提取到的每个表格都是pandas的DataFrame格式,第一列是参数名,第二列是参数值,只需要筛选第一列包含Voltage或Current的行即可,同时可以把结果结构化存储,方便后续生成图表。

修改后的完整代码

import os
import camelot.io as camelot
import pandas as pd

# 定义要提取的关键词
target_keywords = ["Voltage", "Current"]
# 存储所有提取结果,方便后续画图
extracted_data = []

pdf_dir = r'D:\Test'
n = 1

arr = os.listdir(pdf_dir)
for item in arr:
    # 跳过非PDF文件避免报错
    if not item.lower().endswith('.pdf'):
        continue
    # 拼接完整文件路径,解决找不到文件的问题
    file_path = os.path.join(pdf_dir, item)
    tables = camelot.read_pdf(file_path, pages='all', split_text=True)
    print(f'''DATENBLATT {n}: {item}
    ''')
    n += 1
    for tabs in tables:
        df = tabs.df
        # 筛选第一列包含目标关键词的行,不区分大小写
        filter_mask = df[0].str.contains('|'.join(target_keywords), case=False, na=False)
        target_rows = df[filter_mask]
        if not target_rows.empty:
            print(target_rows, "\n" + "="*80 + "\n")
            # 把数据存入结果列表,关联文件名方便后续分析
            for _, row in target_rows.iterrows():
                extracted_data.append({
                    "文件名": item,
                    "参数名称": row[0].strip(),
                    "参数值": row[1].strip()
                })

# 把提取结果转为DataFrame,方便后续清洗和绘图
result_df = pd.DataFrame(extracted_data)
# 可选:提取参数中的纯数值,方便绘图
result_df['数值'] = result_df['参数值'].str.extract(r'(\d+\.?\d*)').astype(float)

后续生成图表示例

以对比两个PDF的额定电压为例:

import matplotlib.pyplot as plt

# 筛选额定电压数据
nominal_voltage = result_df[result_df['参数名称'] == 'Voltage Nominal']
plt.bar(nominal_voltage['文件名'], nominal_voltage['数值'])
plt.ylabel('额定电压(V)')
plt.xticks(rotation=15)
plt.title('不同电池包额定电压对比')
plt.show()

内容的提问来源于stack exchange,提问作者alig

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.21 07:54:01