Python xlrd gets float from excel text cell

I am trying to read values ​​from an Excel file using xlrd. It works great with dates, numbers, and still text. I have a column (category) with cells containing text (cells are formatted as text). When I print out the value of a cell, a float is displayed instead of text. I also printed out the ctype of the Cell object (s) to check it, and it displays as Number. I read the xlrd documentation and tutorial and cannot find why this is happening. Maybe my excel file got messed up somehow? Any suggestions or pointers in the right direction?

import xlrd
import datetime

workbook = xlrd.open_workbook('training.xls')
courseSheet = workbook.sheet_by_index(0)

for row in range(courseSheet.nrows):
    title = courseSheet.cell_value(row, 2)
    date = courseSheet.cell_value(row, 4)
    date = datetime.datetime(*xlrd.xldate_as_tuple(date, workbook.datemode))
    dateTuple = date.timetuple()
    category = courseSheet.cell_value(row, 7)
    print category
+5
source share
2 answers

: xlrd ( ), XLS. (, NUMBER RK ). , , , , datetime, date time . xlrd , .

, , , . , , " ".

"" , "" "... ..., , () , (b), , " "(c) repr (cell.value) (d) " ", ?

:

import xlrd, sys

def dump_cell(sheet, rowx, colx):
    c = sheet.cell(rowx, colx)
    xf = sheet.book.xf_list[c.xf_index]
    fmt_obj = sheet.book.format_map[xf.format_key]
    print rowx, colx, repr(c.value), c.ctype, \ 
        fmt_obj.type, fmt_obj.format_key, fmt_obj.format_str

book = xlrd.open_workbook(sys.argv[1], formatting_info=1)
sheet = book.sheet_by_index(0)
for rowx in xrange(sheet.nrows):
    for colx in xrange(sheet.ncols):
        dump_cell(sheet, rowx, colx)
+3

, OP, , , , python (xlrd) . , Excel. , , "", "" , "" .

, excel, , , . xlrd , , - , excel. (, , "1" excel, xlrd "1" )

, , , excel "1" , xlrd "1.0". xlrd cell.ctype , , - , Text Excel.

, excel . excel .

0

All Articles