简体   繁体   English

Python win32com.client Excel xlTopCount

[英]Python win32com.client Excel xlTopCount

I'm trying to return only the top 10 values in my pivot table.我试图仅返回 pivot 表中的前 10 个值。 Here is the VBA这是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?我错过了什么?

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 .您的问题不包括您收到的错误消息(为什么不呢? ),但我猜您在win32c.xlTopCount收到 NameError,因为没有模块win32c Unless, of course you have some something like import win32com as win32c in which case you will get an AttributeError.当然,除非你有一些像import win32com as win32c这样的东西,在这种情况下你会得到一个 AttributeError。

xlTopCount is a VBA constant, not a PythonWin constant, and in general PythonWin does not have Python analogues for such constants. xlTopCount是一个 VBA 常量,而不是 PythonWin 常量,通常 PythonWin 没有 Python 此类常量的类似物。 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.检查您正在尝试执行的操作的文档将很快显示xlTopCount的值为 1。

So use 1 instead of the nonexistent constant win32c.xlTopCount .所以使用1而不是不存在的常量win32c.xlTopCount

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM