使用Python 2.7和openpyxl在Excel中删除单元格 [英] Delete cells in Excel using Python 2.7 and openpyxl

查看:1112
本文介绍了使用Python 2.7和openpyxl在Excel中删除单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用openpyxl从Excel电子表格中删除单元格。这似乎是一个非常基本的命令,但我已经环顾四周,无法找到如何做到这一点。我可以将其值设置为None,但它们仍然以空单元格的形式存在。 worksheet.garbage_collect()抛出一个错误,表示它已被弃用。我使用最新版本的openpyxl。有没有办法删除一个空单元格(就像在Excel中一样),还是手动将所有的单元格移动?谢谢。

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.

推荐答案

在openpyxl单元格中单独存储在字典中。这使得像删除或添加列或行这样的聚合操作变得困难,因为代码必须处理大量的单个单元格。然而,即使移动到表格或矩阵实现也是棘手的,因为每个单元格的坐标存储在每个单元格上,这意味着您将所有单元格处理到插入或删除的单元格右侧和下方。这就是为什么我们还没有为此添加任何方便的方法,因为它们可能真的很慢,我们不想要这样做。

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.

这篇关于使用Python 2.7和openpyxl在Excel中删除单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