简体   繁体   English

Excel 使用 openpyxl 搜索单元格值并删除行

[英]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.我正在尝试使用 openpyxl 搜索工作表,然后在找到值时删除整行。 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?当我尝试下面的代码时,我收到 TypeError: '>' not supported between 'tuple' 和 'int' 有人可以建议我如何迭代一个单元格,然后从父循环中引用该行吗?

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阅读本文后: 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) sheet.delete_rows(cell.row, 1)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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