代码实现
1.XlsEngine.py
# -*- coding:utf-8 -*- __author__ = 'yanghaitao' import xlrd import xlwt class XlsEngine_rd(): """ The XlsEngine is a demo class for excel openration Just for some basic test or the using or the 3rd class in python """ def __init__(self,file): # define class variable self.xls_name = file self.xlrd_object = None self.xlwt_object = None self.isopenfailed = True def xlrd_open(self): try: # xlrd.Book.encoding="utf-8" self.xlrd_object = xlrd.open_workbook(self.xls_name) self.isopenfailed = False pass except Exception,e: self.isopenfailed = True self.xlrd_object = None print(e) pass finally: ''' do nothing ''' pass return [self.isopenfailed,self.xlrd_object] def dump_sheet(self): if self.isopenfailed == False: ''' dump the sheet usging for getting the sheet table = data.sheets()[0] table = data.sheet_by_index(0) table = data.sheet_by_name(u'Sheet1') ''' for name in self.xlrd_object.sheet_names(): table = self.xlrd_object.sheet_by_name(name) print("sheet %s rownums=%d colnums=%d"%(name,table.nrows,table.ncols)) else: print("file %s is not open \n"%self.xls_name) def dump_cell(self,sheet_index,cell_row,cell_col): ''' sheet_index cell_row cell_col ''' try: table=self.xlrd_object.sheet_by_index(0) value=table.cell(cell_row,cell_col).value return value pass except: print('dump_cell,error') class XlsEngine_wt(): def __init__(self,file): self.xls_name = file self.xlwt_object = None self.xlwt_sheet = None self.isopenfailed = True def open(self): try: self.xlwt_object = xlwt.Workbook() self.isopenfailed = False except Exception,e: print e def add_sheet(self,sheet_name): ''' Create a sheet to the xls ''' try: self.xlwt_object = xlwt.Workbook() self.xlwt_sheet = self.xlwt_object.add_sheet(sheet_name,cell_overwrite_ok=True) self.xlwt_sheet.write(0,0,'WSDL') self.xlwt_sheet.write(0,1,u'方法') self.xlwt_sheet.write(0,2,'DATA') self.xlwt_sheet.write(0,3,u'预期结果') self.xlwt_sheet.write(0,4,u'执行结果') except Exception,e: print(e) def get_sheet(self,sheet_index): try: self.xlwt_object = xlwt.Workbook() self.xlwt_sheet = self.xlwt_object.get_sheet(sheet_index) except Exception,e: print(e) def write(self,row,col,value): ''' Write value to (row,col) ''' try: self.xlwt_sheet.write(row,col,value) except Exception,e: print(e) def save_xls(self): ''' Save the change to xlsfile ''' try: self.xlwt_object.save(self.xls_name) except Exception,e: print(e) def set_fontColour(self,colour_index): fnt=xlwt.Font() fnt.colour_index=colour_index fnt.bold=True style=xlwt.XFStyle() style.font=fnt return style def write_result(self,row,list,index_list,as_value=True): ''' row 行,list 结果列表,index_list 填写数据列表中的值的索引,wsdl 结果中显示调用接口地址,as_value 预期结果 ''' self.write(row,0,str(list[index_list][0])) self.write(row,1,str(list[index_list][1])) self.write(row,2,str(list[index_list][2])) self.write(row,3,bool(list[index_list][3])) if(bool(list[index_list][3])==as_value): #预期结果是否为True self.xlwt_sheet.write(row,4,str(as_value)+'_Success',self.set_fontColour(3)) #colour_index=3,绿色,通过 else: self.xlwt_sheet.write(row,4,str(as_value)+'_Fail',self.set_fontColour(2)) #colour_index=2,红色,失败 def write_resultRed(self,row,list,index_list,wsdl): self.write(row,0,str(list[index_list][0])) #WSDL地址 self.write(row,1,str(list[index_list][1])) #方法名 self.write(row,2,str(list[index_list][2])) #传入参数(用例) self.write(row,3,bool(list[index_list][3])) #预期结果 self.xlwt_sheet.write(row,4,'Unkonw_Fail',self.set_fontColour(2)) #colour_index=2,红色,失败(执行结果)