win32com PowerPoint对象库中的__getitem__错误

1 投票
1 回答
996 浏览
提问于 2025-04-17 13:45

我在尝试让这个代码正常工作。

我用的代码和原文一模一样,但出现了下面的错误:

Traceback (most recent call last):
  File "<pyshell#26>", line 1, in <module>
    ppoint()
  File "<pyshell#24>", line 9, in ppoint
    s1a = s1.Shapes[0].TextFrame.TextRange
  File "C:\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 PowerPoint 14.0 Object Library.Shapes instance at 0x36209528>' object has no attribute '__getitem__'

Win32com导入得很顺利——如果你有什么想法,欢迎分享!

1 个回答

0

我觉得我遇到了同样的问题。在查看MSDN上的COM API后,我发现是语法出了错。你引用的页面上的正确代码应该是:

...
s1 = pres.Slides.Add(1, win32.constants.ppLayoutText)
sleep(1)
s1a = s1.Shapes(1).TextFrame.TextRange
s1a.Text = 'Python-to-%s Demo' % app
sleep(1)
s1b = s1.Shapes(2).TextFrame.TextRange
for i in RANGE:
     s1b.InsertAfter("Line %d\r\n" % i)
     sleep(1)
s1b.InsertAfter("\r\nTh-th-th-that's all folks!")
...

Shapes[1] 应该改成 Shapes(1)。而Rindsberg说得对,PowerPoint的COM API中的集合是从1开始的。

撰写回答