Oct-13-2022, 06:53 AM
I don't know, sorry!
I copied and pasted Post #4 straight into Idle and it worked exactly as I hoped!
I didn't change anything.
Change it back and try again!
This is post #2 changed, because I didn't need a new Excel for post #2
I copied and pasted Post #4 straight into Idle and it worked exactly as I hoped!
I didn't change anything.
Change it back and try again!
This is post #2 changed, because I didn't need a new Excel for post #2
def myApp(): import openpyxl path2XL = '/home/pedro/myPython/openpyxl/xlsx_files/' file = 'some_X_or_x_in_column_2.xlsx' name = file.split('.') savename = name[0] + '_modified.xlsx' sF = openpyxl.load_workbook(path2XL + file) sFsheets = sF.sheetnames sFsheet = sFsheets[0] sFmaxRow = sF[sFsheet].max_row # before for row in sF[sFsheet].values: print(row) # you can choose the rows and columns you want to iterate # for row in sF[sFsheet].iter_rows(min_row=1, min_col=1, max_row=6, max_col=2): # look through column 2 # if a name contains X or x delete that row # row[1] is the value in column 2 of the XL # if there is an X or x in a header name, the headers row will be gone! Fix that! for row in sF[sFsheet].iter_rows(): if row[1].value == None: continue if 'X' in row[1].value or 'x' in row[1].value: sF[sFsheet].delete_rows(row[0].row) # after for row in sF[sFsheet].values: print(row) sF.save(path2XL + savename)From this, you can see how .delete_rows works: it adds and empty row, then deletes, otherwise you would get and index error,