I’ve been using xlrd3 to manage a .xlsx spreadsheet through python. When I’m given the value of the cell the date should be in, it gives me 44295, which should be 09/04/2021.
I understand that this is because of the format excel stores dates as, so I’ve been using the xldate_as_datetime
function in this way:
JavaScript
x
5
1
lastsold = itemworksheet.cell_value(i,32)
2
lastsold_date = xlrd3.xldate_as_datetime(lastsold,0)
3
lastsold_object = lastsold_date.date()
4
lastsold_string = lastsold_object.isoformat()
5
(i is an interable from a ‘for’ loop)
When the code is (lastsold,0)
I get the error
JavaScript
1
2
1
'<' not supported between instances of 'str' and 'int'
2
And when the code is (lastsold,1)
I get the error.
JavaScript
1
2
1
invalid literal for int() with base 10: ''
2
What can I do so I can get the date in the format 09/04/2021 in python?
Advertisement
Answer
I have tested with below code and it works well. Please check it.
JavaScript
1
11
11
1
import xlrd
2
3
book = xlrd.open_workbook("test.xlsx")
4
first_sheet = book.sheet_by_index(0)
5
6
val = first_sheet.cell_value(0, 1)
7
lastsold_date = xlrd.xldate_as_datetime(val, 1)
8
obj = lastsold_date.date()
9
10
print(obj.isoformat())
11