首页 > 解决方案 > 如何使用python将超链接添加到ppt中的图像

问题描述

下面是一段将图片插入幻灯片的脚本。我想为图片添加超链接,但它不起作用。

from pptx import Presentation
import pptx.util
from pptx.util import Inches, Pt

pptfile = r"C:\Users\prashant\NEW - Copy.pptx"
fgr = "gr.png"


prs = Presentation(pptfile)
slides = prs.slides
for slide in prs.slides:
    for shape in slide.shapes:
            pic = slide.shapes.add_picture(fgr, pptx.util.Inches(1), 
                  pptx.util.Inches(0.2),width=pptx.util.Inches(0.8), height=pptx.util.Inches(0.5))
            pic.name = "Picture 999"

    for shape in slide.shapes:
        if shape.name == "Picture 999":
            shape.address = 'https://github.com/scanny/python-pptx'
            # shape.hyperlink.address = 'https://github.com/scanny/python-pptx'    >>Gives Error

prs.save(pptfile)
print("Done!")

脚本成功运行,但超链接未添加到演示文件中的 png 图像。

有什么建议么??

标签: pythonhyperlinkpython-pptx

解决方案


超链接是对形状的一种单击操作,而不是直接属性。所以你需要shape.click_action.hyperlink改用。

shape.click_action.hyperlink.address = 'https://github.com/scanny/python-pptx'

除了超链接之外,单击动作还可以提供跳转到演示文稿中另一张幻灯片的行为。这是在此处使用额外的间接级别而不是让超链接直接出现在形状对象上的基本原理。


推荐阅读