Ask Your Question
0

How can we remove rows that have a checked checkbox and eliminate the sheet that has the same name as the value in the checked row?

asked 2022-11-04 11:00:00 +0000

nofretete gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2021-08-19 16:00:00 +0000

plato gravatar image

Assuming you have a checkbox column (let's say it's column A) and the sheet names are in column B, you can use the following code:

import openpyxl

# Load the workbook
wb = openpyxl.load_workbook('filename.xlsx')

# Select the active worksheet
ws = wb.active

# Create a list to store the sheet names to delete
sheets_to_delete = []

# Loop through the rows in reverse order (to avoid index errors when deleting rows)
for i in range(ws.max_row, 1, -1):
    # Check if the checkbox is checked
    checkbox = ws.cell(row=i, column=1).value
    if checkbox == True:
        # Get the sheet name and add it to the list of sheets to delete
        sheet_name = ws.cell(row=i, column=2).value
        sheets_to_delete.append(sheet_name)
        # Delete the row
        ws.delete_rows(i)

# Delete the sheets in the list
for sheet_name in sheets_to_delete:
    wb.remove(wb[sheet_name])

# Save the workbook
wb.save('filename.xlsx')

This code loops through the rows in reverse order, checking if the checkbox in column A is checked. If it is, it adds the sheet name in column B to the list of sheets to delete, and deletes the row. Finally, it loops through the list of sheet names and deletes each sheet.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2022-11-04 11:00:00 +0000

Seen: 11 times

Last updated: Aug 19 '21