简体   繁体   中英

How to add hyperlink to an image in a ppt using python

Below is a piece of script which inserts a picture to the slide. I want to add hyperlink to the picture, but it doesn't work.

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!")

The script runs successfully but the hyperlink is not added to the png image in the presentation file.

Any suggestions??

A hyperlink is a type of click-action on a shape, not a direct attribute. So you need to use shape.click_action.hyperlink instead.

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

In addition to a hyperlink, a click-action can provide the behavior of jumping to another slide in the presentation. This is the rationale for having an additional level of indirection here rather than having hyperlink appear directly on the shape object.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM