1

I'm trying to delete cells from an Excel spreadsheet using openpyxl. It seems like a pretty basic command, but I've looked around and can't find out how to do it. I can set their values to None, but they still exist as empty cells. worksheet.garbage_collect() throws an error saying that it's deprecated. I'm using the most recent version of openpyxl. Is there any way of just deleting an empty cell (as one would do in Excel), or do I have to manually shift all the cells up? Thanks.

2
  • What's wrong with empty cells? Commented Dec 3, 2014 at 10:08
  • @CharlieClark - I have several rows of empty cells at the start of the Excel sheet, so when I open it up I have to scroll way down to find the data. Not much of a problem if I'm only working with one sheet, but a few hundred... Commented Dec 3, 2014 at 16:07

1 Answer 1

4

In openpyxl cells are stored individually in a dictionary. This makes aggregate actions like deleting or adding columns or rows difficult as code has to process lots of individual cells. However, even moving to a tabular or matrix implementation is tricky as the coordinates of each cell are stored on each cell meaning that you have process all cells to the right and below an inserted or deleted cell. This is why we have not yet added any convenience methods for this as they could be really, really slow and we don't want the responsibility for that.

Hoping to move towards a matrix implementation in a future version but there's still the problem of cell coordinates to deal with.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the response, Charlie. And a huge thanks for all the work you guys do on this!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.