Python测试用例生成脚本(合并相应单元格以及写入单行数据)代码实例

网友投稿 308 2022-08-29


Python测试用例生成脚本(合并相应单元格以及写入单行数据)代码实例

#coding=utf8'''把buffer中的信息,写入到excel中。并按照要求,构造表格样式'''#导入readCSV模块,用来获取buffer数据from readCSV import readCSV#导入写excel的操作库import xlwtclass GenTestCase(object): def __init__(self,configName): try: #创建一个readCSV对象 self.dataInfor=readCSV(configName) #创建工作薄 self.wkbook=xlwt.Workbook() #创建表:“requirement” self.dataSheet=self.wkbook.add_sheet("requirement") #把数据按 按照相应格式写入excel表中 self.writeDataToExcel() #保存文件 self.savaExcel() except Exception,e: print "GenTestCase init error:",e def writeDataToExcel(self): try: #埋点测试用例包含的相关属性,即表头 firstLine=[u"埋点事件",u"页面",u"serviceId",u"属性名", u"属性值",u"属性信息",u"埋点规则",u"版本",u"IOS开发",u"安卓开发",u"创建时间",u"测试人员",u"测试状态"] #在末尾添加长度和firstLine长度相同的list表 #该list作用是为了能比较到最后一个事件的数据 endLine=[] #在表的起始行写入表头数据 try: for index in range(len(firstLine)): self.dataSheet.write(0,index,firstLine[index]) endLine.append("") except Exception,e: print "Creat Head Error:",e #把csv中读取的数据赋给变量dataBody dataBody=self.dataInfor.buffer dataBody.append(endLine) #设置个函数,用来记录要合并的起始行 currentrow=1 #对数据进行循环取值,由于第一行的数据不是需要的数据 #所以从第二个元素开始 try: for rowNum in range(1,len(dataBody)): #对于每个子list中的值,依次写入相关单元格 for index in range(len(dataBody[rowNum])): #判断行是否大于1,为以下比较创建条件 if rowNum>1: #把数据的当前行的元素与上一行元素作比较 #如果不相等执行if语句 try: if dataBody[rowNum-1][0]!=dataBody[rowNum][0] and dataBody[-1][0] !=" " : #print currentrow,rowNum #由于存在特殊情况,当前行号要为1 #针对特定的数据规则从和并前三行和后七行 if currentrow==1: for cols in range(3): #获取当前在合并单元格要保存的值 cellValue=dataBody[currentrow][cols] #对单元格的值进行解码 cellValue=cellValue.decode("gbk") #对单元格字符串进行加u #用来解决UnicodeDecodeError data=u"%s" %(cellValue) #合并单元格并写入数据 self.dataSheet.write_merge(currentrow,rowNum-1,cols,cols,data) for cols in range(6,13): cellValue=dataBody[currentrow][cols] cellValue=cellValue.decode("gbk") data=u"%s" %(cellValue) self.dataSheet.write_merge(currentrow,rowNum-1,cols,cols,data) else: if dataBody[rowNum][0]!=dataBody[currentrow][0]: for cols in range(3): cellValue=dataBody[currentrow][cols] cellValue=cellValue.decode("gbk") data=u"%s" %(cellValue) self.dataSheet.write_merge(currentrow-1,rowNum-1,cols,cols,data) for cols in range(6,13): cellValue=dataBody[currentrow][cols] cellValue=cellValue.decode("gbk") data=u"%s" %(cellValue) self.dataSheet.write_merge(currentrow-1,rowNum-1,cols,cols,data) else: #当数据为一条时 for cols in range(3): cellValue=dataBody[rowNum-1][cols] cellValue=cellValue.decode("gbk") data=u"%s" %(cellValue) self.dataSheet.write(rowNum-1,cols,data) for cols in range(6,13): cellValue=dataBody[rowNum-1][cols] cellValue=cellValue.decode("gbk") data=u"%s" %(cellValue) self.dataSheet.write(rowNum-1,cols,data) currentrow=rowNum+1 break except Exception,e: print "Merage cell Error:",e for cols in range(3,6): cellValue=dataBody[rowNum][cols] cellValue=cellValue.decode("gbk") data=u"%s" %(cellValue) self.dataSheet.write(rowNum,cols,data) except Exception,e: print "Write Data Error:",e except Exception,e: print "Write Data TO Excel Error:",e def savaExcel(self): try: savePath=self.dataInfor.fmObj.RequmentPath() self.wkbook.save(savePath) print savePath,"save success..." except Exception,e: print "Save Excel Error:",e def test(): path="E:\PythonDemo\AutonXQL_V1.0\config.xml" GenTestCase(path) if __name__=="__main__": test()


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Python映射类型内建函数与工厂函数操作实例源代码
下一篇:MyBatis连接池的深入和动态SQL详解
相关文章

 发表评论

暂时没有评论,来抢沙发吧~