openpyxl删除空白行
时间: 2023-10-19 13:34:52 浏览: 155
python如何删除列为空的行
在使用openpyxl模块删除空白行时,可以通过遍历每一行的单元格来判断是否为空白行,并将需要删除的行保存在一个列表中。然后,倒序遍历保存的行号列表,依次删除对应的行即可。这种方法可以确保删除操作不会改变后续行的行号。具体代码如下:
```
del_row = []
for row in sheet_codereview.rows:
empty_row = True
for cell in row:
if cell.value is not None:
empty_row = False
break
if empty_row:
del_row.append(row)
for row in reversed(del_row):
sheet_codereview.delete_rows(row<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* [Python操作Excel插入删除行的方法](https://download.csdn.net/download/weixin_38692928/13777329)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"]
- *2* *3* [python openpyxl删除excel特定行数据遇到的问题](https://blog.csdn.net/aaaaa654963/article/details/124664981)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"]
[ .reference_list ]
阅读全文