26

我不太确定如何使用以下功能:

xlrd.xldate_as_tuple

对于以下数据

xldate:39274.0
xldate:39839.0

有人可以给我一个关于数据函数使用的例子吗?

4

5 回答 5

33

引用文档

Excel 电子表格中的日期

实际上,没有这样的事情。你所拥有的是浮点数和虔诚的希望。Excel日期有几个问题:

(1) 日期不作为单独的数据类型存储;它们存储为浮点数,您必须依赖 (a) Excel 中应用于它们的“数字格式”和/或 (b) 知道哪些单元格中应该有日期。该模块有助于 (a) 检查已应用于每个数字单元格的格式;如果它看起来是日期格式,则单元格被分类为日期而不是数字。对此功能的反馈,尤其是来自非英语地区的反馈,我们将不胜感激。

(2) Excel for Windows 默认将日期存储为自 1899-12-31T00:00:00 以来的天数(或其分数)。Excel for Macintosh 使用默认开始日期 1904-01-01T00:00:00。可以在 Excel 中根据每个工作簿更改日期系统(例如:工具 -> 选项 -> 计算,勾选“1904 日期系统”框)。如果工作簿中已经有日期,这当然是一个坏主意。即使工作簿中没有日期,也没有充分的理由更改它。使用哪个日期系统记录在工作簿中。从 Windows 传输到 Macintosh(反之亦然)的工作簿将与主机 Excel 一起正常工作。使用此模块的 xldate_as_tuple 函数转换工作簿中的数字时,必须使用 Book 对象的 datemode 属性。

参考: http: //support.microsoft.com/default.aspx ?scid=KB;EN-US;q180162

(3) 基于 Windows 的默认 1900 日期系统的 Excel 实现在错误的前提下工作,即 1900 年是闰年。它将数字 60 解释为 1900-02-29,这不是有效日期。因此,任何小于 61 的数字都是不明确的。示例:59 是直接输入 1900-02-28 的结果,还是 1900-03-01 减去 2 天?OpenOffice.org Calc 程序“纠正”了微软的问题;输入 1900-02-27 会导致存储数字 59。另存为 XLS 文件,然后使用 Excel 打开文件——您将看到显示的 1900-02-28。

参考: http: //support.microsoft.com/default.aspx ?scid=kb;en-us;214326

我在这里引用它是因为除非您考虑到这一点,否则您的问题的答案可能是错误的。

因此,将其放入代码中将类似于:

import datetime
import xlrd

book = xlrd.open_workbook("myfile.xls")
sheet = book.sheet_by_index(0)
cell = sheet.cell(5, 19) # type, <class 'xlrd.sheet.Cell'>


if sheet.cell(5, 19).ctype == 3: # 3 means 'xldate' , 1 means 'text'
    ms_date_number = sheet.cell_value(5, 19) # Correct option 1
    ms_date_number = sheet.cell(5, 19).value # Correct option 2

    year, month, day, hour, minute, second = xlrd.xldate_as_tuple(ms_date_number, 
        book.datemode)
    py_date = datetime.datetime(year, month, day, hour, minute, nearest_second)

它为您提供了 Python 日期时间,py_date因为您可以在使用标准日期时间模块时进行有用的操作。

我从来没有使用过 xlrd,我的例子完全是虚构的,但是如果有 amyfile.xls并且它在单元格 F20 中确实有一个日期编号,并且如上所述您对精度不太挑剔,那么这段代码应该可以工作。

于 2010-09-16T15:25:21.970 回答
11

该函数的文档(减去可能的异常列表):

xldate_as_tuple(xldate, datemode) [#]

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.

xldate
    The Excel number
datemode
    0: 1900-based, 1: 1904-based.
    WARNING: when using this function to interpret the contents of
    a workbook, you should pass in the Book.datemode attribute of that
    workbook. Whether the workbook has ever been anywhere near a Macintosh is
    irrelevant. 
Returns:
    Gregorian (year, month, day, hour, minute, nearest_second).

作为 xlrd 的作者,我很想知道如何改进文档。你能回答这些吗:

您是否阅读了有关日期的一般部分(由@msw 引用)?
您是否阅读了上述功能的具体文档?
您能建议对文档进行任何改进吗?
您是否真的尝试过运行该函数,如下所示:

>>> import xlrd
>>> xlrd.xldate_as_tuple(39274.0, 0)
(2007, 7, 11, 0, 0, 0)
>>> xlrd.xldate_as_tuple(39274.0 - 1.0/60/60/24, 0)
(2007, 7, 10, 23, 59, 59)
>>>
于 2010-09-21T03:14:45.883 回答
2

像这样使用它:

number = 39274.0
book_datemode = my_book.datemode
year, month, day, hour, minute, second = xldate_as_tuple(number, book_datemode)
于 2010-09-16T15:08:06.350 回答
2
import datetime as dt
import xlrd

log_dir = 'C:\\Users\\'
infile = 'myfile.xls'
book = xlrd.open_workbook(log_dir+infile)
sheet1 = book.sheet_by_index(0)
date_column_idx = 1

## iterate through the sheet to locate the date columns
for rownum in range(sheet1.nrows):
    rows = sheet1.row_values(rownum)

    ## check if the cell is a date; continue otherwise
    if sheet1.cell(rownum, date_column_idx).ctype != 3 :
        continue

    install_dt_tuple = xlrd.xldate_as_tuple((rows[date_column_idx ]), book.datemode)

    ## the "*date_tuple" will automatically unpack the tuple. Thanks mfitzp :-)
    date = dt.datetime(*date_tuple)
于 2016-01-26T20:21:01.433 回答
2

这是我用来自动转换日期的方法:

cell = sheet.cell(row, col)
value = cell.value
if cell.ctype == 3:  # xldate
    value = datetime.datetime(*xlrd.xldate_as_tuple(value, workbook.datemode))
于 2017-09-13T19:25:50.380 回答