Oct-30-2019, 09:39 AM
Hello !
I try to delete all rows from a Excel sheet, who satisfied this condition: all cells from a row must contain only the values "-" or "".
I use Python and openpyxl .
But my code, don't work well:
I try to delete all rows from a Excel sheet, who satisfied this condition: all cells from a row must contain only the values "-" or "".
I use Python and openpyxl .
But my code, don't work well:
import openpyxl wb1 = openpyxl.load_workbook(filename="testat_openpyxl.xlsx") instr="" for i in range(5, wb1["Centralizator"].max_row): for j in range(7,49): celval=wb1["Centralizator"].cell(row=i,column=j).value ins='{}=="-" or {}=="" and '.format(celval) instr=instr + ins if instr[:-3]: wb1["Centralizator"].delete_rows(i,1) wb1.save('testat_openpyxl.xlsx')My idea is to create a big "if" statement , to check all cells from a row:
if wb1["Centralizator"].cell(row=5,column=7).value=="-" or wb1["Centralizator"].cell(row=5,column=7).value=="" and wb1["Centralizator"].cell(row=5,column=8).value=="-" or wb1["Centralizator"].cell(row=5,column=8).value=="" wb1["Centralizator"].cell(row=5,column=9).value=="-" or wb1["Centralizator"].cell(row=5,column=9).value=="" .........loop until column 48............ wb1["Centralizator"].cell(row=5,column=48).value=="-" or wb1["Centralizator"].cell(row=5,column=48).value=="" now jump to next row if wb1["Centralizator"].cell(row=6,column=7).value=="-" or wb1["Centralizator"].cell(row=6,column=7).value=="" and wb1["Centralizator"].cell(row=6,column=8).value=="-" or wb1["Centralizator"].cell(row=6,column=8).value=="" .........loop until column 48........... wb1["Centralizator"].cell(row=6,column=48).value=="-" or wb1["Centralizator"].cell(row=6,column=48).value=="" next row.......until max_row....