我尝试按照https://groups.google.com/forum/#!topic/python-pptx/nrUkiQQHyZo中讨论的示例进行操作,但最终出现错误'Slide' object has no attribute 'rels'
。
我正在试验的最小代码尝试在第一张幻灯片到第三张幻灯片中建立一个超链接。
import pptx
import pptx.enum
from pptx import Presentation
from pptx.enum.action import PP_ACTION
from pptx.opc.constants import RELATIONSHIP_TYPE as RT
#very minimal Powerpoint template
prs = Presentation("tpl.pptx")
# create 3 slides, each with single text placeholder
s1 = prs.slides.add_slide(prs.slide_layouts[0])
s2 = prs.slides.add_slide(prs.slide_layouts[0])
s3 = prs.slides.add_slide(prs.slide_layouts[0])
# create internal hyperlink
rId = s1.part.relate_to(s3, RT.SLIDE)
r = s1.placeholders[10].text_frame.paragraphs[0].add_run()
r.text = "Link to Slide 3"
rPr = r._r.get_or_add_rPr()
hlinkClick = rPr.add_hlinkClick(rId)
hlinkClick.set('action', 'ppaction://hlinksldjump')
# ERROR WHEN SAVING - Slide' object has no attribute 'rels #
prs.save("Test.pptx")
如何解决错误?或者,有没有更简单的方法来实现这一点?
编辑:全栈跟踪
AttributeError Traceback (most recent call last)
<ipython-input-2-60361164c736> in <module>()
23
24 # ERROR WHEN SAVING - Slide' object has no attribute 'rels #
---> 25 prs.save("Test.pptx")
C:\Users\x\Anaconda3\lib\site-packages\pptx\presentation.py in save(self, file)
44 to a file (a string) or a file-like object.
45 """
---> 46 self.part.save(file)
47
48 @property
C:\Users\x\Anaconda3\lib\site-packages\pptx\parts\presentation.py in save(self, path_or_stream)
116 object.
117 """
--> 118 self.package.save(path_or_stream)
119
120 def slide_id(self, slide_part):
C:\Users\x\Anaconda3\lib\site-packages\pptx\opc\package.py in save(self, pkg_file)
162 a file (a string) or a file-like object.
163 """
--> 164 for part in self.parts:
165 part.before_marshal()
166 PackageWriter.write(pkg_file, self.rels, self.parts)
C:\Users\x\Anaconda3\lib\site-packages\pptx\opc\package.py in parts(self)
139 package.
140 """
--> 141 return [part for part in self.iter_parts()]
142
143 def relate_to(self, part, reltype):
C:\Users\x\Anaconda3\lib\site-packages\pptx\opc\package.py in <listcomp>(.0)
139 package.
140 """
--> 141 return [part for part in self.iter_parts()]
142
143 def relate_to(self, part, reltype):
C:\Users\x\Anaconda3\lib\site-packages\pptx\opc\package.py in iter_parts(self)
54 yield part
55
---> 56 for part in walk_parts(self):
57 yield part
58
C:\Users\x\Anaconda3\lib\site-packages\pptx\opc\package.py in walk_parts(source, visited)
51 yield part
52 new_source = part
---> 53 for part in walk_parts(new_source, visited):
54 yield part
55
C:\Users\x\Anaconda3\lib\site-packages\pptx\opc\package.py in walk_parts(source, visited)
51 yield part
52 new_source = part
---> 53 for part in walk_parts(new_source, visited):
54 yield part
55
C:\Users\x\Anaconda3\lib\site-packages\pptx\opc\package.py in walk_parts(source, visited)
51 yield part
52 new_source = part
---> 53 for part in walk_parts(new_source, visited):
54 yield part
55
C:\Users\x\Anaconda3\lib\site-packages\pptx\opc\package.py in walk_parts(source, visited)
42 """
43 def walk_parts(source, visited=list()):
---> 44 for rel in source.rels.values():
45 if rel.is_external:
46 continue
AttributeError: 'Slide' object has no attribute 'rels'