python pdfplumber读取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):
    for file in files[2]:
        file_list.append(file)
        # print(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)
        p_soruce = '1(.*?)男'  # 利用正则表达式获取你想要的内容
        source = re.findall(p_soruce, tables)
        if source == []:  # 因为名字后面的性别有男有女,所以有可能获取到空值,还有一种情况就是有些文字内容可能会跟你的表达式吻合,所以要多加几个表达式,根据时间情况调整
            p_soruce = '1(.*?)女 居民'
        source = re.findall(p_soruce, tables)
        if source == []:
            p_soruce = '1(.*?)女 残疾'
        source = re.findall(p_soruce, tables)
        if source == []:
            p_soruce = '1(.*?)女'
        source = re.findall(p_soruce, tables)

        new_filename.append(source)
        # source_str = str(source)
        print(new_filename[i])
        pdf.close()
        try:  # 因为有可能有第二次出现名字一样的情况,所以用这个方法暂时顶替,多次出现就不要用这种方法了,暂时我也没更深入去从根源解决
            os.rename(str(file_list[i]), str(eval(str(new_filename[i]).strip('['', '']'))) + '.pdf')
        except Exception as e:
            os.rename(str(file_list[i]), str(eval(str(new_filename[i]).strip('['', '']'))) + str(2) + '.pdf')
        continue

        pdf.close()

在这里插入图片描述

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Wilburzzz

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

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

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

打赏作者

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

抵扣说明:

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

余额充值