简体   繁体   中英

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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