pdf表格读取和筛选

为了从 PDF 文件中读取表格,并筛选出"注册单位"中包含"建工"的数据,可以使用 PyPDF2、pdfplumber、tabula-py 等库来解析 PDF 文件,然后再进行筛选。由于表格处理更复杂,由于表格在 PDF 文件中通常会以一种表格的形式存在,这些库可以直接读取表格并提取内容。

python 复制代码
import pdfplumber
import pandas as pd

def extract_sxjg_from_pdf(file_path):
    # 用于存储所有筛选出的数据
    selected_rows = []
    header =['序号', '人员姓\n名', '身份证号码', '注册类别', '注册单位']    
    # 打开 PDF 文件
    with pdfplumber.open(file_path) as pdf:
        for page in pdf.pages:
            # 提取每一页的表格数据
            tables = page.extract_tables()
            for table in tables:
                print(table)
                if not table or len(table) < 3:
                    continue

                # 创建 DataFrame(跳过前两行标题)
                df = pd.DataFrame(table[2:], columns=header)
                print(df)
                filtered_df = df[df['注册单位'].str.contains('建工第五', na=False) | df['注册单位'].str.contains('建工集团', na=False)]

          
                # 如果有符合条件的行,则添加到结果列表中
                if not filtered_df.empty:
                    selected_rows.append(filtered_df)
    
    # 合并所有符合条件的 DataFrame
    if selected_rows:
        result_df = pd.concat(selected_rows, ignore_index=True)
        return result_df
    else:
        return pd.DataFrame()  # 返回空的 DataFrame

# 读取 PDF 文件路径
file_path = "/debug/20241023.pdf"  # 替换为实际的 PDF 文件路径
result_df = extract_sxjg_from_pdf(file_path)

# 检查并输出结果
if not result_df.empty:
    print("筛选出的包含'建工'的注册单位名单:")
    print(result_df)
    result_df.to_excel(r'/debug/result_df.xlsx',index=None)
else:
    print("没有找到包含'建工'的注册单位的记录。")
相关推荐
dagouaofei16 小时前
全面整理6款文档生成PPT工具,PDF转PPT不再难
python·pdf·powerpoint
yesyesyoucan16 小时前
PDF全能处理站:压缩、拆分、合并一站式解决方案与核心技术解析
pdf
sunon_18 小时前
解决linux系统PDF中文乱码问题
linux·运维·pdf
小灰灰搞电子20 小时前
Qt PDF模块详解
数据库·qt·pdf
TextIn智能文档云平台20 小时前
如何将公司内部知识库(Word/PDF)接入大模型?
pdf·word
HSJ017021 小时前
PDF专业转换工具 - 专业版V0.1,PDF转图片、拆分、合并,高效稳定可靠,永久免费使用
pdf
dagouaofei21 小时前
文档生成PPT到底快不快?PDF转PPT工具实测分析
python·pdf·powerpoint
轻竹办公PPT2 天前
上传PDF直接生成PPT,适合工作汇报和总结场景
python·pdf·powerpoint
非凡ghost2 天前
FlexiPDF(专业PDF编辑软件)
windows·学习·pdf·软件需求
xinyu_Jina2 天前
PaperStudio:WYSIWYG文档的Web实现——从CSS Print到客户端PDF生成的技术解析
前端·css·pdf