import xlrd from xlutils.copy import copy class ExcelUtil: def __init__(self,excel_path=None,index=None): if excel_path == None: excel_path = 'C:\Users\Administrator\Desktop\ooo.xls' if index == None: index = 0 self.data = xlrd.open_workbook(excel_path) self.table = self.data.sheets()[index] self.read_value = self.data self.write_data = copy(self.data) #按照每行的List,添加到一个大的List里面 def get_data(self): ddt_list = [] rows = self.get_lines() if rows!=None: for i in range(rows): col = self.table.row_values(i) ddt_list.append(col) return ddt_list return None def get_lines(self):#获取excel的行数 rows = self.table.nrows if rows>=1: return rows return None def get_col_value(self,row,col):#获取单元格的数据 if self.get_lines()>row: data = self.table.cell(row,col).value return data return None def write_value(self,row,value):#写入数据 self.write_data.get_sheet(0).write(row,2,value) #2是要写入的列数,从0开始的 self.write_data.save('C:\Users\Administrator\Desktop\ooo.xls') if __name__ == '__main__': a = ExcelUtil() zonghanshu=a.get_lines() for i in range(1,zonghanshu): A = str(a.get_col_value(i,1)) suanfa = str(a.get_col_value(i,0)).replace('操作货量',A) #读取excel算法,替换其中的参数。 jieguo=round(eval(suanfa),2)#round 结果保留两位小数,eval 计算 a.write_value(i,jieguo)