如何使用 python 将超链接添加到 ppt 中的图像
How to add hyperlink to an image in a ppt using python
下面是一段在幻灯片中插入图片的脚本。我想给图片加超链接,但是不行
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 图像。
有什么建议吗??
超链接是形状上的一种点击操作,而不是直接属性。所以你需要使用 shape.click_action.hyperlink
来代替。
shape.click_action.hyperlink.address = 'https://github.com/scanny/python-pptx'
除了超链接之外,单击操作还可以提供跳转到演示文稿中另一张幻灯片的行为。这是在这里有一个额外的间接级别而不是让超链接直接出现在形状对象上的基本原理。
下面是一段在幻灯片中插入图片的脚本。我想给图片加超链接,但是不行
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 图像。
有什么建议吗??
超链接是形状上的一种点击操作,而不是直接属性。所以你需要使用 shape.click_action.hyperlink
来代替。
shape.click_action.hyperlink.address = 'https://github.com/scanny/python-pptx'
除了超链接之外,单击操作还可以提供跳转到演示文稿中另一张幻灯片的行为。这是在这里有一个额外的间接级别而不是让超链接直接出现在形状对象上的基本原理。