python pdfplumber读取PDF指定表格内容批量文件重命名改良版

后面帮大学同学写的需求改良版,需求改为了获得PDF表格中某一行的姓名跟另外一行的姓名,都是在第一页,解决思路是设置表达式规则然后不断尝试获取规则的文字,如果空值继续进入下一个规则,因为笔者的PDF文件有些姓名同样出现不会超过7次,所以在重命名是设置了8次重命名尝试,由于PDF文件有重要敏感信息,所以只写如下代码提供个观看或者思路,PDF就不提供了,看看有帮助就好,没有我也没办法,我主攻的方向目前不是这个。

import sys
sys.setrecursionlimit(5000)  # 使用pyinstaller打包成EXE格式出错解决语句之一
import os
import re
import pdfplumber
# 设置初始目录
file_dir = 'D:\你的文件夹'
file_list = []  # 设置空列表用来接收文件夹下的文件名称
os.chdir('D:\你的文件夹')
for files in os.walk(file_dir):
    count = 1
    for file in files[2]:
        file_list.append(file)
    new_filename = []  # 设置空列表用来接收存放你截取的表格内容文字
    for i in range(len(file_list)):  # 循环文件夹的文件
        pdf = pdfplumber.open(file_list[i])  # 打开循环到的文件
        pages = pdf.pages
        page = pages[0]  # 获取第一页的内容,要获取第二页就改成1,一次类推
        tables = page.extract_text()  # 解析表格内容成文字
        # print(tables)
        pdf.close()

        n_soruce = '1(.*?)男 居民'  # 利用正则表达式获取你想要的内容
        nsource = re.findall(n_soruce, tables)
        if nsource == []:
            n_soruce = '1(.*?)男 残疾'
        nsource = re.findall(n_soruce, tables)
        if nsource == []:
            n_soruce = '1(.*?)男居民'
        nsource = re.findall(n_soruce, tables)
        if nsource == []:
            n_soruce = '1(.*?)男残疾'
        nsource = re.findall(n_soruce, tables)

        if nsource == []:  # 因为名字后面的性别有男有女,所以有可能获取到空值,还有一种情况就是有些文字内容可能会跟你的表达式吻合,所以要多加几个表达式,根据时间情况调整
            n_soruce = '1(.*?)女 居民'
        nsource = re.findall(n_soruce, tables)
        if nsource == []:
            n_soruce = '1(.*?)女 残疾'
        nsource = re.findall(n_soruce, tables)

        if nsource == []:  # 因为名字后面的性别有男有女,所以有可能获取到空值,还有一种情况就是有些文字内容可能会跟你的表达式吻合,所以要多加几个表达式,根据时间情况调整
            n_soruce = '1(.*?)女居民'
        nsource = re.findall(n_soruce, tables)
        if nsource == []:
            n_soruce = '1(.*?)女残疾'
        nsource = re.findall(n_soruce, tables)
        nsource = str(eval(str(nsource).strip('['', '']')))

        if '20201231' in tables:
            p_soruce1 = '1(.*?)20201231'  # 利用正则表达式获取你想要的内容
            source = re.findall(p_soruce1, tables)
            if source != []:
                try:
                    if len(source[0]) < 7:
                        p_soruce2 = '2(.*?)20201231'
                        source = re.findall(p_soruce2, tables)
                    if len(source[0]) == 8:
                        p_soruce3 = '3(.*?)20201231'
                        source = re.findall(p_soruce3, tables)
                    if source == []:
                        p_soruce4 = '4(.*?)20201231'
                        source = re.findall(p_soruce4, tables)
                except Exception as n1:
                    source = source

            source_str = str(source)
            source_name = source_str[2:6]
            source_name = source_name + '--' + nsource
            new_filename.append(source_name)
            pdf.close()

            try:
                os.rename(str(file_list[i]), (str(new_filename[i])) + '.pdf')
            except Exception as e:  # 抓住所有错误,一般放在最后
                try:
                    os.rename(str(file_list[i]), (str(new_filename[i])) + str(2) + '.pdf')
                except Exception as e1:
                    try:
                        os.rename(str(file_list[i]), (str(new_filename[i])) + str(3) + '.pdf')
                    except Exception as e2:
                        try:
                            os.rename(str(file_list[i]), (str(new_filename[i])) + str(4) + '.pdf')
                        except Exception as e3:
                            try:
                                os.rename(str(file_list[i]), (str(new_filename[i])) + str(5) + '.pdf')
                            except Exception as e4:
                                try:
                                    os.rename(str(file_list[i]), (str(new_filename[i])) + str(6) + '.pdf')
                                except Exception as e5:
                                    try:
                                        os.rename(str(file_list[i]), (str(new_filename[i])) + str(7) + '.pdf')
                                    except Exception as e6:
                                        os.rename(str(file_list[i]), (str(new_filename[i])) + str(8) + '.pdf')
            print('原文件名:%s 重命名为:%s' % (str(file_list[i]), new_filename[i]))
            pdf.close()
        elif not '20201231' in tables:
            source_name = '无人帮扶' + str(count) + '--' + nsource
            new_filename.append(source_name)
            pdf.close()
            os.rename(str(file_list[i]), (str(new_filename[i])) + '.pdf')
            print('原文件名:%s 重命名为:%s' % (str(file_list[i]), (str(new_filename[i])) + '.pdf'))
            count += 1
  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Wilburzzz

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值