Home » excel » python – How to find if excel cell is a date

python – How to find if excel cell is a date

Posted by: admin March 9, 2020 Leave a comment

Questions:

If I do:

print sh.cell(1,col)

I get:

text:u''
text:u''
text:u'eng'
text:u''
xldate:41450.0
number:11.0
number:30.0
text:u'Reality TV'

However, if I do: print type(sh.cell(1,col)), I get:

<class 'xlrd.sheet.Cell'>

for all.

How would I get “text” or “xldate” or “number” — i.e., the cell type — from xlrd?

How to&Answers:

You want the ctype attribute of the Cell object. E.g.:

sh.cell(1,col).ctype

This is an integer that indicates the cell type. You want to compare to xlrd.XL_CELL_DATE; see documentation here.

Answer:

I did this a hack-ish way:

if str(sh.cell(1,col)).split(':')[0] == 'xldate':