[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 usingif sheet_name in wb.sheetnames
, and remove it usingwb.remove(wb[sheet_name])
. - Finally, we save the modified workbook using
wb.save()
.
分类:
Python Study
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 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