python读取pdf中的表格 社会保险个人权益记录(参保人员缴费信息)
""" python 读取pdf中的表格 社保信息 数据 """ import pandas as pd import pdfplumber pd.set_option('display.width', None) pd.set_option('display.max_rows', None) pd.set_option('display.max_columns', None) pd.set_option('display.max_colwidth', None) pd.set_option('display.float_format', '{:,.3f}'.format) def search(PDF_path): total_data = [] data = [] with pdfplumber.open(PDF_path) as pdf: pages_data = [page.extract_text() for page in pdf.pages] # 读取PDF每页的文本,共多少页,pages_data就有几个元素 # print(pages_data) # exit() # 将pages_data内的元素按“\n”拆分放入data列表 for item in pages_data: data += item.split("\n") total_data += data # total_data = total_data[2:-5] columns = ['缴费起止年月', '养老缴费月数', '养老年缴费基数', '养老个人缴费', '失业年缴费月数', '失业年缴费基数', '失业个人缴费', '工伤年缴费月数', '工伤缴费基数', '医疗年缴费月数', '医疗年缴费基数', '医疗个人缴费', '生育缴费月数', '生育年缴费基数', ] df = pd.DataFrame() target = [item.split(' ') for item in total_data][21:-12] for x in target: if len(x) < 14: for i in range(14 - len(x)): x.append('0') df_sub = pd.DataFrame(x).T df = pd.concat([df, df_sub], axis=0) df = df.reset_index(drop=True) ind = df[~df[0].str.contains('至')].index # 找到 第 0 列 不包含 ‘至’ 的 行的 index df.drop(ind, inplace=True) # 删除 不包含 某些 索引号 的 行 df.columns = columns return df if __name__ == '__main__': # pdf文件地址 PDF_file = '参保人员缴费信息.pdf' Out_file = '参保人员缴费信息.csv' # PDF_path = os.path.dirname(PDF_file) # PDF_FileName = os.path.abspath(PDF_file).split('\\')[-1] if PDF_file.endswith(".pdf"): df = search(PDF_file) print(df.head(100)) else: print("所提供文件 非 pdf文件!") df.to_csv(Out_file, index=False)
合集:
python
分类:
编程相关 / Python
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了