Rounded datetime values with Openpyxl

Viewed 186

I try to extract timestamp from an excel file using openpyxl since I've read that xlrd is soon to be deprecated.

using the following code

import openpyxl

book= openpyxl.load_workbook('file.xlsx')
sh = book.worksheets[0]

rundate=sh.cell(4,1).value
print(rundate)
rundate=sh.cell(5,1).value
print(rundate)
rundate=sh.cell(6,1).value
print(rundate)
rundate=sh.cell(7,1).value
print(rundate)

however, the result I get is not usable because of some decimal errors.

2020-02-01 02:00:00.000001
2020-02-01 03:00:00
2020-02-01 03:59:59.999999
2020-02-01 05:00:00.000001

I don't care about the microsecond so I would just want to get the following. Which by the way is what is displayed in Excel.

2020-02-01 02:00:00
2020-02-01 03:00:00
2020-02-01 04:00:00
2020-02-01 05:00:00

All I can find on forums is a solution using xlrd.

rundate_as_datetime = datetime.datetime(*xlrd.xldate_as_tuple(rundate, book.datemode))
0 Answers
Related