alex_bn_lee

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

[972] Remove specific sheets from an Excel file

To remove specific sheets from an Excel file, you can use the openpyxl library in Python. Here's how you can do it:

from openpyxl import load_workbook
# Path to the Excel file
excel_file_path = 'example.xlsx'
# Open the Excel workbook
wb = load_workbook(excel_file_path)
# List of sheet names to remove
sheets_to_remove = ['Sheet2', 'Sheet3']
# Iterate over the sheet names to remove
for sheet_name in sheets_to_remove:
# Check if the sheet exists in the workbook
if sheet_name in wb.sheetnames:
# Remove the sheet from the workbook
wb.remove(wb[sheet_name])
# Save the modified workbook
wb.save('updated_example.xlsx')

In this code:

  • load_workbook() loads the existing Excel workbook.
  • sheets_to_remove is a list containing the names of the sheets you want to remove.
  • We iterate over each sheet name in sheets_to_remove, check if it exists in the workbook using if sheet_name in wb.sheetnames, and remove it using wb.remove(wb[sheet_name]).
  • Finally, we save the modified workbook using wb.save().

posted on   McDelfino  阅读(5)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· .NET10 - 预览版1新功能体验(一)
历史上的今天:
2022-02-13 【682】拼多多相关设置
2017-02-13 【256】◀▶IEW-答案
2017-02-13 【255】◀▶IEW-Unit20
2017-02-13 【254】◀▶IEW-Unit19
2017-02-13 【253】◀▶IEW-Unit18
2017-02-13 【252】◀▶IEW-Unit17
2017-02-13 【251】◀▶IEW-Unit16
点击右上角即可分享
微信分享提示