import xlrd from datetime import datetime from xlrd import xldate_as_tuple
defread_excel(filename): # filename=os.path.basename(path) if filename.endswith('xls'): worksheet = xlrd.open_workbook(filename) sheet = worksheet.sheet_by_index(0) rows = sheet.nrows cols = sheet.ncols data = [] head = sheet.row_values(0) print('head=',head) data.append(head) for i in range(1,rows): row = sheet.row_values(i) print('row=',row) for j in range(cols): cell=sheet.cell_value(i,j) ctype=sheet.cell(i,j).ctype # if ctype == 3: # print('cell',cell) # print(cols,len(row)) if row: app = {} for j in range(len(row)): ctype=sheet.cell(i,j).ctype if ctype == 3: date = datetime(*xldate_as_tuple(row[j], 0)) row[j]=date.strftime('%Y-%m-%d') print(i,j) print('type,value:',sheet.cell(i,j).ctype,row[j]) app[head[j]] = row[j] data.append(app) elif filename.endswith('xlsx'): worksheet = load_workbook(path) sheets = worksheet.get_sheet_names() sheet = worksheet.get_sheet_by_name(sheets[0]) rows = sheet.rows columns = sheet.columns data = [] head = [col.value for col in rows[0]] data.append(head) for row in rows: app = {} if row: for i in range(len(row)): app[head[i]] = row[i].value data.append(app) # data.remove(data[0]) # print('data=',data) return data