简体   繁体   中英

Reading from excel using python

I am reading a excel file using xlrd and writing the data to a file. Now the problem I am facing is that in the file I get the integer data from excel as float and where I have someting like 000123 it gets truncated to 123.

I want to read the values from cells and write it in same format in the file. Is there a way i can read the excel cells as text everywhere ?

Please suggest proper way to do this.

You can check the cell.ctype if it is XL_CELL_TEXT or XL_CELL_NUMBER or something else.

(other possible celltypes: XL_CELL_EMPTY, XL_CELL_DATE, XL_CELL_BOOLEAN, XL_CELL_ERROR, XL_CELL_BLANK )

If your cell is of type XL_CELL_TEXT then you should be able to get the value of '000123'.

If your cell is of type XL_CELL_NUMBER then your cell.value will probably be '123'. The value of '00123' that can be seen in the excel sheet can be a result of a formatting set in the excel sheet. I suppose this formatting information should be possible to extract from the sheet as well. After you have extracted the formatting information, you should be able to reproduce the value '000123' by combining the formatting information and the original number value of '123'.

However, I am not sure the xlrd module can extract all the the formatting information you need.

Consult the docs: https://secure.simplistix.co.uk/svn/xlrd/trunk/xlrd/doc/xlrd.html

Or the source source: https://github.com/python-excel/xlrd/blob/master/xlrd

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM