问题
I am working with pywin32, workbooks for Excel. I have some charts in a sheet that i have to save. The thing is that there's a slicer in this sheet that permit to filter on a certain variable. I didn't find pywin32 docs for it and i do not have any clue here on how i can (or can't) do it.. Thanks
回答1:
there is reference to Office objects. For slicers https://docs.microsoft.com/en-us/office/vba/api/excel.pivottable.slicers, be aware that slicer is an element of slicers collection
from win32com.client import Dispatch
xl = Dispatch("Excel.Application")
wb = xl.Workbooks.Open("your_file.xlsx")
sl = wb.SlicerCaches("your_slicer")
回答2:
Apparently in Excel, there is a program within it that could record anything you click so if you want to manipulate the filter/slicer, you can right click the element, and then choose "Assign Macro". Then you can click away as it records your clicks. Once youre done, you can view it by again choosing "Assign Macro" and a pop-up window will be available and you can choose your_filter/slicer_name_Click and it will provid you the VBA code. All you have to do is change it so it fits python format or you can implement it like this.
来源:https://stackoverflow.com/questions/56755462/is-there-a-way-to-access-slicers-on-excel-via-python-pywin32