I want to make a Python script which uses xlrd
to open a workbook and convert each worksheet to a separate XML file.
The Excel sheet can contain integer values, floats and strings. When I open the XLS file using xlrd.open_workbook()
I loop through all cells in the worksheet and read the values.
When I check the types of the cell values it seems as if there are only floats or strings. Also all integer values seem to be converted into floats.
Is it possible to read the cell values as they appear in Excel?
Per the documentation, xlrd
supports only seven types of cell value (Cell.ctype
), including
XL_CELL_NUMBER
2
(float
)
There is no separate ctype
for integers. All numerical types (including dates, which are ctype == xlrd.XL_CELL_DATE
) from Excel will be represented as float
objects in Python. You can check if they are equal to an integer using:
cell.value.is_integer()
For example:
>>> 1.0.is_integer()
True
>>> 1.1.is_integer()
False