使用python读取京东pdf发票信息导出到excel表格中
代码
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
pip install pdfminer3k
pip install openpyxl
pip install pandas
pip install numpy
"""
import datetime
import os
import re
import pandas as pd
from pdfminer.converter import PDFPageAggregator
from pdfminer.layout import LTTextBoxHorizontal, LAParams
from pdfminer.pdfinterp import PDFResourceManager, PDFPageInterpreter
from pdfminer.pdfinterp import PDFTextExtractionNotAllowed
from pdfminer.pdfparser import PDFParser, PDFDocument
current_time = str(datetime.datetime.now().year) + "年" + str(datetime.datetime.now().month) + "月" + str(
datetime.datetime.now().day) + "日"
def from_pdf_to_txt(read_file, page_start=0, page_end=0):
"""
:param read_file: str. 注意后缀名是".pdf"
:param write_file: str. 注意后缀名填".txt"
:param page_start: int
:param page_end: int
:return:
"""
# 以二进制读模式打开
origin_pdf_file = open(read_file, 'rb')
# 用文件对象来创建一个pdf文档分析器
parser = PDFParser(origin_pdf_file)
# 创建一个pdf文档
doc = PDFDocument()
# 连接分析器与文档对象,这个语句比较有意思,相互set对方进去
parser.set_document(doc)
doc.set_parser(parser)
# 提供初始化密码.如果pdf没有密码,就传入一个空参数
doc.initialize()
# 检测文档是否提供txt转换,不提供就忽略
if not doc.is_extractable:
# 如果pdf不支持提取,则直接报错
raise PDFTextExtractionNotAllowed
else:
# 创建pdf资源管理器 来管理共享资源
srcmgr = PDFResourceManager()
# 创建一个pdf设备对象
device = PDFPageAggregator(srcmgr, laparams=LAParams())
# 创建一个pdf解释器对象
interpreter = PDFPageInterpreter(srcmgr, device)
# 循环遍历列表,每次处理一个page的内容
pages = list(doc.get_pages())
if page_end == 0:
page_end = len(pages)
results = ''
for i in range(page_start, page_end):
interpreter.process_page(pages[i])
# 接受该页面的LTPage对象
layout = device.get_result()
# 这里返回的是一个LTPage对象,里面存放着这个page解析出的各种对象
# 一般包括LTTextBox,LTFigure,LTImage,LTTextBoxHorizontal等等
# 想要获取文本就取它的text属性,即x.get_text()
# 获取text属性
for x in layout:
if isinstance(x, LTTextBoxHorizontal):
# with open(write_file, 'a', encoding='utf-8') as f:
string = x.get_text().replace('\n', '')
string = string.replace(":", '')
string = string.replace(":", '')
results = results + string.replace(' ', '')
# f.write(results)
# 最后关闭原始pdf文件
origin_pdf_file.close()
return results
#
def re_text(bt, text):
m1 = re.search(bt, text)
if not m1 is None:
reText = m1[0]
return reText
def get_pdf(dirpath):
pdf_file = []
for root, subdirs, filenames in os.walk(dirpath):
for filename in filenames:
if filename.endswith('.pdf'):
filepath = os.path.join(root, filename)
pdf_file.append(filepath)
return pdf_file
def get_text(dirpath, xlfilename):
results = []
filenames = get_pdf(dirpath)
for filename in filenames:
pdftext = from_pdf_to_txt(filename, 0, 1)
# print(pdftext)
cont = {}
cont['平台'] = "京东"
# 获取订单号
bt1 = '(?<=订单号)\d+'
rt1 = re_text(bt1, pdftext)
cont['订单号'] = rt1
cont['备注号码'] = None
if not rt1:
bt1 = '\d+号码\d+'
rt1 = re_text(bt1, pdftext)
rt1 = rt1.split("号码")[1]
cont['备注号码'] = rt1
# 获取发票抬头
bt2 = '\d+[\u4e00-\u9fa5]+'
rt2 = re_text(bt2, pdftext)
# print("1-rt2",rt2)
bt2 = '[\u4e00-\u9fa5]+'
rt2 = re_text(bt2, rt2)
# print("2-rt2", rt2)
if "公司" not in rt2:
rt2 = "个人"
cont['发票抬头'] = rt2
# 获取发票金额
bt3 = '-?\d+(\.\d+)?小写'
rt3 = re_text(bt3, pdftext.replace("(", "").replace(")", ""))
rt3 = rt3.replace("小写", "")
cont['发票金额'] = rt3
# 获取发票代码
bt4 = '(?<=发票代码)\d+'
rt4 = re_text(bt4, pdftext)
cont['发票代码'] = rt4
# 获取发票号码
bt5 = '(?<=发票号码)\d+'
rt5 = re_text(bt5, pdftext)
cont['发票号码'] = rt5
# 获取开票日期
bt6 = '(?<=开票日期)\d+'
rt6 = re_text(bt6, pdftext)
rt6 = rt6[0:4] + "年" + rt6[4:6] + "月" + rt6[6:] + "日"
cont['开票日期'] = rt6
cont['创建时间'] = current_time
cont['最后修改时间'] = current_time
results.append(cont)
# print(results)
pf = pd.DataFrame(results)
order = ["平台", "订单号", "备注号码", "发票抬头", "发票金额", "发票代码", "发票号码", "开票日期", "创建时间", "最后修改时间"] # 指定列的顺序
pf = pf[order]
file_path = pd.ExcelWriter(xlfilename) # 打开excel文件
# 替换空单元格
pf.fillna(' ', inplace=True)
# 输出
pf.to_excel(file_path, encoding='utf-8', index=False, sheet_name="sheet1")
file_path.save()
if __name__ == '__main__':
xlfilename = '发票.xlsx'
dirpath = 'C:\\Users\\william\\Desktop\\20210319\\发票'
get_text(dirpath, xlfilename)
三种发票样式
红线画的是需要提取出来的数据