Home » excel » python – Can't get Excel ChartObjects Count

python – Can't get Excel ChartObjects Count

Posted by: admin May 14, 2020 Leave a comment


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)



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
    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
How to&Answers:

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