热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

Python实现将xml导入至excel

本文给大家讲解的是使用Python的Testlink实现将实现将xml导入至excel表格中,方法非常的简单,另外附上其他小伙伴的方法,有需要的童鞋们可以参考下。
最近在使用Testlink时,发现导入的用例是xml格式,且没有合适的工具转成excel格式,xml使用excel打开显示的东西也太多,网上也有相关工具转成csv格式的,结果也不合人意。

那求人不如尔己,自己写一个吧

需要用到的模块有:xml.dom.minidom(python自带)、xlwt

使用版本:

python:2.7.5

xlwt:1.0.0

一、先分析Testlink XML格式:

这是一个有两级testusuit的典型的testlink用例结构,我们只需要取testsuite name,testcase name,preconditions,actions,expectedresults

二、程序如下:

#coding:utf-8
'''
Created on 2015-8-20

@author: Administrator
'''
'''
'''
import xml.etree.cElementTree as ET
import xml.dom.minidom as xx
import os,xlwt,datetime

workbook=xlwt.Workbook(encoding="utf-8")
# 
booksheet=workbook.add_sheet(u'sheet_1')
booksheet.col(0).width= 5120
booksheet.col(1).width= 5120
booksheet.col(2).width= 5120
booksheet.col(3).width= 5120
booksheet.col(4).width= 5120
booksheet.col(5).width= 5120

dom=xx.parse(r'D:\\Python27\test.xml')
root = dom.documentElement
row=1
col=1

borders=xlwt.Borders()
borders.left=1
borders.right=1
borders.top=1
borders.bottom=1


style = xlwt.easyxf('align: wrap on,vert centre, horiz center') #自动换行、水平居中、垂直居中
#设置标题的格式,字体方宋、加粗、背景色:菊黄
#测试项的标题

title=xlwt.easyxf(u'font:name 仿宋,height 240 ,colour_index black, bold on, italic off; align: wrap on, vert centre, horiz center;pattern: pattern solid, fore_colour light_orange;')
item='测试项'
Subitem='测试分项'
CaseTitle='测试用例标题'
COndition='预置条件'
actiOns='操作步骤'
Result='预期结果'
booksheet.write(0,0,item,title)
booksheet.write(0,1,Subitem,title)
booksheet.write(0,2,CaseTitle,title)
booksheet.write(0,3,Condition,title)
booksheet.write(0,4,actions,title)
booksheet.write(0,5,Result,title)
#冻结首行
booksheet.panes_frozen=True
booksheet.horz_split_pos= 1


#一级目录
for i in root.childNodes:
  testsuite=i.getAttribute('name').strip()
  #print testsuite
  #print testsuite
  '''
  写测试项
  '''
  print "row is :",row
  booksheet.write(row,col,testsuite,style)
  

  #二级目录
  for dd in i.childNodes:
    print "    %s" % dd.getAttribute('name')
    testsuite2=dd.getAttribute('name')
    if not dd.getElementsByTagName('testcase'):
      print "Testcase is %s" % testsuite2
      row=row+1
      booksheet.write(row,2,testsuite2,style)  #写测试分项
    
    row=row+1
    
    booksheet.write(row,1,testsuite2,style)
    itemlist=dd.getElementsByTagName('testcase')
    
    for subb in itemlist:
      #print "         %s" % subb.getAttribute('name')
      testcase=subb.getAttribute('name')
      
      row=row+1
      booksheet.write(row,2,testcase,style)

      ilist=subb.getElementsByTagName('preconditions')
      for ii in ilist:
        precOnditions=ii.firstChild.data.replace("
"," ") col=col+1 booksheet.write(row,3,preconditions,style) steplist=subb.getElementsByTagName('actions') #print steplist for step in steplist: actiOns=step.firstChild.data.replace("
"," ") col=col+1 booksheet.write(row,4,actions,style) #print "测试步骤:",steplist[0].firstChild.data.replace("
"," ") expectlist=subb.getElementsByTagName('expectedresults') for expect in expectlist: result=expect.childNodes[0].nodeValue.replace("
","" ) booksheet.write(row,5,result,style) row=row+1 workbook.save('demo.xls')

写入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)


