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.
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
Asked: 2022-11-04 11:00:00 +0000
Seen: 11 times
Last updated: Aug 19 '21
Can the names of rows be modified in MUI DataGrid?
How to modify the name of existing captions in MS Word?
How can you use linq to choose a specific column from a datatable?
What is the meaning of the role being returned as 'undefined' in DiscordJS?
What is the process of obtaining a column name by using a given row and its corresponding value?
What is the process for changing the name of a dropdown in a Grafana table query?