一个python 脚本将XML文件转换到excel
2014-04-08 19:00
627 查看
需要下载一个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)
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)
相关文章推荐
- python脚本11——.strings文件与excel互转、 xml文件与excel互转
- 一个Excel转换为Json格式的Python脚本
- 一个Excel转换为Json格式的Python脚本
- 分享一个简单的python读写文件脚本
- Python 实现一个小功能: 删除某路径下文件及文件夹的脚本
- 利用Python将多个excel文件合并为一个文件
- Python将多个excel文件合并为一个文件
- 用python生成一个导出数据库的bat脚本文件
- python读取csv文件并把文件放入一个list中脚本实例
- 一个获取指定目录下一定格式的文件名称和文件修改时间并保存为文件的python脚本
- 8. Python脚本学习实战笔记八 使用XML-RPC进行文件共享
- unix/windows下编写一个python脚本(文件备份)--python学习(4)
- 利用python生成一个导出数据库的bat脚本文件
- python 将excel转换成字典,并且将字典写到txt文件里
- 一个开发中的用来格式化.c .h头文件格式的python自用脚本(python3.1欢迎试用)
- 一个hex文件与bin文件相互转换的python代码
- 一个简单的python读写文件脚本
- 将excel表中的数据写到txt文本文件的Python脚本
- 使用一个简单的python脚本将一个本地文件以码流的形式,通过UDP协议发送到对端:
- 使用python脚本下载www.wuxia.net.cn上的书籍,并且将它们合并成一个文件