简体   繁体   English

使用python从Excel读取

[英]Reading from excel using python

I am reading a excel file using xlrd and writing the data to a file. 我正在使用xlrd读取Excel文件并将数据写入文件。 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. 现在我面临的问题是,在文件中,我从excel以float形式获取整数数据,并且在其中有000123之类的东西将其截断为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 ? 有没有办法我可以到处读取Excel单元格作为文本?

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. 您可以检查cell.ctype是否为XL_CELL_TEXTXL_CELL_NUMBER或其他。

(other possible celltypes: XL_CELL_EMPTY, XL_CELL_DATE, XL_CELL_BOOLEAN, XL_CELL_ERROR, XL_CELL_BLANK ) (其他可能的单元格类型: 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'. 如果您的单元格类型为XL_CELL_TEXT那么您应该能够获得值'000123'。

If your cell is of type XL_CELL_NUMBER then your cell.value will probably be '123'. 如果您的单元格类型为XL_CELL_NUMBER则您的cell.value可能为'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. 在Excel工作表中可以看到的值'00123'可以是在Excel工作表中设置格式的结果。 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'. 提取格式信息之后,应该可以通过组合格式信息和原始数字值“ 123”来再现值“ 000123”。

However, I am not sure the xlrd module can extract all the the formatting information you need. 但是,我不确定xlrd模块是否可以提取您需要的所有格式信息。

Consult the docs: https://secure.simplistix.co.uk/svn/xlrd/trunk/xlrd/doc/xlrd.html 咨询文档: 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 或源代码来源: https : //github.com/python-excel/xlrd/blob/master/xlrd

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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