How to read dates using xlrd?

This is the code in which the variable "rec" is used to read dates in an excel sheet, but the value of its value for printing is how to print in the date format, for example, "2015: 09: 02"

for rec in sorted(out.keys()):
print rec #printing float values
print str(out[rec])

I got the conclusion:

42240.0
24
+4
source share
1 answer

Excel internal values ​​are stored as floating point numbers. So in xlrd, if you want to read Excel date values ​​as Python date values, you should use a method xldate_as_tupleto get the date.

Documentation: http://www.lexicon.net/sjmachin/xlrd.html#xlrd.xldate_as_tuple-function

Here's a general example:

import datetime, xlrd
book = xlrd.open_workbook("myexcelfile.xls")
sh = book.sheet_by_index(0)
a1 = sh.cell_value(rowx=0, colx=0)
a1_as_datetime = datetime.datetime(*xlrd.xldate_as_tuple(a1, book.datemode))
print 'datetime: %s' % a1_as_datetime

myexcelfile.xls A1 , datetime a1_as_datetime.

+6

Source: https://habr.com/ru/post/1606194/


All Articles