Search code examples
pythonpowerpoint

TypeError: 'method' object cannot be interpreted as an integer


This should be quick and easy, I'm just trying to convert some VBA into python and I believe I don't understand how loops work here.

Basically I'm trying to count howmany series there are in a chart, then iterate through those series with for iseries in range(1, nseries):

And I end up getting the following error:

Traceback (most recent call last): File "xx.py", line 10, in for iseries in range(1, nseries): TypeError: 'method' object cannot be interpreted as an integer

Full script below. The print statements is my attempt at trying to see if the loop worked correctly and counted the correct amount of series/points. That also seems to not work as nothing has been printed, so maybe this is the issue?:

from pptx import Presentation

prs = Presentation('Test.pptx')
for slide in prs.slides:
        for shape in slide.shapes:
            if not shape.has_chart:
                continue
            nseries = shape.chart.series.count
            print('Series number:', nseries)
            for iseries in range(1, nseries):
                series = shape.chart.series(iseries)
                npoint = series.points.count
                print('Point number:', npoint)
prs.save('test3.pptx')

Solution

  • This is likely because count is a function, not an attribute. Try changing the line to:

    nseries = shape.chart.series.count()

    A better way to loop through the series, however, would be to do it directly instead of using an index:

    for series in shape.chart.series:
        # do something with series