【问题标题】:Python - Remove excel sheets using Filter()Python - 使用 Filter() 删除 Excel 工作表
【发布时间】:2020-11-29 13:44:30
【问题描述】:

我只想使用 Filter() 函数在 Excel 中包含某些工作表。

这是我目前的代码:

import win32com.client as win32
from pathlib import Path

win32c = win32.constants

excel = win32.gencache.EnsureDispatch('Excel.Application')
wb = excel.Workbooks.Open("C:/Prueba/GOOG.xlsm")

def included(sheet_name):
    l = ['Report_Data', 'Report_Main']
    if sheet_name in l:
        return True

wb.__Sheets__ = filter(included, [sheet.Name for sheet in wb.Sheets]) # wb.__Sheets__ doesn't work of course...

我的猜测是我需要从工作簿对象正确访问 Sheets 属性,然后过滤器设置应该这样做。例如,我尝试了“Sheets”,但似乎不起作用(也不会引发错误......)。

有什么想法吗?

【问题讨论】:

    标签: python excel filter


    【解决方案1】:

    对于您正在使用的筛选过程,您无需打开工作簿。您可以使用 openpyxl 加载文件并获取工作表名称。

    试试这个代码:

    from pathlib import Path
    import openpyxl
    
    wb = openpyxl.load_workbook('C:/Prueba/GOOG.xlsm')
    print("All Sheets:", wb.sheetnames)
    
    def included(sheet_name):
        l = ['Report_Data', 'Report_Main']
        if sheet_name in l:
            return True
    
    wb.__Sheets__ = filter(included, wb.sheetnames) # wb.__Sheets__ doesn't work of course..
    
    print(list(wb.__Sheets__))
    

    如果您更喜欢使用 Win32 并实际打开 Excel,您可以使用以下代码:

    from win32com.client import Dispatch
    import win32com
    import win32com.client as win32
    
    excel = win32com.client.dynamic.Dispatch('Excel.Application')
    excel.Visible = True
    
    wb = excel.Workbooks.Open("C:/Prueba/GOOG.xlsm")
    print("All Sheets:",[wb.Sheets(i+1).Name for i in range(wb.Sheets.Count)])
    
    def included(sheet_name):
        l = ['Report_Data', 'Report_Main']
        if sheet_name in l:
            return True
    
    ShtList = filter(included, [wb.Sheets(i+1).Name for i in range(wb.Sheets.Count)]) 
    
    print(list(ShtList))
    
    excel.Quit()
    

    这是删除多余工作表并将工作簿另存为新文件的完整代码。

    from win32com.client import Dispatch
    import win32com
    import win32com.client as win32
    from shutil import copyfile
    
    excel = win32com.client.dynamic.Dispatch('Excel.Application')
    excel.Visible = True
    
    filename = "C:/Prueba/GOOG.xlsm"
    filenamenew = "C:/Prueba/GOOG.New.xlsm"
    
    copyfile(filename, filenamenew)
    
    wb = excel.Workbooks.Open(filenamenew)
    print("All Sheets:",[wb.Sheets(i+1).Name for i in range(wb.Sheets.Count)])
    
    def remove(sheet_name):
        l = ['Report_Data', 'Report_Main']
        if not sheet_name in l:
            return True
    
    ShtList = list(filter(remove, [wb.Sheets(i+1).Name for i in range(wb.Sheets.Count)]))
    
    print("DelLst:",ShtList)
    
    excel.DisplayAlerts = False  # new prompt for delete
    
    for s in ShtList:
       print("del", s)
       wb.Worksheets(s).Delete()
    
    wb.Save()
    
    excel.DisplayAlerts = True
    excel.Quit()
    

    【讨论】:

    • 谢谢,如果我想使用win32客户端,你知道解决方案是什么样的吗?只是好奇(我的工作场所有一个旧版本的 openpyxl,并且将其更新到最新版本非常乏味)
    • 您知道如何保存“新”工作簿(即使用过滤表)吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-03
    • 2010-10-15
    • 2016-03-17
    • 1970-01-01
    相关资源
    最近更新 更多