Excel中的时间(HH:MM:SS)在openpyxl中转换为十进制值

时间:2017-02-04 11:26:11

标签: python-3.x openpyxl

我正在尝试运行以下代码:

import openpyxl

wb = openpyxl.load_workbook("/Users/kakkar/Downloads/TRADEBOOK ALL-EQ 01-01-2016 TO 04-02-2017.xlsx")
sheet = wb.get_sheet_by_name('TRADEBOOK')
print(sheet.cell(row=15, column=3).value)
print(sheet.cell(row=15, column=3).internal_value) 

在输入的Excel文件中,第3列包含时间值10:47:49。这在openpyxl中被视为浮点值0.449872685185185

如何以HH:MM:SS形式获取时间值?

2 个答案:

答案 0 :(得分:1)

Excel将时间(和日期)值存储为浮点数。整数是自纪元(1970年1月1日)以来的天数,剩余部分是当天的部分。

Here is an answer可以帮助您获得超出值的小时,分​​钟和秒数,但this answer可能更适合在不编写代码的情况下获得答案(它使用xlrd库)。< / p>

答案 1 :(得分:0)

10:47:49的.internal_value(HH:MM:SS) IS 0.449872685185185,所以没关系。

请提供以下输出:

cell = sheet['C15']
print('cell=\t%s\n\ttype=%s\n\t.number_format=%s;\n\t.value=%s;\n\t.internal_value=%s;' % (cell,str(type(cell.value)),cell.number_format,cell.value,cell.internal_value) )

使用Python测试:3.4.2 - openpyxl:2.4.1 - LibreOffice:4.3.3.2