简体   繁体   English

Openpyxl删除单元格

[英]Openpyxl deleting cells

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. 我想选择列表的最后20项目,将它们复制一个新的电子表格,复制后,我想从第一个电子表格中delete它们。

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. 我有第一部分(复制最后20个),但是我不确定如何delete该单元格,因此保存后它不会显示为“ none

filtered = [-20:] how do i delete those 20 cells from the spreadsheet? 已筛选= [-20:]我如何从电子表格中删除那20个单元格?

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 问题是已filtered = [x for x in sheet.rows if x is not None]无法正常工作,并且实际上将“ None项附加到filtered列表

how can i make sure that its only picking the last 20 items that are not none ? 我如何才能确保其唯一采摘最后的20个项目是不是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. [x for x in somesheet.rows if x is not None]将返回行列表,因为ws.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. 不建议调用ws.garbage_collect() ,因为这没有任何意义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM