通过win32com在Python中使用Excel透视表过滤器

时间:2018-05-10 10:50:42

标签: python python-3.x pywin32

我真的很难找到如何通过win32com在Python中使用这些简单的VBA代码行,但我找不到如何正确执行它:

ActiveSheet.PivotTables("PivotTable1").PivotFields("Quarters").ClearAllFilters
ActiveSheet.PivotTables("PivotTable1").PivotFields("Effective deadline"). _
    PivotFilters.Add2 Type:=xlBefore, Value1:="10/10/2017"

运行这些行时:

from win32com.client import DispatchEx

excel = DispatchEx('Excel.Application')
wb = excel.Workbooks.Open('myfile.xlsx')
ws = wb.Worksheets('MySheet')
ws.PivotTables(1).PivotFields("Quarters").PivotFilters('Add2', 'xlBefore', '10/10/2017')

我最终得到一个'参数数量无效',所以我想我很接近但是找不到文档来完成我的代码

有没有人设法做过这种工作?

1 个答案:

答案 0 :(得分:3)

你正在调用错误的方法。您应该在ws.PivotTables(1).PivotFields("Effective deadline").ClearAllFilters() ws.PivotTables(1).PivotFields("Effective deadline").PivotFilters.Add2(31, None, '10/10/2017') 属性后调用xlBefore

31

另请注意,您需要根据要应用的过滤器类型指定XlPivotFilterType Enumeration(在这种情况下为// match all shippers match (shipper:Shipper) // create a list containing all shippers and pass to the next context with collect(shipper) as shippers // match all customers which has purchased an order with each shipper match (customer:Customer) where all (shipper in shippers where (customer)-[:PURCHASED]->(:Order)-[:SHIP_VIA]->(shipper)) // return customers return customer = pd.Grouper