PDF 中的表格是最难处理的内容之一。pdfplumber 可以精确提取表格结构和数据。

一、提取表格

import pdfplumber
import pandas as pd

with pdfplumber.open("报表.pdf") as pdf:
    all_tables = []
    for page in pdf.pages:
        tables = page.extract_tables()
        for table in tables:
            df = pd.DataFrame(table[1:], columns=table[0])
            all_tables.append(df)
    result = pd.concat(all_tables, ignore_index=True)
    result.to_excel("表格数据.xlsx", index=False)

二、提取指定区域

with pdfplumber.open("文件.pdf") as pdf:
    page = pdf.pages[0]
    # 裁剪指定区域 (x0, y0, x1, y1)
    crop = page.within_bbox((50, 50, page.width - 50, page.height - 50))
    table = crop.extract_table()

💡 觉得有用的话,点赞 + 关注【张老师技术栈】吧!

Logo

Agent 垂直技术社区,欢迎活跃、内容共建。

更多推荐