FoxyRayne
FoxyRayne

Reputation: 13

openpyxl: How can I grab data from specific columns through all rows?

I have searched the forums a few times and I haven't found anything that covers looking for specific column information while iterating through all rows.

I have a spreadsheet that has 8 columns and 2200 rows.

I just need columns 0 5 8 9 (A G J K)

My current code is below and it returns all values from A-K and removes None values.

from openpyxl import Workbook , load_workbook
from itertools import chain

wb = Workbook()

wb = load_workbook("CR_Master_List.xlsx", read_only=True)

ws = wb.active

mylist = []
def iter_rows(ws):
    for row in ws.iter_rows():
        yield [cell.value for cell in row if cell.value is not None]

mylist = list(iter_rows(ws))

print(mylist)

Any help would be appreciated.

Upvotes: 1

Views: 2442

Answers (1)

Charlie Clark
Charlie Clark

Reputation: 19507

The following should work.

cols = [0, 5, 8, 9]

def filter_cols(ws):
    for row in ws.iter_rows(max_col=10):
        cells = [cell.value for (idx, cell) in enumerate(row) if (
             idx in cols and cell.value is not None)]
        yield cells

Upvotes: 1

Related Questions