扬州开发区建设局网站,陕西省建设厅便民服务网站,wordpress link rel,数据分析师Python 读取电子发票PDF 转成Excel
目录
0.前提
1.python相关的处理PDF的库
2.实际好用的
3.实际代码
4.思考 0.前提 只识别普通电子发票PDF#xff0c;提取其中某些关键内容到excel中。
1.python相关的处理PDF的库 如下4个库是经常更新维护的#xff01; pyP…Python 读取电子发票PDF 转成Excel
目录
0.前提
1.python相关的处理PDF的库
2.实际好用的
3.实际代码
4.思考 0.前提 只识别普通电子发票PDF提取其中某些关键内容到excel中。
1.python相关的处理PDF的库 如下4个库是经常更新维护的 pyPDF/pyPDF2、pdfplumber、PyMuPDF、Camelot等4个库。
2.实际好用的 个人推荐pdfplumber它有extract_tables函数
3.实际代码
import pdfplumber
import re
import os
import pandas as pd
处理 普通发票电子PDF 提取关键字段内容信息写入excel中其它类的发票 自己参考对应规律 更改代码即可参考pdfplumber官网、以及https://blog.csdn.net/Burannnn/article/details/129393295def re_text(bt, text):# re 搜索正则匹配 包含re.compile包含的文字内容m1 re.search(bt, text)if m1 is not None:return re_block(m1[0])return Nonedef re_block(text):# 去掉空格、中英文小括号、中文冒号变英文冒号去掉中文全角空格return text.replace( , ).replace( , ).replace(, ).replace(), ).replace(, :)def get_pdf(dir_path):pdf_file []for root, sub_dirs, file_names in os.walk(dir_path):for name in file_names:if name.endswith(.pdf):filepath os.path.join(root, name)pdf_file.append(filepath)return pdf_filedef read(xlsx_path, pdf_root):# 构建excel writer 写入器writer pd.ExcelWriter(xlsx_path)all {开票日期: [],名称: [],纳税人识别号: [],价税合计(小写): [],货物或应税劳务、服务名称: [],规格型号: [],单位: [],数量: [],单价: [],金额: [],税率: [],税额: [],}filenames get_pdf(pdf_root)for filename in filenames:print(f正在读取{filename})with pdfplumber.open(filename) as pdf:first_page pdf.pages[0]pdf_text first_page.extract_text()# print(pdf_text)kaipiao re_text(re.compile(r开票日期(.*)), pdf_text)if kaipiao:all[开票日期].append(kaipiao.replace(开票日期:, ))mingcheng re_text(re.compile(r名\s*称\s*[:]\s*([\u4e00-\u9fa5])), pdf_text)if mingcheng:all[名称].append(mingcheng.replace(名称:, ))# nashuiren re_text(re.compile(r纳税人识别号\s*[:]\s*([a-zA-Z0-9])), pdf_text)# if nashuiren:# all[纳税人识别号].append(nashuiren.replace(纳税人识别号:, ))jine re_text(re.compile(r小写.*(.*[0-9.])), pdf_text)if jine:all[价税合计(小写)].append(jine.replace(小写¥, ))table first_page.extract_tables()[0]# 纳税人识别号 购买方for t in table[0]:t_ str(t).replace( , )nashuiren re_text(re.compile(r纳税人识别号\s*[:]\s*([a-zA-Z0-9])), t_)if nashuiren:all[纳税人识别号].append(nashuiren.replace(纳税人识别号:, ))# 这里根据pdfplumber提取table来依次输出查看规律适合普通发票其它发票打印输出看规律即可for t in table[1]:if not t:continuet_ str(t).replace( , ) # 去掉空格ts t_.split(\n)if 货物或应税劳务、服务名称 in t_:if len(ts) 1:all[货物或应税劳务、服务名称].append(ts[1])else:all[货物或应税劳务、服务名称].append()if 规格型号 in t_:if len(ts) 1:all[规格型号].append(ts[1])else:all[规格型号].append()if 单位 in t_:if len(ts) 1:all[单位].append(ts[1])else:all[单位].append()if 数量 in t_:if len(ts) 1:all[数量].append(ts[1])else:all[数量].append()if 单价 in t_:if len(ts) 1:all[单价].append(ts[1])else:all[单价].append()if 税率 in t_:if len(ts) 1:all[税率].append(ts[1])else:all[税率].append()if 金额 in t_:if len(ts) 1:all[金额].append(ts[1])else:all[金额].append()if 税额 in t_:if len(ts) 1:all[税额].append(ts[1])else:all[税额].append()print(all)df pd.DataFrame(all)df.to_excel(writer)writer.save()returnpdf_root rG:\PDF
xlsx_path rG:\PDF\all.xlsxread(xlsx_path, pdf_root)
4.思考 对于专用发票找到对应的规律即可。这里最好用的是extract_tables函数打印出来找规律即可