excel-formulareferencerangeopenpyxldelete-row

Delete ranges with openpyxl and adapt the relative references in formulae in the remaining cells


With openpyxl, when I use worksheet.move_range(range, rows=12, translate=True) the range will be moved 12 rows down. The translate=True will move the relative references in formulae in the range by the number of rows that is inserted.

Is there something similar for deleting ranges?

For instance, if I write worksheet.delete_range(range, rows=12, translate=True) I want the first 12 rows in the range to be deleted and the relative references in formulae in the rest of the range adapted by the number of rows that is deleted.


Solution

  • Not only can you move rows down, you can also move rows up. So instead of deleting a certain number of rows, you can use move_range with translate=True to move everything coming after the to be deleted rows up by the same amount and have the the relative references in formulae adapted.