zzu zzu - 6 months ago 24
Python Question

Nonsequential cells with openpyxl

My desire is to treat an excel row like an array and grab only a couple of cells from each row. The cells will be from the same column each time. Something like this:

for row in sheet.iter_rows():
new_row = []
new_row += row[1]
new_row += row[3]


I am having a hard time finding good info on this from the docs.

Edit: I would also be open to ways to achieve this using some python method other than the openpyxl package.

zzu zzu
Answer

Turns out it was a matter of specifying row[i].value, as well as throwing brackets around it.

from openpyxl import load_workbook

wb = load_workbook("myfile.xlsx", use_iterators=True)
ws = wb.active

data = []
for row in ws.iter_rows():
    new_row = []
    new_row += [row[1].value]
    new_row += [row[3].value]
    data += new_row
Comments