import xlrd
from xlutils.copy import copy
class check:
def __init__(self, path, casesheet,beginnum):
self.path = path
self.casesheet = casesheet
self.beginnum=beginnum
def check_caselist(self):
wookbook = xlrd.open_workbook(self.path,formatting_info=True)
#获取用例所在的工作表
casesheet = wookbook.sheet_by_name(self.casesheet)
#复制一份用来记录校验结果,保证不影响源用例文件
wbook = copy(wookbook)
#副本内创建工作表"result"
result = wbook.add_sheet('result')
#创建一个工作表写对比结果
# 获取行数
nRows = casesheet.nrows
ncols = casesheet.ncols
ture=0
false=0
for x in range(1,nRows):
#行列表
row_case=[]
for y in range(self.beginnum,ncols):
data=casesheet.cell(x,y).value
ls=data.split('\n')
num=len(ls)
row_case.append(ls)
result.write(x, y, label=num)
if len(row_case[0])==len(row_case[1])==len(row_case[2]):
result.write(x, ncols, label='ture')
#统计行数一样的case数量
ture=ture+1
else:
result.write(x, ncols, label='false')
#统计行数不一样的case数量
false=false+1
new_file="check_"+self.path
wbook.save(new_file)
print(f"全部校验完成:总数={nRows-1},ture={ture},false={false}")
if __name__ == '__main__':
#用例文件地址
path = r'testcase.xls'
#用例数据所在的工作表名称
sheename = 'Sheet1'
#需要校验的起始列号-要求步骤、数据、期望三列
beginnum=4
a = check(path, sheename,beginnum)
a.check_caselist()
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.
45.
46.
47.
48.
49.
50.
51.
校验结果:
读取excel:
import xlrd
class read_data():
def __init__(self,path,sheetname):
self.path=path
self.sheetname=sheetname
def get_caselist(self):
# path= r'filems_文件生命周期.xls'
wookbook = xlrd.open_workbook(self.path,formatting_info=True)
sheetname=wookbook.sheet_names()
sheet1=wookbook.sheet_by_name(self.sheetname)
#获取行数
nRows=sheet1.nrows
ncols=sheet1.ncols
#获取单元格所在的数据
caselist=[]
for x in range(1,nRows):
row_case=[]
for y in range(0,ncols):
data=sheet1.cell(x,y).value
if y in (4, 5, 6,7):
if '\n' in str(data):
print('当前data数据:')
print(data)
ls=data.split('\n')
#去除空列表
if len(list(filter(None,ls))):
row_case.append(ls)
else:
row_case.append(str(data))
else:
row_case.append(data)
# 去除空列表
if len(list(filter(None,row_case))):
caselist.append(row_case)
print("用例集合》》》")
print(len(caselist))
print(caselist)
return caselist
if __name__ == '__main__':
path = r'test2022.xls'
sheename='Sheet1'
a=read_data(path,sheename)
a.get_caselist()