如何将当前date转换为Excel中存储的date?

我可以使用xldate_as_tuple函数将从Excel中读取的date转换为适当的date。 是否有任何function,可以做相反的,即将适当的date转换为浮动,这是存储为Excel中的date?

在xlrd xldate.py模块中,有几个函数, xldate_from_date_tuple()xldate_from_time_tuple()xldate_from_datetime_tuple() ,它们将datetime对象转换为Excel系列date。

此外,在XlsxWriter utility.py模块中,还有一个名为datetime_to_excel_datetime()的函数,该函数可以为Excel序列date转换date时间对象。

所有这些解决scheme都考虑到了这个时代和Excel 1900的飞跃问题。

几乎Bjoern Stiel的答案(试图箭头但没有足够的声望)。

不得不添加tz_info(用于此的pytz):

  from win32com.client import Dispatch xlApp = Dispatch("Excel.Application") book = xlApp.Workbooks.Open(r"C:\Path\to\file") d = pytz.utc.localize(datetime.datetime.now()) book.Sheets(1).Cells(2,12).Value = d 

Exceldate表示为pywintypes.Timetypes的对象。 所以为了例如把当前时间戳分配给一个单元,你需要:

workbook.Worksheets(1).Cells(1,1).Value = pywintypes.Time(datetime.datetime.now())