def excel_info(self, paths, types, sheetname=None): '''讀取每行每列的信息 :param paths 文件路徑 :param types primary:取第一個sheet內容 field:取字段表內容 ''' excel = load_workbook(paths) if types == 'primary': num = excel.get_sheet_names() # 獲取sheet: table = excel.get_sheet_by_name(num[0]) if types == 'field': if sheetname == None: print('sheet empty') return False table = excel[sheetname] # 經過表名獲取 # 獲取行數和列數: rows = table.max_row # 獲取行數 cols = table.max_column # 獲取列數 # 獲取單元格值: s = [] j = 2 for x in range(rows - 1): f = {} for i in range(1, cols + 1): value = table.cell(row=j, column=i).value # 從第二行開始,獲取每一列的值 # if value == None: # value = '' dict_value = table.cell(row=1, column=i).value # 取到第一行每列的key值 f[dict_value] = value s.append(f) j += 1
注:primary讀取第一個sheet,根據第一個sheet關聯讀取相關sheet表單python