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
633 views
in Technique[技术] by (71.8m points)

pywin32 - Python win32com.client Excel xlTopCount

I'm trying to return only the top 10 values in my pivot table. Here is the VBA

ActiveSheet.PivotTables("PivotTable2").PivotFields("Account Number"). _
        PivotFilters.Add2 Type:=xlTopCount, DataField:=ActiveSheet.PivotTables( _
        "PivotTable2").PivotFields("$$"), Value1:=11

I've tried several different appraoches like the below to no avail.

ws.PivotTables(pt_name).PivotFields('Charges').Type=win32c.xlTopCount
ws.PivotTables(pt_name).PivotFields('Charges').AddPivotFilters(ws.PivotTables(pt_name).PivotFields("$$",11).NumberFormat = win32c.xlTopCount

What am I missing?

question from:https://stackoverflow.com/questions/65944050/python-win32com-client-excel-xltopcount

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

1 Reply

0 votes
by (71.8m points)

Your question doesn't include the error message you get (why not?) but I am guessing you are getting a NameError at win32c.xlTopCount because there is no module win32c. Unless, of course you have some something like import win32com as win32c in which case you will get an AttributeError.

xlTopCount is a VBA constant, not a PythonWin constant, and in general PythonWin does not have Python analogues for such constants. You're expected to look them up. Checking the documentation for what you are trying to do will quickly show that the value of xlTopCount is 1.

So use 1 instead of the nonexistent constant win32c.xlTopCount.


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

...