selenium 和 接口自动化测试时,使用xlutils模块,读取excel 并且 追加excel内容 形成一个新的excel结果

在学习自动化的过程中。使用excel管理测试用例,读取excel的内容和追加内容的方法。
#coding:utf-8
import xlrd
import os
import xlwt
from xlutils.copy import copy

class ExcelUtil():
def __init__(self,excelPath,sheetName):
#excel路径
self.data = ExcelUtil.__getExcel(excelPath)
#sheet
self.table = self.data.sheet_by_name(sheetName)
#获取第一行作为key值
self.keys = self.table.row_values(0)
#获取总行数
self.rowNum = self.table.nrows
#获取总列数
self.colNum = self.table.ncols
@staticmethod
def __getExcel(excelPath):
if not os.path.exists(excelPath):
return "not excel file"
else:
filepath = xlrd.open_workbook(excelPath)
return filepath
def dict_data(self):
if self.rowNum <=1:
print "总行数小于1"
else:
r = []
j=1
for i in range(self.rowNum-1):
s = {}
#从第二行取对应values值
values = self.table.row_values(j)
for x in range(self.colNum):
s[self.keys[x]] = values[x]
r.append(s)
j+=1
return r
def ExcelData():
if not os.path.exists(os.path.join("localhost","conf","test.xls")):
print "not test.xlsx"
else:
filepath = os.path.join("localhost","conf","test.xls")
sheetName = "Sheet1"
data = ExcelUtil(filepath,sheetName)
return data.dict_data()

def Inputexcel(nrow,ncol,result):
filepath = os.path.join("localhost","conf","test.xls")
sheetName = "Sheet1" #excel的sheetname
data = xlrd.open_workbook(filepath) #打开原excel的文件
newdate = copy(data)
newWS = newdate.get_sheet(newdate.sheet_index(sheetName))
newWS.write(nrow,ncol,result)
filepath1 = os.path.join("localhost","conf","testresult.xls")
newdate.save(filepath1) #将新文件另存为
return "Done"
if __name__=="__main__":
print "1"

需要

pip install xlrd  

pip install xlutils

 读取的结果,悠老提供,观看过的人,自己去了解,怎么使用吧!不动手无法记住内容的

 

追加excel内容

 追加前

 

 

追加后

 

posted @ 2018-03-07 08:56  小~yytt~  阅读(176)  评论(0编辑  收藏  举报