import openpyxl #导入模块 class DoExcel: def doexcel(self,filename,sheet_name): wb=openpyxl.load_workbook(filename) sheet=wb[sheet_name] # value=sheet.cell(1,1).value #row指行,column指列,max_row最大行,max_column最大列 test_data=[] for item in range(2,sheet.max_row+1): get_data= {} get_data["case_id"]=sheet.cell(item,1).value get_data["url"]=sheet.cell(item,2).value get_data["headers"]=sheet.cell(item,3).value get_data["data"]=sheet.cell(item,4).value get_data["method"]=sheet.cell(item,5).value get_data["title"] = sheet.cell(item, 6).value test_data.append(get_data) return test_data def write_back(self,filename,sheet_name,item,value): #将测试结果写回excel wb = openpyxl.load_workbook(filename) sheet = wb[sheet_name] sheet.cell(item, 7).value = value wb.save(filename) if __name__ == '__main__': print(DoExcel().doexcel("../test_data/test_data.xlsx","login"))
运行结果: