自动化测试时,有时候会需要将测试结果数据写入到excel中进行记录,robot的第三方库ExcelLibrary中有如下几个关键字,
但是我按照帮助中的实例进行操作,总是会报错,提示“IndexError: list index out of range”
目前还未找到解决方法,如果有热心的网友刚好解决了该问题,可以给我留言哦
我这里暂时只好通过自写库解决写入excel文件问题。
WriteExcel.py如下:
#-*- coding: utf8 -*-
from os.path import join
from xlrd import open_workbook
from xlutils.copy import copy
import os,sys
FilePath1='G:\\pythonCase\\userlogin.xls'
sheetName=None
rb='r+w'
FilePath=FilePath1.replace('\\', '/')
rb=open_workbook(FilePath,'r')
#写数据
def Write_Excel(sheetName,rowIndex,lineIndex,content):
rbook=open_workbook(FilePath,'w')
wb=copy(rbook)
sheetIndex=rbook.sheet_names().index(sheetName)
wb.get_sheet(int(sheetIndex)).write(int(rowIndex),int(lineIndex),content)
wb.save(FilePath)
print 'write file ok'
if __name__ == '__main__':
pass
将该自写库导入到对应的目录中,新建关键字“写入excel”,然后就可以直接调用该关键字使用了。
注:如果该条用例中,同时使用了“读取excel数据”和“写入excel”,并且读取excel数据中打开的excel文件和写入的excel文件为同一个,如下:
执行该条用例就会报错,提示
“IOError: [Errno 22] invalid mode ('w+b') or filename: 'G:/pythonCase/userlogin.xls'”
应该是excel操作权限冲突,两个excel文件改成不一样的就可以运行成功。
关于同时对同一个excel进行读写操作,后面有新收获了再记录。