1 from openpyxl import load_workbook 2 class RwExcelFile: 3 4 def read_Excel(self,file_path): 5 ''' 6 读取excel中所有数据并以列表形式返回 7 :param file_path: 8 :return: 9 ''' 10 excel_File = load_workbook(file_path) 11 sheet_names = excel_File.sheetnames 12 list_column = [] 13 list_row = [] 14 list_sheet = [] 15 for i in sheet_names: 16 excel_File_sheet=excel_File[i] 17 for j in range(1,excel_File_sheet.max_row+1): 18 for k in range(1,excel_File_sheet.max_column+1): 19 list_column.append(excel_File_sheet.cell(j,k).value) 20 list_row.append(list_column) 21 list_column = [] 22 list_sheet.append(list_row) 23 list_row = [] 24 excel_File.close() 25 return list_sheet 26 27 def write_Excel(self,file_path,sheetname,row,cloumn,T_value): 28 ''' 29 向excel指定位置写入值 30 :param file_path: 文件地址 31 :param sheetname: sheet名 32 :param row: 行 33 :param cloumn:列 34 :param T_value: 值 35 :return: 无返回 36 ''' 37 excel_File = load_workbook(file_path) 38 excel_File[sheetname].cell(row,cloumn,T_value) 39 excel_File.save(file_path) 40 excel_File.close()