用python读写excel,当读写内容为时间时,会发现时间变成了浮点数,这篇文章记录了对于这种情况的处理。
将时间写入excel:
dateFormat = xlwt.XFStyle()
dateFormat.num_format_str = 'yyyy/mm/dd hh:mm:ss'
dateFormat2 = xlwt.XFStyle()
dateFormat2.num_format_str = 'yyyy/mm/dd'
if isinstance(要写入的值,datetime.datetime): # 判断是datetime类型还是date类型
sheet.write(行,列,值,dateFormat)
elif isinstance(要写入的值,datetime.date):
sheet.write(行,列,值,dateFormat2)
从excel读取时间
日期 = datetime.datetime(*xlrd.xldate_as_tuple(值, ExcelFile.datemode))