Python测试用例生成脚本(合并相应单元格以及写入单行数据)代码实例
2017-05-10 17:26
1531 查看
#coding=utf8 ''' 把buffer中的信息,写入到excel中。 并按照要求,构造表格样式 ''' #导入readCSV模块,用来获取buffer数据 from readCSV import readCSV #导入写excel的操作库 import xlwt class 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()
相关文章推荐
- Python测试用例生成脚本(合并相应单元格以及写入单行数据)代码实例
- Python对excel写入数据操作实例代码(改进无法合并最后一组数据的问题)
- selenium+python关于登录的脚本代码,使用了读取excel以及向excel中写入测试结果的方法
- selenium+python关于登录的脚本代码,使用了读取excel以及向excel中写入测试结果的方法
- Python对excel写入数据操作实例代码(改进无法合并最后一组数据的问题)
- Python对excel写入数据操作实例代码(改进无法合并最后一组数据的问题)
- Python对excel写入数据操作实例代码(改进无法合并最后一组数据的问题)
- Python对excel写入数据操作实例代码(只供参考)
- Python对excel写入数据操作实例代码(只供参考)
- python脚本――生成EXCEL,连接数据库,并将指定数据写入EXCEL
- 将C++代码全部写到头文件:)python脚本帮助自动生成相应的实现文件初始框架
- 详解python脚本自动生成需要文件实例代码
- python3生成测试数据,并写入ssdb
- Python对excel写入数据操作实例代码(只供参考)
- Python对excel写入数据操作实例代码(只供参考)
- Python 脚本生成测试数据,Python生成随机数据,Python生成大量数据保存到文件夹中
- 利用Python爬取微博数据生成词云图片实例代码
- 根据开发提供的svn更新版本号从开发分支合并代码到测试分支工作目录的部分脚本
- 非常实用的Junit3与Junit4 测试 以及两者平滑过渡(高兼容性)实例代码
- 用java代码实现从excel表格读取数据然后写入生成国际化配置文件properties