简体   繁体   中英

How do I iterate through a column using openpyxl and get the values of specific cells?

I am trying to build a function that takes an already loaded excel sheet and grabs specific cells and their values and puts them into a list. Right now i am able to have it return the list in the format below, but I need it to return the value in each of those cells instead of the location.

def total_pie(x):
    pielist=[]
    for i in x.iter_cols(min_row=25,max_row=25, min_col=4,max_col=10):
        for cell in i:
            pielist.append(cell)
    pielist=pielist[::2]
    return pielist


total_pie(ws)
Out[180]:
[<Cell 'ws'.D25>,
 <Cell 'ws'.F25>,
 <Cell 'ws'.H25>,
 <Cell 'ws'.J25>]

I am trying to build a function that takes an already loaded excel sheet and grabs specific cells and their values and puts them into a list. Right now i am able to have it return the list in the format below, but I need it to return the value in each of those cells instead of the location.

def total_pie(x):
    pielist=[]
    for i in x.iter_cols(min_row=25,max_row=25, min_col=4,max_col=10):
        for cell in i:
            pielist.append(cell)
    pielist=pielist[::2]
    return pielist


total_pie(ws)
Out[180]:
[<Cell 'ws'.D25>,
 <Cell 'ws'.F25>,
 <Cell 'ws'.H25>,
 <Cell 'ws'.J25>]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM