openpyxl offers the ability to delete one row with index like ws.delete_cols(7)
Now i want to delete more cells.
First I iterate over all cells:
for row in ws.iter_rows():
for cell in row:
There I want to make ws.delete_cols(cell)
but it does not work. Does someone know how to make it?
CodePudding user response:
the cell
is not an integer, so you are seeing the error. You can use cell.column
to get the column number. Based on your code, my understanding is that you are trying to delete all columns one by one till there is no data. To do that, you don't need to do this. ws.max_column
to get the number of columns. Also, as you are deleting, it is better to start at the last column and then work backwards so that the numbers dont get messed up. Below is the code to do this... hope this helps.
wb=openpyxl.load_workbook('YourFile.xlsx')
ws=wb['Sheet1']
for col in range(ws.max_column, 0,-1):
ws.delete_cols(col)
wb.save('YourFile.xlsx')