Question

I got a really strange problem. I'm trying to read some data from an excel file, but the property nrows has a wrong value. Although my file has a lot of rows, it just returns 2.

I'm working in pydev eclipse. I don't know what is actually the problem; everything looks fine.

When I try to access other rows by index manually, but I got the index error.

I appreciate any help.

If it helps, it's my code:

def get_data_form_excel(address):
    wb = xlrd.open_workbook(address)
    profile_data_list = []
    for s in wb.sheets():
        for row in range(s.nrows):
            if row > 0:
                values = []
                for column in range(s.ncols):
                    values.append(str(s.cell(row, column).value))
                profile_data_list.append(values)
    print str(profile_data_list)
    return profile_data_list
Was it helpful?

Solution 2

After trying some other files I'm sure it's about the file, and I think it's related to Microsoft 2003 and 2007 differences.

OTHER TIPS

To make sure your file is not corrupt, try with another file; I doubt xlrd is buggy.

Also, I've cleaned up your code to look a bit nicer. For example the if row > 0 check is unneeded because you can just iterate over range(1, sheet.nrows) in the first place.

def get_data_form_excel(address):
    # this returns a generator not a list; you can iterate over it as normal,
    # but if you need a list, convert the return value to one using list()
    for sheet in xlrd.open_workbook(address).sheets():
        for row in range(1, sheet.nrows):
            yield [str(sheet.cell(row, col).value) for col in range(sheet.ncols)]

or

def get_data_form_excel(address):
    # you can make this function also use a (lazily evaluated) generator instead
    # of a list by changing the brackets to normal parentheses.
    return [
        [str(sheet.cell(row, col).value) for col in range(sheet.ncols)]
        for sheet in xlrd.open_workbook(address).sheets()
        for row in range(1, sheet.nrows)
    ]

I recently got this problem too. I'm trying to read an excel file and the row number given by xlrd.nrows is less than the actual one. As Zeinab Abbasi saied, I tried other files but it worked fine.

Finally, I find out the difference : there's a VB-script based button embedded in the failed file, which is used to download and append records to the current sheet.

Then, I try to convert the file to .xlsx format, but it asks me to save as another format with macro enabled, e.g .xlsm. This time xlrd.nrows gives the correct value.

Is your excel file using external data? I just had the same problem and found a fix. I was using excel to get info from a google sheet, and I wanted to have python show me that data. So, the fix for me was going to DATA>Connections(in "Get External Data")>Properties and unchecking "Remove data from the external data range before saving the workbook"

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top