python-pptx icon indicating copy to clipboard operation
python-pptx copied to clipboard

Local Hyperlinks, Documentation demo

Open doakey3 opened this issue 8 years ago • 8 comments

I am trying to create a hyperlink to another slide in the same presentation.

From the documentation on hyperlinks, I understand I need to create a run like this:

    from pptx import Presentation

    filepath = 'test.pptx'
    pres = Presentation(filepath)
    slide = pres.slides[0]
    paragraph = slide.shapes[0].text_frame.paragraphs[0]

    run = paragraph.add_run()
    run.text = "hello world"
    run.hyperlink.address = 'https://github.com/scanny/python-pptx'

The documentation is a bit hard for me to understand regarding how to create a hyperlink to a target within the PowerPoint file itself. The quote from the documentation is:

Hyperlinks in PowerPoint can have four types of targets: .... another part in the same presentation. This uses an internal relationship (in the .rels item) to the target part.

Can you describe in detail how to create a target using the .rels item and how to hyperlink to that target? Thanks!

doakey3 avatar Jul 29 '18 02:07 doakey3

I found that you can hyperlink to a slide by formatting the "URL" like this:

run.hyperlink.address = "#Slide 3"

That did the trick.

doakey3 avatar Jul 29 '18 03:07 doakey3

There's a new feature in the last month or so enabling this more directly:

shape.click_action.target_slide = prs.slides[3]

http://python-pptx.readthedocs.io/en/latest/api/shapes.html#pptx.shapes.base.BaseShape.click_action

scanny avatar Jul 31 '18 16:07 scanny

How is this done with run using this new method?

eugesd avatar Oct 24 '18 20:10 eugesd

The .click_action property is currently only on a shape, not yet a Run object. So it's still the old method there if you're constrained to making a link from text (like a traditional hyperlink).

scanny avatar Oct 25 '18 13:10 scanny

hey scanny i want to create a text including hyperlink word in it like source:ancbhfd "jkl"asdf as u see jkl is my hyperlink text so how can i make it?

nikmane221 avatar Apr 27 '22 12:04 nikmane221

I found that you can hyperlink to a slide by formatting the "URL" like this:

run.hyperlink.address = "#Slide 3"

That did the trick.

Is "#Slide 3" in this example the name property of a slide, or the slide ID? And is the # symbol part of that name/ID, or the indicator that this is an internal link?

lowfuel avatar Jun 14 '22 16:06 lowfuel

I found that you can hyperlink to a slide by formatting the "URL" like this:

run.hyperlink.address = "#Slide 3"

That did the trick.

Is "#Slide 3" in this example the name property of a slide, or the slide ID? And is the # symbol part of that name/ID, or the indicator that this is an internal link?

I have the same question. @scanny or @doakey3 ?

viseshrp avatar Sep 01 '22 16:09 viseshrp

I played around with the link in powerpoint itself, it generates a link of this nature:

https://docs.google.com/presentation/d/1FWKElbximUY3QB5a-APbRl6Uo/edit#slide=id.p2

and if we will use #slide=id.p2 it works!

For myself, I have found the following to be a temporary solution:

run.hyperlink.address = f'#slide=id.p{slide_id}'

Warning: id of your slides may have different format, not suitable for everyone

limness avatar Dec 09 '23 23:12 limness