用Python的xlrd模块处理时间单元格

搜索关键字:

  • Python xlrd excel 日期 时间单元格

参考链接:

参考解答:

import xlrd

data = xlrd.open_workbook(EXCEL_PATH)
table = data.sheet_by_index(0)
lines = table.nrows
cols = table.ncols
print u'The total line is %s, cols is %s'%(lines, cols)

# 读取某个单元格
table.cell(x, y).value
'''
x : 行
y : 列
行/列都是从0开始
'''

In [1]: import xlrd

In [2]: adata = xlrd.open_workbook('test_xlrd.xlsx')

In [3]: atable = adata.sheet_by_index(0)

In [4]: atable.cell(1, 5).value
Out[4]: 42182.0

In [5]: xlrd.xldate_as_tuple(atable.cell(1, 5).value, 0)
Out[5]: (2015, 6, 27, 0, 0, 0)

In [6]: xlrd.xldate_as_tuple(42182.0, 0)    # 转化为 tuple 形式
Out[6]: (2015, 6, 27, 0, 0, 0)

In [7]: xlrd.xldate.xldate_as_datetime(42182.0, 0)  # 转化为 datetime 对象
Out[7]: datetime.datetime(2015, 6, 27, 0, 0)

# 查看源码(在IPython中)
In [8]: xlrd.xldate_as_tuple??
In [9]: xlrd.xldate.xldate_as_datetime??

=EOF=

发表评论

电子邮件地址不会被公开。 必填项已用*标注