python实现发票二维码解析

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

  • 增值税电子普通发票:
  • 增值税专用发票:
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  Rong_Z  阅读(1074)  评论(0编辑  收藏  举报