Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python-使用筛选器()删除excel工作表_Python_Excel_Filter - Fatal编程技术网

Python-使用筛选器()删除excel工作表

Python-使用筛选器()删除excel工作表,python,excel,filter,Python,Excel,Filter,我只想使用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 inc

我只想使用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属性,然后过滤器设置应该这样做。例如,我尝试了“工作表”,但似乎不起作用(也不会抛出错误…)


有什么想法吗?

对于您正在使用的筛选过程,您不需要打开工作簿。您可以使用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__))
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()
如果您希望继续使用Win32并实际打开Excel,则可以使用以下代码:

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__))
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 client,您知道解决方案会是什么样子吗?只是好奇(我的工作场所有一个旧版本的openpyxl,更新到最近的版本是非常乏味的),你知道如何保存“新”工作簿(即,带有过滤的工作表)?