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

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

这应该又快又简单,我只是想将一些 VBA 转换成 python,我相信我不明白这里的循环是如何工作的。

基本上,我试图计算图表中有多少个系列,然后使用 for iseries in range(1, nseries):

遍历这些系列

我最终收到以下错误:

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

完整脚本如下。打印语句是我尝试查看循环是否正常工作并计算正确数量的 series/points 的尝试。这似乎也不起作用,因为没有打印任何内容,所以也许这就是问题所在?:

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')

这可能是因为 count 是一个函数,而不是一个属性。尝试将行更改为:

nseries = shape.chart.series.count()

然而,循环遍历系列的更好方法是直接执行而不是使用索引:

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