参考:https://www.cnblogs.com/xiao-apple36/p/9603499.html#_label2
写入模块:xlwt
安装方法:pip install xlwt
#打开xls表
def Openfile(filename):
file_d = xlrd.open_workbook(filename)
return file_d
#获取表的sheets数量
def Sheetnums(filename):
return len(xlrd.open_workbook(filename).sheets())
#获取表中sheets名称,返回值为由sheetname组成的list
def Sheetname(filename):
return xlrd.open_workbook(filename).sheet_names()
#计算表中某一行或列的值
def GetPriority(filename,sheenum,linenum):
select_sheet = xlrd.open_workbook(filename).sheets()[sheenum-1]
读取模块:xlrd
安装方法:pip install xlrd
def CreateSheet(linenum,sheetname,list):
'''
功能:创建一个sheet并根据linenum写入一行值
:param linenum: shee中写入的行号
:param sheetname: 创建的sheet名称
:param list: 写入的值,为list类型
'''
# 创建一个excel对象
workBook = xlwt.Workbook(encoding='utf-8')
# 创建一个sheet,并写入行
workSheet = workBook.add_sheet(sheetname)
for one in range(0, len(list)):
workSheet.write(linenum, one, list[one])
return workSheet
举例:
if __name__ == '__main__':
filename = 'SMC产品测试用例.xls'
file_d = Openfile(filename)
print(len(file_d.sheets())) #获取表的sheets数量
print(file_d.sheet_names()) #获取表中sheets名称,返回值为由sheetname组成的list
select_sheet = xlrd.open_workbook(filename).sheets()[2] #获取第三个sheet表格
rownum = select_sheet.nrows #计算第三个sheet表格的行数
# print(rownum)
workBook = xlwt.Workbook(encoding='utf-8') #创建一个excel对象
workSheet = workBook.add_sheet('用例优先级') #创建一个sheet列,并命名为用例优先级
for row in range(0,rownum):
value1 = select_sheet.cell_value(row, 4) #依次获取第5列中的元素值
value2 = select_sheet.cell_value(row, 8) # 依次获取第9列中的元素值
valuelist = [value1,value2]
for one in range(len(valuelist)):
workSheet.write(row, one, valuelist[one]) #依次将获取到的第5列和第9列的元素值放到优先级sheet中
workBook.save('统计结果.xls')