无法获取Excel ChartObjects Count

时间:2013-01-31 08:15:59

标签: python excel charts

有没有人知道如何获得Excel ChartObjects Count? 在VBA示例中,我可以在一个工作表中获取所有图表名称和图表计数。 但我不知道为什么,python无法获得图表计数。我尝试了所有其他ChartObjects属性,除Count之外的所有工作。

[MSDN Library][1]
ChartObjects.Application Property (Excel)
Not Working ===> ChartObjects.Count Property (Excel)
ChartObjects.Creator Property (Excel)
ChartObjects.Height Property (Excel)
ChartObjects.Left Property (Excel)
ChartObjects.Locked Property (Excel)
ChartObjects.Parent Property (Excel)
ChartObjects.Placement Property (Excel)
ChartObjects.PrintObject Property (Excel)
ChartObjects.ProtectChartObject Property (Excel)
ChartObjects.ShapeRange Property (Excel)
ChartObjects.Top Property (Excel)
ChartObjects.Visible Property (Excel)
ChartObjects.Width Property (Excel)

ex.
Python:
   workbook.Sheets(2).ChartObjects.Count

VBA:
   ActiveWorkbook.Sheets(lngS).ChartObjects.Count

Python出错:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "F:\Python27\lib\site-packages\win32com\client\__init__.py", line 465, in
 __getattr__
    raise AttributeError("'%s' object has no attribute '%s'" % (repr(self), attr
))
AttributeError: '<win32com.gen_py.Microsoft Excel 14.0 Object Library.ChartObjec
t instance at 0x51078984>' object has no attribute 'Count'

  [1]: http://msdn.microsoft.com/en-us/library/ff846604%28v=office.14%29.aspx

1 个答案:

答案 0 :(得分:3)

回答我的问题: ChartObjects是一个函数。所以不能有count属性。 下面的代码段解决了Python和Visual Basic中的问题。

注意,VBA不需要这一步。

Python or VB:
oWorksheet = workbook.Worksheets("Created.vs.Resolved")
oChart = oWorksheet.ChartObjects()
chart_count = oChart.Count

VBA: 
ActiveWorkbook.Sheets(lngS).ChartObjects.Count