简体   繁体   English

如果使用 python openpyxl 在特定列中的单元格为空白,如何删除 Excel 中的行?

[英]How to delete rows in Excel if cells in specific column are blank using python openpyxl?

I am a Python beginner (and also new to Stack Overflow) and I've recently discovered openpyxl which has been a lifesaver, but I cannot solve the following issue.我是 Python 初学者(也是 Stack Overflow 的新手),我最近发现了 openpyxl,它一直是救星,但我无法解决以下问题。

I have a spreadsheet with data in columns AI.我有一个电子表格,其中包含 AI 列中的数据。 The spreadsheet is a course-completion report which tells me if customers have completed a particular course that my company offers.电子表格是一份课程完成报告,它告诉我客户是否完成了我公司提供的特定课程。 If the customer has completed the course, the date of completion is listed in column "I" as "mm/dd/yyyy" format.如果客户已完成课程,则完成日期在“I”列中以“mm/dd/yyyy”格式列出。 If the customer has NOT completed the course, the cell in column "I" is blank.如果客户尚未完成课程,则“I”列中的单元格为空白。 I would like to delete all rows where the value in column "I" is blank.我想删除“I”列中的值为空白的所有行。 I have tried the following:我尝试了以下方法:

for cell in ws['I'][1:]:
    if cell.value is None:
        ws.delete_rows()

This didn't throw any error messages, but it also did not work.这没有抛出任何错误消息,但它也不起作用。 I also tried:我也试过:

for cell in ws['I'][1:]:
    if cell.value == None:
        ws.delete_rows()

Again, I am a beginner, so I may be missing something very simple.同样,我是初学者,所以我可能会遗漏一些非常简单的东西。 Any and all help is greatly appreciated.非常感谢任何和所有帮助。 Thank you.谢谢你。

It seems that you just need to specify the row to be deleted:看来您只需要指定要删除的行:

for cell in ws['I'][1:]:
    print(cell.value)
    if cell.value == None:
        ws.delete_rows(cell.row)
    

also you have to save the changes:您还必须保存更改:

wb.save('yourfile.xlsx')

暂无
暂无

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

相关问题 如何使用 python 中的 openpyxl 将 excel 文件中的特定单元格移动到新列 - How to move specific cells in an excel file to a new column with openpyxl in python 使用Python 2.7和openpyxl在Excel中删除单元格 - Delete cells in Excel using Python 2.7 and openpyxl 如果满足条件,如何使用 openpyxl python 删除 excel 中的特定行 - How to delete specific rows in excel with openpyxl python if condition is met 使用 python 中的 openpyxl 将特定行和列添加到另一个 excel 文件中 - add specific rows and column into another excel file using openpyxl in python 如何使用 python openpyxl 从 excel 中按列名删除列 - how to delete columns by column name from an excel using python openpyxl 如何使用openpyxl和python3为excel工作表中的一系列单元格(列和行)提供字体颜色? - How to give font color to a range of cells (columns and rows) in excel worksheet using openpyxl and python3? 如何使用 Python 获取行为空白的 Excel 的列名 - How to get the column names of an Excel where the rows are blank Using Python 如何使用 Python 将 Excel 中的空白单元格替换为 0? - How to replace the blank cells in Excel with 0 using Python? 如何在 Python 中使用 Openpyxl 对多行的 excel 行进行平均? - How to average across excel rows for multiple rows using Openpyxl in Python? 使用 openpyxl python 在 excel 中插入带空白的公式 - Inserting formula with Blank in excel using openpyxl python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM