Loading

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

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??

原文地址:http://ju.outofmemory.cn/entry/193144

posted @ 2016-11-04 10:36  青岛欧姆网络科技  阅读(7750)  评论(0编辑  收藏  举报