【问题标题】:TypeError: 'method' object cannot be interpreted as an integerTypeError:“方法”对象不能解释为整数
【发布时间】:2021-02-10 05:32:06
【问题描述】:

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

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

我最终得到以下错误:

Traceback(最近一次调用最后一次):文件“xx.py”,第 10 行,在 对于范围内的序列(1,nseries):TypeError:“方法”对象不能解释为整数

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

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

【问题讨论】:

  • 请使用完整的错误回溯更新您的问题。

标签: python powerpoint


【解决方案1】:

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

nseries = shape.chart.series.count()

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

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

【讨论】:

  • 感谢您的评论!我最终得到了以下错误:TypeError: count() missing 1 required positional argument: 'value' 那么我需要在括号内添加一些东西吗?
  • 我实际上不确定count() 方法的来源,因为文档中没有提到它。你可以做的是直接循环系列:for iseries, series in enumerate(share.chart.series): ...
  • 感谢您的回复!我认为这正是我所发现的,count 属性根本不存在......但是您使用 enumerate 的版本应该做完全相同的事情:) 我实际上将代码重新编写为 for series in chart.series 并在之后解决了它声明chart = shape.chart。如果您发布它,很高兴接受您的回答!
猜你喜欢
  • 2017-08-01
  • 2021-07-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-05
  • 2015-03-18
  • 2016-01-26
  • 1970-01-01
相关资源
最近更新 更多