Python xlrd 读取 DateTime 作为序列号

Python xlrd read DateTime as serial number

我有一个 excel,它是一种 DateTime 格式。但是当我阅读 sheet 时,它显示为序列号。

例如,在 sheet 中它是 08/03/2015 但 xlrd 读作 42219

代码非常简单

workbook = xlrd.open_workbook(file_contents=excel_contents)
the_sheet = workbook.sheet_names()[0]
number_of_rows = the_sheet.nrows - 1
curr_row = 0
data = []

while curr_row < number_of_rows:
    curr_row += 1
    air_date = the_sheet.cell_value(curr_row, 1)
    air_time = the_sheet.cell_value(curr_row, 2)
    schedule_time = '{}:{}'.format(air_date, air_time)
    unaware = datetime.strptime(schedule_time, '%m/%d/%Y:%I:%M %p') 

我在Python中用来翻译成datetimeair_date因为格式不同而炸掉了。

这是例子excel。

这是我得到的错误

builtins.ValueError ValueError: time data '42219.0:06:00 AM' does not match format '%m/%d/%Y:%I:%M %p'

您似乎需要使用 xldate_as_tuple 将 excel 格式转换为 python 格式(参见 documentation and this already existing question,您可以在其中找到转换日期的格式至 python.

xldate_as_tuple :

Convert an Excel number (presumed to represent a date, a datetime or a time) into a tuple suitable for feeding to datetime or mx.DateTime constructors.