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("没有找到包含'建工'的注册单位的记录。")
相关推荐
MartinRY40 分钟前
SpringBoot将HTML转化成PDF文件
spring boot·pdf·html
hello_simon3 小时前
超便捷超实用的文档处理工具,PDF排序,功能强大,应用广泛,无需下载,在线使用,简单易用快捷!
pdf·excel·pdf转html·excel转pdf格式
令狐少侠20114 小时前
python之安装PaddlePaddle和PaddleX解析pdf表格
python·ai·pdf·paddlepaddle
Java潘老师5 小时前
DeepSeek 全套汇总资料pdf免费下载(最新更新8篇)
pdf·deepseek
IDRSolutions_CN1 天前
开发PDF时,如何比较 PDF 文件
java·经验分享·pdf·软件工程·团队开发
DreamNotOver1 天前
PDF 中提取数学公式
python·pdf·提取公式
m0_540507782 天前
2026考研数学张宇武忠祥复习视频课,高数基础班+讲义PDF
考研·pdf
ElasticPDF-新国产PDF编辑器3 天前
Vue 项目 PDF 批注插件库在线版 API 示例教程
前端·vue.js·pdf
ElasticPDF-新国产PDF编辑器3 天前
Vue PDF Annotation plugin library online API examples
javascript·vue.js·pdf
Eiceblue3 天前
.NET用C#在PDF文档中添加、删除和替换图片
开发语言·pdf·c#·.net·idea