python实现发票二维码解析

通过发票左上角的二维码信息,获取发票的关键信息,只需将图片格式的电子发票或扫描后的发票图片传入即可.
测试结果如下:

  • 增值税电子普通发票:{'发票代码': '031xxxxxx311', '发票号码': '74xxxx17', '不含税金额': '13665.98', '开票日期': '20210119', '校验码': '1687xxxxxxxxxx768'}
  • 增值税专用发票:{'发票代码': '310xxxxx130', '发票号码': '53xxxx64', '不含税金额': '2949.02', '开票日期': '20210614', '校验码': ''}
from pyzbar import pyzbar
from PIL import Image


def parse_invoice_info(picture):
    """
    发票二维码解析,获取关键信息
    :param picture: 发票图片
    :return: dict
    """
    invoice_info = {}
    img = Image.open(picture)

    # 解析二维码的数据
    results = pyzbar.decode(img)

    for result in results:
        text_list = result.data.decode('utf-8').split(',')
        invoice_info['发票代码'] = text_list[2]
        invoice_info['发票号码'] = text_list[3]
        invoice_info['不含税金额'] = text_list[4]
        invoice_info['开票日期'] = text_list[5]
        invoice_info['校验码'] = text_list[6]
    return invoice_info
posted @ 2021-07-09 14:33  cnblogs用户  阅读(1608)  评论(0编辑  收藏  举报