python删除excel符合条件的行,如何在Excel电子表格中删除满足某些条件的行?

I would like to create a "reduced" version of an Excel (xlsx) spreadsheet (i.e. by removing some rows according to some criterion), and I'd like to know if this can be done with openpyxl.

In (pythonish) pseudo-code, what I want to do would look something like:

wb = openpyxl.reader.excel.load_workbook('/path/to/workbook.xlsx')

sh = wb.get_sheet_by_name('someworksheet')

# weed out the rows of sh according to somecriterion

sh.rows[:] = [r for r in sh.rows if somecriterion(r)]

# save the workbook, with the weeded-out sheet

wb.save('/path/to/workbook_reduced.xlsx')

Can something like this be done with openpyxl, and if so, how?

解决方案

2018 update: I was searching how to delete a row today and found that the functionality is added in openpyxl 2.5.0-b2. Just tried and it worked perfectly.

Here's the link where I found the answer: https://bitbucket.org/openpyxl/openpyxl/issues/964/delete_rows-does-not-work-on-deleting

And here's the syntax to delete one row:

ws.delete_rows(index, 1)

where:

'ws' is the worksheet,

'index' is the row number, and

'1' is the number of rows to delete.

There's also the ability to delete columns, but I haven't tried that.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值