[英]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.