Search code examples
pythonexcelopenpyxlxlrdxlwt

What is the equivalent function in openpyxl?


In xlrd and xlwt to append the rows of a sheet to an array I can do:

Stuff = []

column_count = sheet.ncols - 1

for i in range (0, column_count):
     Stuff.append([sheet.cell_value(row, i) for row in range(sheet.nrows)])

How do I do the equivalent in openpyxl?


Solution

  • You can iterate through the rows of a worksheet:

    stuff = [[cell.value for cell in row] for row in sheet]
    

    Or, if you rather group it by columns, use .columns:

    stuff = [[cell.value for cell in column] for column in sheet.columns]
    

    The columns property is not available for read-only worksheets because data is stored in rows.