简体   繁体   中英

Excel search for cell value and delete row using openpyxl

I am trying to search a sheet using openpyxl and then remove the entire row when a value is found. When I attempt the below code I am receiving TypeError: '>' not supported between instances of 'tuple' and 'int' Can someone advise how I can iterate on a cell but then reference the row from the parent loop?

Here is my code:

wb = xl.load_workbook(filename)
sheet = wb[os.path.splitext(filename)[0]]

for row in sheet.rows:
    for cell in row:
        if cell.value == "Report Generated By  :":
            sheet.delete_rows(row, 1)

After reading up on this article: https://openpyxl.readthedocs.io/en/stable/api/openpyxl.cell.cell.html

I discovered I could solve this with the following statement:

sheet.delete_rows(cell.row, 1)

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM