首页 > 其他分享 >[972] Remove specific sheets from an Excel file

[972] Remove specific sheets from an Excel file

时间:2024-02-13 09:02:09浏览次数:23  
标签:sheet wb Excel Remove remove sheets workbook

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().

标签:sheet,wb,Excel,Remove,remove,sheets,workbook
From: https://www.cnblogs.com/alex-bn-lee/p/18014326

相关文章

  • [970] Combine multiple Excel files into one Excel file with multiple sheets
    YoucancombinemultipleExcelfilesintooneExcelfilewithmultiplesheetsusingthePandaslibraryinPython.Here'sageneralapproach:ReadeachExcelfileintoaPandasDataFrame.CreateanExcelwriterobjectusingPandas.WriteeachDataFra......
  • [971] [Keep original formats] Combine multiple Excel files into one Excel file w
    IftheexistingExcelfilehasspecialformattingthatpreventsreadingitdirectlywithPandas,youcanusealibrarylikeopenpyxltohandletheappendingofnewsheets.Here'showyoucanachievethis:importosfromopenpyxlimportload_workbook......
  • C#使用MiniExcel导入导出数据到Excel/CSV文件
    MiniExcel简介简单、高效避免OOM的.NET处理Excel查、写、填充数据工具。目前主流框架大多需要将数据全载入到内存方便操作,但这会导致内存消耗问题,MiniExcel尝试以Stream角度写底层算法逻辑,能让原本1000多MB占用降低到几MB,避免内存不够情况。特点:低内存耗用,避免OOM、频繁......
  • SharePoint Online 中Excel引用另一个Excel中的单元格
    前言最近,碰到一个需求,用户想要在SharePointOnline中从一个Excel文件引用另一个Excel文件的内容,所以就研究了一下。正文1.建一个测试的Excel文件,随便写点东西,如下图:2.新建另一个Excel,用来引用上一个Excel文件,如下图:3.在第一个Excel中的文件,选中单......
  • 第 4章 用 CSV 和 Excel 存储数据
    第4章用CSV和Excel存储数据4.1用CSV文件存储数据CSV(Comma-SeparatedValues)其实就是纯文本,用逗号分隔值,可以分隔成多个单元格。CSV文件除了可以用普通的文本编辑工具打开,还能用Excel打开,但CSV和Excel有以下不同:所有值都是字符串类型。不支持设置字体颜色和样......
  • 43、excel快速填充序列号,删除行时序号自动跟上
    平时填充序号的做法:首先在第1、2行输入1、2,然后用手往下拖动,填充后面的行,缺点:当我删除一行时,后面的序号不会自动按顺序填充上 解决方法:1、在excel上选中A6单元格,然后左上角输入A6:A110,按【回车】键2、直接输入【=ROW()-1】,再按【ctrl+回车】键盘就可以了缺点:由于公......
  • #排列组合#CF1550D Excellent Arrays
    洛谷传送门CF1550D分析对于excellent的\(a\)来说\(|a_i-i|=x\)的值是固定的,考虑枚举它一半正一半负时函数值是最大的,当\(n\)为奇数时要分为两种情况(不过可以通过杨辉三角合并)问题是,由于\(l,r\)的范围,并不能做到所有位置都能可正可负,不过不超过\(mn=\min\{1-l,r-n\}......
  • 【office编程】VB删除指定excel中行
    打开Excel中的VisualBasic编辑器,键入以下代码:SubBatchDelete()DiminputstrAsStringDimresult()AsStringDimsheet_no()AsStringDimstart_end()AsStringDimi,n,num,WS_CountAsIntegerDimwsAsWorksheetinputst......
  • [office] Excel怎么设置从指定部分分页打印
    excel表格中的指定一部分数据想分成两页打印,该怎么设置呢?下面就跟小编一起看看吧。Excel设置从指定部分分页打印的步骤1、现有如下字段:按班级进行打印。一班的同学打印在一张纸上。二班的同学打印在一张纸上!2、点击视图菜单下面的分页预览3、选择要分割的......
  • python爬虫爬取豆瓣电影top250并写入Excel中
    importrequestsimportreimportopenpyxl#创建工作表wb=openpyxl.Workbook()ws=wb.active#调整列距forletterin['B','C']:ws.column_dimensions[letter].width=66#发送网络请求headers={"User-Agent":'Mozilla/5.0(WindowsNT10.0;Win64;x64)......