scanny / python-pptx

Create Open XML PowerPoint documents in Python
MIT License
2.26k stars 498 forks source link

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

Closed jessie-pang closed 6 months ago

jessie-pang commented 6 months ago

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!

scanny commented 6 months ago

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

jessie-pang commented 5 months ago

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.

MartinPacker commented 5 months ago

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.