scanny / python-pptx

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

Dealing with ampersands in add_picture #759

Closed materialguy closed 2 years ago

materialguy commented 2 years ago

I'm getting an XML error when trying to add a picture. I did some searching and the root cause is there is an ampersand in the picture file name. I found this thread on stack overflow (https://stackoverflow.com/questions/38461983/unknown-error-in-python-pptx-module-add-picture/) which is identical to what I'm dealing with. I unfortunately cannot change the picture filenames so I need to find a work around. Scanny mentions that the name needs to be escaped before writing. Is this something I can implement myself? If so how?

MartinPacker commented 2 years ago

How about a global find and replace with & - which is the entity reference?

materialguy commented 2 years ago

I tried a local version of this already within the add_picture argument:

prs.slides.shapes.add_picture(filename.replace('&', '&'))

It cleared the XML error but then gave me a FileNotFoundError so it's not able to find the picture after I do that. I don't understand the underlying XML very well but it sounds like I need to do that but at a deeper level somewhere after the picture is already called. I'm open to any suggestions.

scanny commented 2 years ago

@materialguy what version are you running? I retired this issue in the latest release with this code and the test in that same commit: https://github.com/scanny/python-pptx/commit/127d73ab29a5729cc026bc4426e7fb7abaf5d647#diff-8e59c8e26c1c0cd74b68b6cdf39b0ae91d4ca11615845e8a56b75fde8a48be5cL64-R70

If for some reason that's not working I'd like to work out why :)

materialguy commented 2 years ago

That was it, I was running 0.6.19. Upgraded to 0.6.21 and the problem is now solved. Thanks Scanny!

scanny commented 2 years ago

Glad you got it working :)