1 | initial version |
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.