推荐阅读
  • Python 异步编程:深入理解 asyncio 库(上)
    本文介绍了 Python 3.4 版本引入的标准库 asyncio,该库为异步 IO 提供了强大的支持。我们将探讨为什么需要 asyncio,以及它如何简化并发编程的复杂性,并详细介绍其核心概念和使用方法。 ... [详细]
  • Android 九宫格布局详解及实现:人人网应用示例
    本文深入探讨了人人网Android应用中独特的九宫格布局设计,解析其背后的GridView实现原理,并提供详细的代码示例。这种布局方式不仅美观大方,而且在现代Android应用中较为少见,值得开发者借鉴。 ... [详细]
  • QBlog开源博客系统:Page_Load生命周期与参数传递优化(第四部分)
    本教程将深入探讨QBlog开源博客系统的Page_Load生命周期,并介绍一种简洁的参数传递重构方法。通过视频演示和详细讲解,帮助开发者更好地理解和应用这些技术。 ... [详细]
  • PyCharm下载与安装指南
    本文详细介绍如何从官方渠道下载并安装PyCharm集成开发环境(IDE),涵盖Windows、macOS和Linux系统,同时提供详细的安装步骤及配置建议。 ... [详细]
  • 本文详细介绍如何使用Python进行配置文件的读写操作,涵盖常见的配置文件格式(如INI、JSON、TOML和YAML),并提供具体的代码示例。 ... [详细]
  • 技术分享:从动态网站提取站点密钥的解决方案
    本文探讨了如何从动态网站中提取站点密钥,特别是针对验证码(reCAPTCHA)的处理方法。通过结合Selenium和requests库,提供了详细的代码示例和优化建议。 ... [详细]
  • python的交互模式怎么输出名文汉字[python常见问题]
    在命令行模式下敲命令python,就看到类似如下的一堆文本输出,然后就进入到Python交互模式,它的提示符是>>>,此时我们可以使用print() ... [详细]
  • 深入理解Tornado模板系统
    本文详细介绍了Tornado框架中模板系统的使用方法。Tornado自带的轻量级、高效且灵活的模板语言位于tornado.template模块,支持嵌入Python代码片段,帮助开发者快速构建动态网页。 ... [详细]
  • 1.如何在运行状态查看源代码?查看函数的源代码,我们通常会使用IDE来完成。比如在PyCharm中,你可以Ctrl+鼠标点击进入函数的源代码。那如果没有IDE呢?当我们想使用一个函 ... [详细]
  • 本文介绍了在安装或运行 Python 项目时遇到的 'ModuleNotFoundError: No module named setuptools_rust' 错误,并提供了解决方案。 ... [详细]
  • CentOS7源码编译安装MySQL5.6
    2019独角兽企业重金招聘Python工程师标准一、先在cmake官网下个最新的cmake源码包cmake官网:https:www.cmake.org如此时最新 ... [详细]
  • 如何在PHPcms网站中添加广告
    本文详细介绍了在PHPcms网站后台添加广告的方法,涵盖多种常见的广告形式,如百度广告和Google广告,并提供了相关设置的步骤。同时,文章还探讨了优化网站流量的SEO策略。 ... [详细]
  • 本文详细介绍了如何使用Python编写爬虫程序,从豆瓣电影Top250页面抓取电影信息。文章涵盖了从基础的网页请求到处理反爬虫机制,再到多页数据抓取的全过程,并提供了完整的代码示例。 ... [详细]
  • 本文介绍如何使用 Python 编写程序,检查给定列表中的元素是否形成交替峰值模式。我们将探讨两种不同的方法来实现这一目标,并提供详细的代码示例。 ... [详细]
  • 本文将介绍由密歇根大学Charles Severance教授主讲的顶级Python入门系列课程,该课程广受好评,被誉为Python学习的最佳选择。通过生动有趣的教学方式,帮助初学者轻松掌握编程基础。 ... [详细]
author-avatar
大男孩小男淫
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有