I'm writing the code for the below task for an excel automation task.
If the cell contents in column 'C' are empty, the entire row of that cell is deleted.
example image
If you look at the image above, rows 5, 7, 9, and 11 of column 'C' are empty, so I need to delete those rows.
Here is the code I wrote for the above task.
import openpyxl
wb = openpyxl.load_workbook("sample.xlsx")
ws=wb.worksheets[0]
for row in ws.rows:
for cell in row:
if cell[2].value == None:
ws.delete_rows(row, 1)
When I run the above code I get the following error:
C:\project\b>C:/python39-32/python.exe c:/project/b/ex.py
Traceback (most recent call last):
File "c:\project\b\ex.py", line 46, in <module>
if cell[2].value == None:
TypeError: 'Cell' object is not subscriptable
How do I solve this?
The reason for the error is row
is not an integer (not row number) but the whole row. To achieve what you are looking for, you need to iterate using row numbers. Below code is a way to achieve this.
wb = openpyxl.load_workbook("sample.xlsx")
ws=wb.worksheets[0]
i=1 # I am checking all rows, including header. Change to '2' if you want to skip header row
while i <= ws.max_row:
if ws.cell(row=i, column=3).value == None: #Check 3rd column of each row for None
ws.delete_rows(i, 1)
# No i+1 here as the row below will move up by one and you need to check that as well
else:
i += 1
wb.save("sample.xlsx")
Options - 2 Based on @Charlieclark's suggestion, here is another way to delete the rows. Iterate in reverse order and delete the rows where 3rd column is blank. Certainly more elegant and shorter
import openpyxl
wb = openpyxl.load_workbook("sample.xlsx")
ws=wb.worksheets[0]
for row in reversed(list(ws.rows)):
if row[2].value is None:
ws.delete_rows(row[0].row, 1)
wb.save("sample.xlsx")
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.