1 import xlrd
2 from xlrd import xldate_as_tuple
3 from datetime import datetime
4
5 #打印Excel表格数据类型,寻找对应的结局函数
6 def ensure_cell_type():
7 book = xlrd.open_workbook('example.xlsx')
8 sheet = book.sheet_by_name('Sheet1')
9 for row in range(sheet.nrows):
10 for col in range(sheet.ncols):
11 print(sheet.cell(row,col))
12 print('type',sheet.cell(row,col).ctype)
13
14 #转换Excel中时间格式,此处的类型是3
15 def tran_time_form():
16 book = xlrd.open_workbook('example.xlsx')
17 sheet = book.sheet_by_name('Sheet1')
18 for row in range(sheet.nrows):
19 for col in range(sheet.ncols):
20 value = sheet.cell(row, col).value
21 if sheet.cell(row, col).ctype == 3:
22 date = xldate_as_tuple(sheet.cell(row, col).value, 0)
23 print(date)
24 value = datetime(*date)
25 print(value)
26
27 def main():
28 ensure_cell_type()
29 #tran_time_form()
30
31 if __name__ == "__main__":
32 main()