scanny / python-pptx

Create Open XML PowerPoint documents in Python
MIT License
2.37k stars 513 forks source link

max hyperlink url length #766

Closed JobyB closed 2 years ago

JobyB commented 2 years ago

Hi,

I have an application that generates a presentation of about 50 slides, each slide is a chart of different data points, each chart is made up of data pulled by a particular URL that selects and filters the data, I am trying to embed a link on each slide to get back to the data, and I also need that link to persist when the presentation is exported to a PDF, the following code works except when I have exceptionally long URLS the len of the 2 that break are 2268 and 2723

    def add_link(self, slide, url):

        # Right now we can't do really long urls
        if len(url) < 1024:
            height = Inches(0.3)
            top = Inches(7.7)
            width = Inches(0.75)
            left = 0

            shape = slide.shapes.add_textbox(left, top, width, height)
            tf = shape.text_frame
            r = tf.paragraphs[0].add_run()
            r.text = "Link"
            r.font.size = Pt(10)
            r.font.color.rgb = self.default_forecolor
            hlink = r.hyperlink
            hlink.address = url

When those long urls are allowed into the slides when I open the presentation I get an error from powerpoint "PowerPoint found a problem with content in data.pptx PowerPoint can attempt to repair the presentation"

I realize this is a very specific issue, but if there is a quick fix or workaround I can use, that would be great.

scanny commented 2 years ago

Hmm, I don't see any length limits in the XML schema for these elements. Does PowerPoint allow you to add hyperlink URLs of this length by hand (using the PowerPoint application)?

scanny commented 2 years ago

Interestingly, apparently Internet Explorer (Microsoft's browser, old one anyway) has a URL limit of 2083 characters, so that could explain a Microsoft product's distaste for very long URLs.

One alternative would be to generate tinyURLs for those. I expect there is some sort of RESTful API for that, but also I expect they charge if you do it very much.

Let's see what you find out with the "by hand" approach. Maybe do a binary search on the longest length allowed, possibly starting at 2083, then up or down by 100, then 50, etc. until you find the "this one works, add a character and it breaks" threshold.

JobyB commented 2 years ago

Thanks for the quick reply, it looks like it is indeed a limit on PowerPoint, the link below states: Powerpoint 2010 the limit on character is 1032.

https://social.msdn.microsoft.com/Forums/office/en-US/4e668216-3341-4784-a8d4-2c7023334ac2/hyperlink-limit-in-powerpoint-2007?forum=officegeneral

Sorry should have done more research before bugging you :)

MartinPacker commented 2 years ago

Note: That is PowerPoint 2010. It's possible (but not that likely) things have improved since then.

Are there any strategies you can employ to compress the URLs?

JobyB commented 2 years ago

Nope :( its a huge query string that can't be reduced, and due to corporate policy we are unable to use something like tinyURL. It's 2 URL out of about 35, so I guess we will just have to live with it, and leave the URL in the notes section.