Can't get Excel ChartObjects Count

2019-05-31 12:59发布

Is there anyone know how to get Excel ChartObjects Count? In VBA example, I can get all the Chart name and chart count in one sheet. but I don't know why , python can't get charts count. I try all other ChartObjects property, all work except 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

Error from 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条回答
Lonely孤独者°
2楼-- · 2019-05-31 13:27

Answer my question: The ChartObjects is a function. so can't have count attribute. Below code segment fixs the issue in both Python and Visual Basic.

Note, VBA doesn't need this step.

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

VBA: 
ActiveWorkbook.Sheets(lngS).ChartObjects.Count
查看更多
登录 后发表回答