Python解析Testlink导出的xml并写入excel

发表于:2015-9-10 10:55

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:woailiulihui    来源:51Testing软件测试网采编

  目前测试团队要在Testlink写用例,但为了方便呈现还要同时一份excel用例。如果写两份,工作量就太大了,Testlink虽然有导出功能,但是只能导出xml形式,因此把xml形式转换为excel的艰巨任务就交给我啦!
  下图是其中一个用例集导出的示范用例截图:
  说实话,这个格式比较蛋疼,用python里的Elementtree来解析。关于数据存储格式,想到了每个testcase用字典dic_temp来存,把需要写入excel的项提取出来,由于每个case可能包括很多step,因此每个step的键代表步骤编号,值的话是一个列表list_temp,包括具体步骤和预期结果。
#coding=utf-8
from xml.etree import ElementTree
from win32com.client import Dispatch
import win32com.client
import os
class easy_excel:
def __init__(self,filename=None):
self.xlApp=win32com.client.Dispatch('Excel.Application')
if filename:
self.filename=os.getcwd()+"\\"+filename
print self.filename
self.xlBook=self.xlApp.Workbooks.Open(self.filename)
else:
self.xlBook=self.xlApp.Workbooks.Open(filename)
self.filename=''
def save(self,newfilename=None):
if newfilename:
self.filename=newfilename
self.xlBook.SaveAs(newfilename)
else:
self.xlBook.Save()
def close(self):
self.xlBook.Close(SaveChanges=0)
def getCell(self,sheet,row,col):
sht=self.xlBook.Worksheets(sheet)
return sht.Cell(row,col).Value
def setCell(self,sheet,row,col,value):
sht=self.xlBook.Worksheets(sheet)
sht.Cells(row,col).Value=value
sht.Cells(row,col).HorizontalAlignment=3
sht.Rows(row).WrapText=True
def mergeCells(self,sheet,row1,col1,row2,col2):
sht=self.xlBook.Worksheets(sheet)
sht.Range(sht.Cells(row1,col1),sht.Cells(row2,col2)).Merge()
def setBorder(self,sheet,row,col):
sht=self.xlBook.Worksheets(sheet)
sht.Cells(row,col).Borders.LineStyle=1
def set_col_width(self,sheet):
sht=self.xlBook.Worksheets(sheet)
sht.Columns("D:F").ColumnWidth=30
xls=easy_excel('test.xlsx')
xls.setCell('Sheet1',2,2,u"编号")
xls.setCell('Sheet1',2,3,u"优先级")
xls.setCell('Sheet1',2,4,u"操作步骤")
xls.setCell('Sheet1',2,5,u"预期结果")
xls.setCell('Sheet1',2,6,u"实际结果")
xls.set_col_width('Sheet1')
dic_temp={}
root=ElementTree.parse("./test.xml")
lst_node=root.getiterator("testcase")
num=len(lst_node)
row_flag=3
step_num=0
for case in range(0,num):
steps=lst_node[case].find("steps")
step_num=len(steps)
keywords=lst_node[case].find("keywords")
if keywords is not None:
dic_temp['keyword']=keywords[0].attrib["name"]
else:
dic_temp['keyword']=0
for step in steps:
list_temp=[]
list_temp.append(step[1].text)
list_temp.append(step[2].text)
dic_temp[step[0].text]=list_temp
row_start=row_flag
xls.setCell('Sheet1',row_flag,2,case+1)
xls.setCell('Sheet1',row_flag,3,dic_temp['keyword'])
for j in range(1,step_num+1):
xls.setCell('Sheet1',row_flag,4,dic_temp[str(j)][0])
xls.setCell('Sheet1',row_flag,5,dic_temp[str(j)][1])
row_flag+=1
xls.mergeCells('Sheet1',row_start,3,row_flag-1,3)
xls.mergeCells('Sheet1',row_start,2,row_flag-1,2)
for row in range(2,row_flag):
for col in range(2,7):
xls.setBorder('Sheet1',row,col)
xls.save()
xls.close()
print 'success!'
raw_input("Press any key to continue...")
  写入excel用了win32com,使用它前提是电脑里要有excel软件,网上虽然有教程但是不是很全,设置单元格格式我就试了很久,个人对win32接口不熟,后来发现搜一搜vb操作excel的方法然后依葫芦画瓢,实现了设置单元格格式以及合并单元格。下图是实际效果:
  <p></p>之类的标志是由于导出xml时没做处理,这个可以通过字符串处理的,在此就不赘述了。源码都在附件里,需要自己新建空的test.xlsx。
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号