scanny / python-pptx

Create Open XML PowerPoint documents in Python

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Is it possible to add support for "/select,file:///..." URIs?

numoe opened this issue · comments

Thanks for the python-pptx package. It makes it easy to work with and convert powerpoint documents.

Is it possible to add support for "/select,file:///..." URIs in python-pptx? The intention is to use the /select, URI scheme in Windows to convert URLs from a presentation that hyperlinks to files on Google drive to instead open Windows Explorer on a local mirror disk with the given file selected, so that it easily can be dragged into the desired application (e.g. Adobe Premiere Pro).

When running the following example:
...
r = p.add_run()
r.text = 'link to python-pptx @ GitHub'
hlink = r.hyperlink
hlink.address = '/select,file:///home/nils/Bilete/2024/20231107_110023.mp4'
prs.save("test.pptx")

then python-pptx assumes that / is the root of a file path and inserts "file:/// before /select, which breaks the URI scheme on Windows:
file:///select,file:///home/nils/Bilete/2024/20231107_110023.mp4

How can this issue be fixed? If you point me in the right direction in the code, then I can try to help out fixing this issue. If it is not desirable to include it in the codebase, then it would be useful for me to have as a local patch.

Hmm, I can't find a place where python-pptx is making that replacement.

How do you know that's what's happening? I think you would need to inspect the "rels file" for the slide in question, it will be in the .pptx file (which is a zip archive) named something like ppt/slides/_rels/slide1.xml.rels.

You're looking for an entry like:

<Relationship Id="rId2" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/{something like hyperlink}" Target="some-url"/>

From what I can tell, whatever you give to hlink.address goes here without any changes. So you might need to check the same entry on a .pptx file that behaves the way you want to see what it puts there and how to copy it.

The issue is not in python-pptx as you say. I did further debugging, and python-pptx does indeed produce the correct URI. Debugging the function relate_to() and printing self._rels.xml gave:

b'<?xml version=\'1.0\' encoding=\'UTF-8\' standalone=\'yes\'?>\n<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"><Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/slideLayout" Target="../slideLayouts/slideLayout2.xml"/><Relationship Id="rId2" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink" Target="/select,file:///home/nils/Bilete/2024/20231107_110023.mp4" TargetMode="External"/></Relationships>'

I verified the PPT indeed included the /select,file:///... URI by unzipping the pptx file and searching all files for this URI.

The problem I experience is elsewhere (in LibreOffice, which I used).

Sorry for any inconvenience and thanks for checking the bug report!
Nils

No worries @numoe :)