Prashant Kumar
Prashant Kumar

Reputation: 701

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??

Upvotes: 3

Views: 1425

Answers (1)

scanny
scanny

Reputation: 28863

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.

Upvotes: 4

Related Questions