Python测试用例生成脚本(合并相应单元格以及写入单行数据)代码实例
来源:互联网 发布:网络集线器hub 编辑:程序博客网 时间:2024/06/03 21:35
#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()
0 0
- Python测试用例生成脚本(合并相应单元格以及写入单行数据)代码实例
- Python对excel写入数据操作实例代码(改进无法合并最后一组数据的问题)
- 【python练习】 xlwt 操作excel 写入list数据并考虑合并单元格
- selenium+python关于登录的脚本代码,使用了读取excel以及向excel中写入测试结果的方法
- selenium+python关于登录的脚本代码,使用了读取excel以及向excel中写入测试结果的方法
- 如何将数据依次写入合并单元格区域
- excel中分开的度分秒合并到一个单元格以及转换成相应的度
- word合并单元格后,VBA读取设置相应单元格内容
- python写代码生成脚本
- Python对excel写入数据操作实例代码(只供参考)
- java 基于poi 写入excel 合并单元格
- Python csv写入占多个单元格
- JS生成表格,合并单元格
- datagridview"合并单元格的测试"
- js合并单元格(未测试)
- VBA代码实例---单元格复制以及resize函数介绍
- 数据标准化 使用Python脚本处理excel单元格换行符
- 数据清洗-- 使用Python脚本处理excel单元格换行符
- system()函数
- 多线程同步,多线程数据共享
- pip安装
- 在untiy3d中获取通过父游戏物体得到子游戏物体
- C++ 建造者模式
- Python测试用例生成脚本(合并相应单元格以及写入单行数据)代码实例
- mui开发h5+app过程中页面间参数传递的总结第一篇
- [Spark_API]Transformation-reduceByKey()和aggregateByKey()
- 如何获得高并发的经验
- java对Redis 基本操作
- 软件设计原则(四)依赖倒置原则 -Dependence Inversion Principle
- st 表--求区间最值
- Android面试题-终极解决ViewPager.setCurrentItem中间很多页面切换方案
- 弹框提示