Search code examples
pythonexcelxlrd

How to determine the excel cell value type(data type such as date, string, float etc.)


I want to read the cell(1,1) value in the excel sheet, it is date in the format of 21-08-18. I'm using the python package xlrd to read the value, it returns the float value ex. like 4567.0

book = xlrd.open_workbook(path)
compSheet = book.sheet_by_name("sheet_name")
cell_value = compSheet.cell_value(1, 1)

How to know the data type of the cell before reading the value whether it is date or string or float or etc.


Solution

  • You can get a cell's type to see if it's a date and compare that to xlrd's cell object definition (or directly compare it to the number as seen in that table, as cell.ctype just returns a number).

    book = xlrd.open_workbook(path)
    compSheet = book.sheet_by_name("sheet_name")
    cell = compSheet.cell(1,1)
    if cell.ctype is xlrd.XL_CELL_DATE:
        print "Cell 1,1 is a date!"