scanny / python-pptx

Create Open XML PowerPoint documents in Python

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How can I keep the hyperlinks when converting html to ppt slide?

jessie-pang opened this issue · comments

I am trying to convert a html page to a PPT slide. The orginal html page has words with hyperlinks. I'd like to keep the same hyperlinks in PPT slides. What's the best way to do that?

I got the following sample codes to add hyperlinks, but that's for adding new text with hyperlinks:

p = shape.text_frame.paragraphs[0]
r = p.add_run()
r.text = 'link to python-pptx @ GitHub'
hlink = r.hyperlink
hlink.address = 'https://github.com/scanny/python-pptx'

https://python-pptx.readthedocs.io/en/latest/dev/analysis/txt-hyperlink.html

My expectation is:
HTML source code:

Please see the user guide.

In PPT slide: Please see the user guide. (There is a hyperlink on the passphase "user guide")

Thank you!

If you already have the text in a run, just start with the line hlink = run.hyperlink and take it from there.

Thanks Steve for your reply. I've tried "hlink = run.hyperlink". The thing is, sometimes multiple words are included in a run, for example:
In a run: "Please see the user guide."
However, I only want to put the hyperlink on "user guide".

Is there any way to control which/how many words are included in a run? I feel like it currently uses a run for one paragraph.

If you were including multiple font settings in a paragraph you'd have multiple runs. So it's not a single run per paragraph. You construct the paragraph from the runs you want. How to parse a string of text for things you want to turn into a "hyperlink run" is a different problem. Possibly solved using a regular expression or two.