继承框架 -- python脚本

pdf数据处理写到excl

import openpyxl
import pdfplumber

if __name__ == '__main__':
    # pdf_path = '/Users/xushouchun/Desktop/余佳丽/马素敏评审表(1).pdf'
    # excel_file = '/Users/xushouchun/Desktop/余佳丽/空白文档.xlsx'

    excel_file = input("余老板:请输入excel对应输出地址路径: ").strip("'\"")
    # print(excel_file)
    while True:
        try:
            pdf_path = input("余老板:那个输入一下pdf文件对应地址路径,全路径哦: ").strip("'\"")
            # 正常处理的
            structural = [0, 1, 2, 3, 4, 5, 10, 11]
            # 不能正常处理的
            other_structural = [6, 7, 8, 9, 14, 15]

            # 定义数组
            rows = []
            result = {}
            rows_process = []

            # 打开对应的udf
            with pdfplumber.open(pdf_path) as pdf:
                # 获取第二页(索引为1)
                second_page = pdf.pages[1]  # 页码从0开始,1表示第二页
                tables = second_page.extract_tables()

                # 遍历提取的表格数据并写入 Excel
                for table in tables:
                    # 拿到每一行数据
                    for row in table:
                        rows.append(row)

            # print(rows)

            for index, row in enumerate(rows):
                if index in structural:
                    for i in range(0, len(row), 2):  # 每两个元素为一对
                        if i + 1 < len(row) and row[i] is not None:
                            if row[i + 1] is None:
                                result[row[i].replace("\n", '')] = ''
                            else:
                                if i + 1 < len(rows) and rows[i + 1] is not None:
                                    result[row[i].replace("\n", '')] = row[i + 1]



                elif index in other_structural:
                    rows_process.append(row)

            for i in range(0, len(rows_process), 2):
                if i + 1 < len(rows_process):
                    keys = rows_process[i]
                    values = rows_process[i + 1]
                    for j in range(len(keys)):
                        if keys[j] is not None and j < len(values) and values[j] is not None:
                            if "资格取得时间" in keys[j].replace("\n", ''):
                                if "与评" in values[j].replace("\n", ''):

                                    result["资格取得时间"] = values[j].replace("\n", '').replace("与评", "")
                                else:
                                    values[j].replace("\n", '')
                            else:
                                result[keys[j].replace("\n", '')] = values[j].replace("\n", '')

            # for key, value in result.items():
            #     print(f"{key}: {value}")

            title_data = []
            # 打开对应的udf
            with pdfplumber.open(pdf_path) as pdf:
                # 获取第二页(索引为1)
                second_page = pdf.pages[2]  # 页码从0开始,1表示第二页
                tables = second_page.extract_tables()

                # 遍历提取的表格数据并写入 Excel
                for table in tables:
                    # 拿到每一行数据
                    for row in table:
                        title_data.append(row)

            # 提取教育经历数据
            education_experience = []
            for row in title_data:
                if row[0] == '1.教 育 经 历':
                    # 找到教育经历的起始行,接下来处理行
                    start_index = title_data.index(row) + 1  # 下一个行
                    for data_row in title_data[start_index:]:
                        if data_row[0] == '2.工 作 经 历':  # 一旦找到工作经历部分就停止
                            break
                        education_experience.append(data_row)

            # 清除 None 和空行
            education_experience = [row for row in education_experience if row and any(item for item in row)]

            # 找到最后一条教育经历
            if education_experience:
                last_education = education_experience[-1]  # 获取最后一条
                year = last_education[0]  # 2019
                college = last_education[1]  # 湖南工程学院
                degree = last_education[2]  # 专科
                major = last_education[4]  # 土木工程
                result["第一学历"] = degree
                result["第一毕业院校"] = college
                result["第一专业"] = major
                result["第一毕业时间"] = year

            workbook = openpyxl.load_workbook(excel_file)
            sheet = workbook['Sheet1']
            # 找到最后一行
            last_row = sheet.max_row
            # 表头与输入数据字段的对应关系
            header_mapping = {
                '序号': last_row - 1,  # 可以设置为自增编号
                '姓名': result['姓名'],
                '性别': result['性别'],
                '行业': None,  # 需要输入行业信息
                '身份证件号码': result['身份证件号码'],
                '出生地': result['出生地'],
                '出生日期': result['出生日期'],
                '政治面貌': result['政治面貌'],
                '现从事专业及时间': result['现从事专业及时间'],
                '参加工作时间': result['参加工作时间'],
                '现工作单位': result['现工作单位'],
  

            }

            # 将数据写入下一行
            for col_num, (header, value) in enumerate(header_mapping.items(), start=1):
                sheet.cell(row=last_row + 1, column=col_num, value=value)
            # 保存工作簿
            workbook.save(excel_file)

            cont = input("余老板 : 是否继续输入啊?回车继续哈 n 就结束啦: ")
            if cont.lower() == 'n':
                break
        except Exception as e:
            print("这条数据发生错误/找一下寿春同学:", str(e))


pip install pyinstaller
pyinstaller -F xxx -i xxx -n 脚本
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值