Openpyxl deleting cells

点点圈 提交于 2020-01-03 01:45:17

问题


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


回答1:


[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.



来源:https://stackoverflow.com/questions/31931176/openpyxl-deleting-cells

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!