Is there a way to access Slicers on Excel via Python (pywin32)

梦想与她 提交于 2020-08-05 10:08:50

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!