Openpyxl删除单元格 [英] Openpyxl deleting cells

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

问题描述

我想选择一个列表的最后一个 20 项目复制一个新的电子表格,一旦复制,我想要 delete 他们从第一个电子表格。



我有第一部分(复制最后20),但不知道如何删除该单元格,所以它不会显示为 none 之后,我保存它。



filtered = [ -20:]
如何从电子表格中删除这20个单元格?

  def cell_values(somesheet):
#从工作表中提取最后20个项目
filtered = [x for somesheet.rows如果x不为无]
last20 = filtered [-20:]
extracted_values = []

在last20中的行:
行中的单元格:
#print cell.value
extracted_values.append(cell.value)

问题是 filtered = [x如果x不为None,则为sheet.rows中的x] 不工作,实际追加项目到过滤列表



我如何确保它只选择最后20项那不是 none
以及一旦我从列表中选择了它们,就删除它们

解决方案

[x for x in somesheet.rows if x is not None] 将返回行列表,因为 ws.rows 返回行列表。 p>

目前没有办法从工作表中删除单元格。最好是将其值设置为None。除非它们具有特殊的格式,否则在保存文件时将从工作表中删除它们。调用 ws.garbage_collect()已被弃用,因为没有任何意义。


i want to pick the last 20 items of a list copy them a new spreadsheet, and once copied i want to delete them from the first spreadsheet.

i have the first part going (copy the last 20) but im not sure how to delete that cell so it doesnt appear as none after i save it.

filtered = [-20:] how do i delete those 20 cells from the spreadsheet?

def cell_values(somesheet):
    # extract the last 20 items from the worksheet
    filtered = [x for x in somesheet.rows if x is not None]
    last20 = filtered[-20:]
    extracted_values = []

    for row in last20:
        for cell in row:
            # print cell.value
            extracted_values.append(cell.value)

the problem is that filtered = [x for x in sheet.rows if x is not None] is not working and actually appending None items to the filtered list

how can i make sure that its only picking the last 20 items that are not none? as well as delete them once i have picked them from the list

解决方案

[x for x in somesheet.rows if x is not None] will return a list of rows because ws.rows returns a list of rows.

There is currently no way to remove cells from a worksheet. The best thing is to set their values to None. Unless they have special formatting they will be removed from the worksheet when the file is saved. Calling ws.garbage_collect() is deprecated because it makes no sense.

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

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