19

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?

2 Answers 2

20

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.

4
  • 12
    Be aware that delete_rows() actually moves the rows below up. That means that after deleting a row, all the other rows below it have a different rownumber. So remember this when iterating over all the rows. Nov 22, 2018 at 21:35
  • @NielsHenkens How do you suggest that can be solved, like if I have multiple rows that need to be deleted?
    – yoyo
    Jan 6, 2022 at 18:23
  • It has Ben a while since I used this, but from memory: If you are, for example, looping over all your rows, you could reduce your index number (or not increment it) after a row deletion. Such that the next row in your iteration had the same row number as the row that you just deleted. Jan 6, 2022 at 18:44
  • Works so slow. Is there any way to delete rows as a bunch, e.g. 2, 5, 10 row? Jul 1, 2022 at 8:53
3

Internally openpyxl does not seem to have a concept of 'rows' it works with cells and keeps track of the dimensions and if you use Worksheet.rows it calculates a 2D array of cells from that. You can mutate the array, but it doesn't change the Worksheet.

If you want to do this within the Worksheet, you need to copy the values from the old position to the new position, and set the value of the cells that are no longer needed to '' or None and call Worksheet.garbage_collect().

If your dataset is small and of uniform nature (all strings e.g.), you might be better of copying the relevant cell (content) to a new worksheet, remove the old one and set the title of the new one to the title of the just deleted one.

The most elegant thing to do, IMHO, would be to extend Worksheet or a subclass with a delete_rows method. I would implement such a method by changing the coordinates of its Cells in place. But this could break if openpyxl internals change.

2
  • Setting cell values to '' or None doesn't delete them, much less the entire row.
    – Cerin
    Jul 21, 2015 at 18:58
  • 3
    But calling garbage_collect will remove the trailing empty rows. Aug 2, 2015 at 19:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.