前提:xlrd获取表格内容,当单元格输入为日期时,python获取的内容不是正确的日期格式,需要单独处理。
wbook=xlrd.open_workbook(file_path)
sheet=wbook.sheet_by_index(num)
#获取表格基础信息
rows = sheet.nrows
cols=sheet.ncols
data_list = []
# """获取每行数据, 组成一个list"""
# for n in range(1, rows):
# values = sheet.row_values(n)
# data_list.append(values)
# print(data_list)
"""判断日期格式的数据,需要特殊处理后返回"""
for row in range(1,rows):
row_data_list = []
for col in range(0,cols):
cell_data=sheet.cell(row,col)
if cell_data.ctype==3:
cell_data=xlrd.xldate.xldate_as_datetime(sheet.cell(row,col).value, 0).strftime("%Y/%m/%d")
else:
cell_data=cell_data.value
row_data_list.append(cell_data)
data_list.append(row_data_list)
print(data_list)
ctype_text = { XL_CELL_EMPTY: 'empty', XL_CELL_TEXT: 'text', XL_CELL_NUMBER: 'number', XL_CELL_DATE: 'xldate', XL_CELL_BOOLEAN: 'bool', XL_CELL_ERROR: 'error', XL_CELL_BLANK: 'blank', }