0

Im searching for a date in excel using a string that I've converted to python date. I get a error trying to convert excel values to date using the following code:

from dateutil import parser
import xlrd

d = '4/8/2019'
dt_obj = parser.parse(d)
wbpath = 'XLSX FILE'
wb = xlrd.open_workbook(wbpath)
ws = wb.sheet_by_index(1)
for rowidx in range(ws.nrows):
    row = ws.row(rowidx)
    for colidx, cell in enumerate(row):
        if xlrd.xldate_as_tuple(cell.value, wb.datemode) == dt_obj:
            print(ws.name)
            print(colidx)
            print(rowidx)

ERROR I get:

Traceback (most recent call last):
  File "C:/Users/DKisialeu/PycharmProjects/new/YIM.py", line 12, in <module>
    if xlrd.xldate_as_tuple(cell.value, wb.datemode) == dt_obj:
  File "C:\Users\DKisialeu\AppData\Local\Programs\Python\Python37\lib\site-packages\xlrd\xldate.py", line 95, in xldate_as_tuple
    if xldate < 0.00:
TypeError: '<' not supported between instances of 'str' and 'float'
Alex
  • 6,610
  • 3
  • 20
  • 38
dkisiale
  • 35
  • 4

1 Answers1

0

Make sure that the dates in your excel spreadsheet are formatted as dates and not as text.

I get the same error by running your code with a spreadsheet with any text formatted cells at all.

YetAnotherRCG
  • 38
  • 1
  • 4
  • I have I have date format for the cells that I am trying to pull. I also have cells with cell formatting that serve different purpose in Excel. Is there anyway around this? – dkisiale Apr 09 '19 at 20:44