Python xlrd read as string

Rnet picture Rnet · Apr 18, 2011 · Viewed 46.5k times · Source

I'm having difficulties in reading a particular cell value from Excel in xlrd. Whatever value I'm reading (date value) is getting converted to a number. I know there are solutions to convert it into a python date format, but can I read directly the string value in xlrd?

Answer

John Machin picture John Machin · Apr 18, 2011

xlrd does NOT convert dates to float. Excel stores dates as floats.

Quoting from the xlrd documentation (scroll down a page):

Dates in Excel spreadsheets

In reality, there are no such things. What you have are floating point numbers and pious hope. There are several problems with Excel dates:

(1) Dates are not stored as a separate data type; they are stored as floating point numbers and you have to rely on (a) the "number format" applied to them in Excel and/or (b) knowing which cells are supposed to have dates in them. This module helps with (a) by inspecting the format that has been applied to each number cell; if it appears to be a date format, the cell is classified as a date rather than a number.

(2) ... When using this package’s xldate_as_tuple() function to convert numbers from a workbook, you must use the datemode attribute of the Book object.

See also the section on the Cell class to learn about the type of cells, and the various Sheet methods which extract the type of a cell (text, number, date, boolean, etc).

Check out python-excel.org for info on other Python Excel packages.