Using openpyxl to find rows that contain cell with specific value (Python 3.6)

前端 未结 4 2218
春和景丽
春和景丽 2021-02-10 16:24

I am completely new to openpyxl so, as you can imagine, I am having pretyy hard times when I try to make use of it.

I have an excel report that contains only one sheet

4条回答
  •  春和景丽
    2021-02-10 17:01

    wb = load_workbook("report.xlsx")
    ws = wb.active
    
    abc_rows=[]
    
    for row in range(2,ws.max_row+1):
        if(ws[row][4]=='ABC'):
           abc_rows.append(row)
    

    To access the selected rows separately, further add this code

    i=1
    abc_dict={}
    for row in ws.values:
        if(i in abc_rows):
            (a,b,c,d,e,f,g,h,i,j,k,l)=row
             abc_dict[i]=row
        i=i+1  
    

    To access selected rows seperately, use

    abc_dict[2] gives entire second row as tuples and abc_dict[2][0] gives first cell value. you can parse till abc_dict[2][11] to get each cell of selected rows seperately.

提交回复
热议问题