ofd电子发票解析

#!/usr/bin/env python
#coding=utf-8
import zipfile
from lxml import etree
import json
import traceback

js = {}

with zipfile.ZipFile(r'D:\upload\电子发票.ofd', 'r') as myzip:
    root = etree.XML(myzip.read('Doc_0/Attachs/original_invoice.xml'))
    head = {
        'fpdm': '/eInvoice/fp:InvoiceCode',
        'fphm': '/eInvoice/fp:InvoiceNo',
        'kprq': '/eInvoice/fp:IssueDate',
        'jym': '/eInvoice/fp:InvoiceCheckCode',
        'gfmc': '/eInvoice/fp:Buyer/fp:BuyerName',
        'gfsh': '/eInvoice/fp:Buyer/fp:BuyerTaxID',
        'gfdzdh': '/eInvoice/fp:Buyer/fp:BuyerAddrTel',
        'gfkhh': '/eInvoice/fp:Buyer/fp:BuyerFinancialAccount',
        'mm': '/eInvoice/fp:TaxControlCode',
        'hjje': '/eInvoice/fp:TaxExclusiveTotalAmount',
        'hjse': '/eInvoice/fp:TaxTotalAmount',
        'jshjxx': '/eInvoice/fp:TaxInclusiveTotalAmount',
        'xfmc': '/eInvoice/fp:Seller/fp:SellerName',
        'xfsh': '/eInvoice/fp:Seller/fp:SellerTaxID',
        'xfdzdh': '/eInvoice/fp:Seller/fp:SellerAddrTel',
        'xfkhh': '/eInvoice/fp:Seller/fp:SellerFinancialAccount',
        'skr': '/eInvoice/fp:Payee',
        'fhr': '/eInvoice/fp:Checker',
        'kpr': '/eInvoice/fp:InvoiceClerk'
    }
    namespace = {"fp": "http://www.edrm.org.cn/schema/e-invoice/2019"}
    for key, value in head.items():
        try:
            js[key] = root.xpath(value, namespaces=namespace)[0].text
        except Exception as e:
            print('head', key, value)
            print(traceback.format_exc())
    body = {
        'spmc': 'fp:Item',
        'ggxh': 'fp:Specification',
        'dw': 'fp:MeasurementDimension',
        'sl': 'fp:Quantity',
        'dj': 'fp:Price',
        'je': 'fp:Amount',
        'slv': 'fp:TaxScheme',
        'se': 'fp:TaxAmount'
    }
    js['spxx'] = []
    nodes = root.xpath('/eInvoice/fp:GoodsInfos/fp:GoodsInfo', namespaces=namespace)
    for node in nodes:
        jsitem = {}
        for key, value in body.items():
            try:
                jsitem[key] = node.xpath(value, namespaces=namespace)[0].text
            except Exception as e:
                print('body', key, value)
                print(traceback.format_exc())
        js['spxx'].append(jsitem)

print(json.dumps(js, indent=True, ensure_ascii=False).encode('utf8').decode('utf8'))

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值