场景将批量的XML发票信息提取汇总到EXCEL中解析XML发票importxml.etree.ElementTreeasET treeET.parse(./【365约车特选-39.54元-1个行程】高德打车电子发票.xml)roottree.getroot()forchildinroot:print(一级分支,child.tag,child.attrib)forsubchildinchild:print(--二级分支,subchild.tag,subchild.attrib,subchild.text)forsubsubchildinsubchild:print(----三级分支,subsubchild.tag,subsubchild.attrib,subsubchild.text)一级分支 Header {} --二级分支 EIid {} 26327000001329568432 --二级分支 EInvoiceTag {} SWEI3200 --二级分支 Version {} 0.32 --二级分支 InherentLabel {} None ----三级分支 InIssuType {} None ----三级分支 EInvoiceType {} None ----三级分支 GeneralOrSpecialVAT {} None ----三级分支 TaxpayerType {} None --二级分支 UndefinedLabel {} None ----三级分支 Label {} None ----三级分支 Label {} None ----三级分支 Label {} None 一级分支 EInvoiceData {} --二级分支 SellerInformation {} None ----三级分支 SellerIdNum {} 91320981MAEBB5QA34 ----三级分支 SellerName {} 昆明盛智易联科技有限公司东台分公司 ----三级分支 SellerAddr {} 东台沿海经济区科创大厦415 ----三级分支 SellerTelNum {} 18108755741 ----三级分支 SellerBankName {} 中信银行北京北辰支行 ----三级分支 SellerBankAccNum {} 8110701013603001914 --二级分支 BuyerInformation {} None ----三级分支 BuyerIdNum {} 91xxxxxxxxxxxxxxxx3 ----三级分支 BuyerName {} xxxxxx有限责任公司 ----三级分支 BuyerAddr {} None ----三级分支 BuyerTelNum {} None ----三级分支 BuyerBankName {} None ----三级分支 BuyerBankAccNum {} None --二级分支 BasicInformation {} None ----三级分支 TotalAmWithoutTax {} 38.39 ----三级分支 TotalTaxAm {} 1.15 ----三级分支 TotalTax-includedAmountInChinese {} 叁拾玖元伍角肆分 ----三级分支 TotalTax-includedAmount {} 39.54 ----三级分支 RequestTime {} 2026-07-18 23:45:14 ----三级分支 Drawer {} 杨艳彩 --二级分支 IssuItemInformation {} None ----三级分支 ItemName {} *交通运输服务*客运服务费 ----三级分支 MeaUnits {} None ----三级分支 Quantity {} 1 ----三级分支 UnPrice {} 39.35 ----三级分支 Amount {} 39.35 ----三级分支 TaxRate {} 0.030000 ----三级分支 ComTaxAm {} 1.18 ----三级分支 TotaltaxIncludedAmount {} 40.53 ----三级分支 TaxClassificationCode {} 3010101020203000000 --二级分支 IssuItemInformation {} None ----三级分支 ItemName {} *交通运输服务*客运服务费 ----三级分支 Amount {} -0.96 ----三级分支 TaxRate {} 0.030000 ----三级分支 ComTaxAm {} -0.03 ----三级分支 TotaltaxIncludedAmount {} -0.99 ----三级分支 TaxClassificationCode {} 3010101020203000000 --二级分支 SpecificInformation {} None --二级分支 AdditionalInformation {} None ----三级分支 Remark {} None 一级分支 TaxSupervisionInfo {} --二级分支 InvoiceNumber {} 26327000001329568432 --二级分支 IssueTime {} 2026-07-18 --二级分支 TaxBureauCode {} 13200000000 --二级分支 TaxBureauName {} 国家税务总局江苏省税务局 一级分支 ptbh {}寻找标签数据root.findall()仅查找标记为当前元素直接子级的元素root.findall(.//ItemName)[Element ItemName at 0x10c60aac0, Element ItemName at 0x10c60aa20]root.findall(.//ItemName)[0].text*交通运输服务*客运服务费root.findall(.//ItemName)[1].text*交通运输服务*客运服务费root.findall(.//IssuItemInformation/ItemName)[0].text*交通运输服务*客运服务费root.find()找到 第一 带有特定标签的子项root.find(.//TotalTax-includedAmount).text39.54完整脚本代码importxml.etree.ElementTreeasETfromopenpyxlimportWorkbookfromtqdmimporttqdm# 进度条工具importosfromdatetimeimportdatetime wbWorkbook()wswb.active nowdatetime.now()nownow.strftime(%Y-%m-%d %H-%M-%S)print(当前日期和时间,now)# 获取当前工作目录的完整路径current_directory_pathos.getcwd()# 获取当前工作目录的名称current_directory_nameos.path.basename(current_directory_path)print(当前工作目录的完整路径:,current_directory_path)print(当前工作目录的名称:,current_directory_name)filenamesos.listdir(.)path_files[]forfilenameinfilenames:filenamefilename.split(.)iffilename[-1]xml:filenamestr.join(.,filename)path_file./filename path_files.append(path_file)print(f文件夹下总共{len(path_files)}个XML发票文件)china_name[文件名,文件类型,发票号码,开票时间,税务局代码,税务局名称,票据编号,购买方名称,购买方税号,销售方名称,销售方税号,销售方电话,销售方开户银行,销售方开会银行账号,项目名称,规格型号,单位,数量,单价,金额,税率/征收率,税额,价税合计小写,价税合计大写]foriinrange(len(china_name)):ws.cell(row1,columni1).valuechina_name[i]defparse_xml_invoice(xml_path):treeET.parse(xml_path)roottree.getroot()# 提取关键字段try:InvoiceNumberroot.find(.//InvoiceNumber).textexceptExceptionase:InvoiceNumber未提取成功print(f文件{xml_path}遇到 InvoiceNumber 参数报错{e})try:IssueTimeroot.find(.//IssueTime).textexceptExceptionase:IssueTime未提取成功print(f文件{xml_path}遇到 IssueTime 参数报错{e})try:TaxBureauCoderoot.find(.//TaxBureauCode).textexceptExceptionase:TaxBureauCode未提取成功print(f文件{xml_path}遇到 TaxBureauCode 参数报错{e})try:TaxBureauNameroot.find(.//TaxBureauName).textexceptExceptionase:TaxBureauName未提取成功print(f文件{xml_path}遇到 TaxBureauName 参数报错{e})try:ptbhroot.find(.//ptbh).textexceptExceptionase:ptbh未提取成功print(f文件{xml_path}遇到 ptbh 参数报错{e})try:BuyerNameroot.find(.//BuyerName).textexceptExceptionase:BuyerName未提取成功print(f文件{xml_path}遇到 BuyerName 参数报错{e})try:BuyerIdNumroot.find(.//BuyerIdNum).textexceptExceptionase:BuyerIdNum未提取成功print(f文件{xml_path}遇到 BuyerIdNum 参数报错{e})try:SellerNameroot.find(.//SellerName).textexceptExceptionase:SellerName未提取成功print(f文件{xml_path}遇到 SellerName 参数报错{e})try:SellerIdNumroot.find(.//SellerIdNum).textexceptExceptionase:SellerIdNum未提取成功print(f文件{xml_path}遇到 SellerIdNum 参数报错{e})try:SellerTelNumroot.find(.//SellerTelNum).textexceptExceptionase:SellerTelNum未提取成功print(f文件{xml_path}遇到 SellerTelNum 参数报错{e})try:SellerBankNameroot.find(.//SellerBankName).textexceptExceptionase:SellerBankName未提取成功print(f文件{xml_path}遇到 SellerBankName 参数报错{e})try:SellerBankAccNumroot.find(.//SellerBankAccNum).textexceptExceptionase:SellerBankAccNum未提取成功print(f文件{xml_path}遇到 SellerBankAccNum 参数报错{e})## 判断项目名目有几行try:IssuItemInformation_listroot.findall(.//IssuItemInformation)iflen(IssuItemInformation_list)2:ItemName多个项目目录,未提取SpecMod多个项目目录,未提取MeaUnits多个项目目录,未提取Quantity多个项目目录,未提取UnPrice多个项目目录,未提取Amount多个项目目录,未提取TaxRate多个项目目录,未提取ComTaxAm多个项目目录,未提取else:try:ItemNameroot.find(.//ItemName).textexceptExceptionase:ItemName未提取成功print(f文件{xml_path}遇到 ItemName 参数报错{e})try:SpecModroot.find(.//SpecMod).textexceptExceptionase:SpecMod未提取成功print(f文件{xml_path}遇到 SpecMod 参数报错{e})try:MeaUnitsroot.find(.//MeaUnits).textexceptExceptionase:MeaUnits未提取成功print(f文件{xml_path}遇到 MeaUnits 参数报错{e})try:Quantityroot.find(.//Quantity).textexceptExceptionase:Quantity未提取成功print(f文件{xml_path}遇到 Quantity 参数报错{e})try:UnPriceroot.find(.//UnPrice).textexceptExceptionase:UnPrice未提取成功print(f文件{xml_path}遇到 UnPrice 参数报错{e})try:Amountroot.find(.//Amount).textexceptExceptionase:Amount未提取成功print(f文件{xml_path}遇到 Amount 参数报错{e})try:TaxRateroot.find(.//TaxRate).textexceptExceptionase:TaxRate未提取成功print(f文件{xml_path}遇到 TaxRate 参数报错{e})try:ComTaxAmroot.find(.//ComTaxAm).textexceptExceptionase:ComTaxAm未提取成功print(f文件{xml_path}遇到 ComTaxAm 参数报错{e})exceptExceptionase:print(f文件{xml_path}遇到 IssuItemInformation 分支 参数报错{e})try:TotalTax_includedAmountroot.find(.//TotalTax-includedAmount).textexceptExceptionase:TotalTax_includedAmount未提取成功print(f文件{xml_path}遇到 TotalTax_includedAmount 参数报错{e})try:TotalTax_includedAmountInChineseroot.find(.//TotalTax-includedAmountInChinese).textexceptExceptionase:TotalTax_includedAmountInChinese未提取成功print(f文件{xml_path}遇到 TotalTax_includedAmountInChinese 参数报错{e})context{}context{文件名:xml_path,文件类型:XML,发票号码:InvoiceNumber,开票时间:IssueTime,税务局代码:TaxBureauCode,税务局名称:TaxBureauName,票据编号:ptbh,购买方名称:BuyerName,购买方税号:BuyerIdNum,销售方名称:SellerName,销售方税号:SellerIdNum,销售方电话:SellerTelNum,销售方开户银行:SellerBankName,销售方开会银行账号:SellerBankAccNum,项目名称:ItemName,规格型号:SpecMod,单位:MeaUnits,数量:Quantity,单价:UnPrice,金额:Amount,税率/征收率:TaxRate,税额:ComTaxAm,价税合计小写:TotalTax_includedAmount,价税合计大写:TotalTax_includedAmountInChinese}returncontext i2forpath_fileinpath_files:xml_pathpath_file contextsparse_xml_invoice(xml_path)ws.cell(rowi,column1).valuecontexts[文件名]ws.cell(rowi,column2).valuecontexts[文件类型]ws.cell(rowi,column3).valuecontexts[发票号码]ws.cell(rowi,column4).valuecontexts[开票时间]ws.cell(rowi,column5).valuecontexts[税务局代码]ws.cell(rowi,column6).valuecontexts[税务局名称]ws.cell(rowi,column7).valuecontexts[票据编号]ws.cell(rowi,column8).valuecontexts[购买方名称]ws.cell(rowi,column9).valuecontexts[购买方税号]ws.cell(rowi,column10).valuecontexts[销售方名称]ws.cell(rowi,column11).valuecontexts[销售方税号]ws.cell(rowi,column12).valuecontexts[销售方电话]ws.cell(rowi,column13).valuecontexts[销售方开户银行]ws.cell(rowi,column14).valuecontexts[销售方开会银行账号]ws.cell(rowi,column15).valuecontexts[项目名称]ws.cell(rowi,column16).valuecontexts[规格型号]ws.cell(rowi,column17).valuecontexts[单位]ws.cell(rowi,column18).valuecontexts[数量]ws.cell(rowi,column19).valuecontexts[单价]ws.cell(rowi,column20).valuecontexts[金额]ws.cell(rowi,column21).valuecontexts[税率/征收率]ws.cell(rowi,column22).valuecontexts[税额]ws.cell(rowi,column23).valuecontexts[价税合计小写]ws.cell(rowi,column24).valuecontexts[价税合计大写]ii1wb.save(f./{current_directory_name}_发票汇总_{str(now)}.xlsx)print(Finish)