一个python 脚本将XML文件转换到excel

需要下载一个module:xlwt,如下是source code

 

 

import xml.dom.minidom
import xlwt
import sys

col = 0
row = 0   


def handle_xml_report(xml_report, excel):   
    problems = xml_report.getElementsByTagName("problem")
    handle_problems(problems, excel)
   

def handle_problems(problems, excel):
    for problem in problems:
        handle_problem(problem, excel)


def handle_problem(problem, excel):
    global row
    global col
    code = problem.getElementsByTagName("code")   
    file = problem.getElementsByTagName("file")   
    line = problem.getElementsByTagName("line")   
    message   = problem.getElementsByTagName("message")

    for node in code:   
        excel.write(row, col, node.firstChild.data)
        col = col + 1
    for node in file:   
        excel.write(row, col, node.firstChild.data)
        col = col + 1       
    for node in line:   
        excel.write(row, col, node.firstChild.data)        
        col = col + 1       
    for node in message:   
        excel.write(row, col, node.firstChild.data)        
        col = col + 1
    row = row+1
    col = 0

if __name__ == '__main__':
    if(len(sys.argv) <= 1):
        print ("usage: xml2xls src_file [dst_file]")
        exit(0)
    #the 1st argument is XML report ; the 2nd is XLS report
    if(len(sys.argv) == 2):
        xls_report = sys.argv[1][:-3] + 'xls'
    #if there are more than 2 arguments, only the 1st & 2nd make sense
    else:
        xls_report = sys.argv[2]
    xmldoc = xml.dom.minidom.parse(sys.argv[1])
    wb = xlwt.Workbook()
    ws = wb.add_sheet('MOLint')
    ws.write(row, col, 'Error Code')
    col = col + 1
    ws.write(row, col, 'file')
    col = col + 1   
    ws.write(row, col, 'line')   
    col = col + 1   
    ws.write(row, col, 'Description')
    row = row + 1
    col = 0
    handle_xml_report(xmldoc, ws)
    wb.save(xls_report)

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值