Excel library: Error when getting list of row values from excel file

625 views
Skip to first unread message

finspin

unread,
Dec 12, 2012, 6:09:04 AM12/12/12
to robotframe...@googlegroups.com
I'm testing an excel library I found copy/pasted on this forum. I'm running into problem with one keyword though.


Here is the library:

# ExcelKeywords.py
from xlrd import open_workbook

class ExcelKeywords(object):

    book=None
    currentSheet=None

    def __init__(self):
        self.book=None

    def open_excel_file(self, file, sheetIndex=0):
        self.book = open_workbook(file)
        self.currentSheet = self.book.sheet_by_index(sheetIndex)

    def get_excel_column_count(self):
        return self.currentSheet.ncols

    def get_excel_row_count(self):
        return self.currentSheet.nrows

    def get_excel_row_values(self, row):
        return [self.currentSheet.cell(row,col).value for col in range(self.currentSheet.ncols)]

    def get_excel_cell_value(self, row, column):
        return self.currentSheet.cell(int(row),int(column)).value

My tests:

Test Excel Library
    Open Excel File    test.xlsx
    ${row_column}=     Get Excel Column Count
    ${row_count}=     Get Excel Row Count
    @{row_values}=     Get Excel Row Values    0
    ${cell_value}=     Get Excel Cell Value        0    0

All keywords work fine except the second last - Get Excel Row Count. I'm getting an error: TypeError: list indices must be integers, not unicode.

I can't figure out what's the problem. Any ideas?

finspin

unread,
Dec 12, 2012, 6:21:24 AM12/12/12
to robotframe...@googlegroups.com
I made a typo in the original post. The problematic keyword is Get Excel Row Values.

Mika Hänninen

unread,
Dec 12, 2012, 6:22:06 AM12/12/12
to jaros...@gmail.com, robotframework-users
You need to convert row into int (I think).

def get_excel_row_values(self, row):
        return [self.currentSheet.cell(int(row),col).value for col in range(self.currentSheet.ncols)]

finspin

unread,
Dec 12, 2012, 6:39:44 AM12/12/12
to robotframe...@googlegroups.com, jaros...@gmail.com
That was it. Thanks a lot!
Reply all
Reply to author
Forward
0 new messages