Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

Excel Pivot Table Filter using python and win32com

I have a pivot table with a column name asset_name and with possible filter values like FCG~11J.6::~ABC-01~GF1234567004 , FCG~11J.7::~ABC-03~GF1907505749 , FCG~11J.9::~ABC-04~GF1907505742 etc

I'm trying to apply a filter using below command

pt_ws.PivotTables(pt_name).PivotFields("asset_name").ClearAllFilters()
pt_ws.PivotTables(pt_name).PivotFields("asset_name").PivotFilters.Add(21, None, "ABC-01")

21 = xlCaptionContains(Filters for all captions that contain the specified string). More info XlPivotFilterType enumeration

but it throws an error

File "C:PivotFilters.py", line 35, in Add
ret = self._oleobj_.InvokeTypes(181, LCID, 1, (9, 0), ((3, 1), (12, 17), (12, 17), (12, 17), (12, 17), (12, 17), (12, 17), (12, 17)),Type

com_error: (-2147352567, 'Exception occurred.', (0, None, None, None, 0, -2146827284), None)

The current workaround that I'm using is by writing the filter value directly to the cell

pt_ws.Cells(3, 2).Value = "FCG~11J.6::~ABC-01~GF1234567004"

But I would like to use the filter command as it's more flexible because of XlPivotFilterType

Thanks

question from:https://stackoverflow.com/questions/65840041/excel-pivot-table-filter-using-python-and-win32com

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...