#!/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'))
ofd电子发票解析
最新推荐文章于 2024-06-07 11:04:47 发布