Accessing the PowerPoint XML to add imgEffect using python-pptx

Viewed 77

How would I go about adding custom XML to a PowerPoint file to add image effects? If I change a14 to a it will add but not do anything if I have as a14 (like the xml is on a manually generated file). I get the error

File "C:\Program Files\Python39\lib\site-packages\pptx\oxml\xmlchemy.py", line 28, in OxmlElement nsptag = NamespacePrefixedTag(nsptag_str) File "C:\Program Files\Python39\lib\site-packages\pptx\oxml\ns.py", line 62, in init self._ns_uri = _nsmap[self._pfx] KeyError: 'a14'

Does anyone have any pointers as to where I'm going wrong please? I'm new to both Python & Python-PPTX and normally can work things out but this one has got my brain fried!

def AddEffects(shape):


    pic = shape._pic
    rId = pic.xpath('./p:blipFill/a:blip')[0]


    jj = OxmlElement("a:biLevel")
    jj.set("thresh","50000")

    rId.append(jj)


    el = OxmlElement("a:extLst")
    jj.append(el)

    saturation = OxmlElement("a:ext")
    saturation.set("uri", "{BEBA8EAE-BF5A-486C-A8C5-ECC9F3942E4B}")

    el.append(saturation)

    saturation2 = OxmlElement("a14:imgProps")
    saturation2.set("xmlns", "http://schemas.microsoft.com/office/drawing/2010/main")
    saturation.append(saturation2)

    saturation3 = OxmlElement("a14:imgLayer")
    saturation3.set("embed", "rId3")
    saturation2.append(saturation3)

    saturation4 = OxmlElement("a14:imgEffect")
    saturation3.append(saturation4)

    saturation5 = OxmlElement("a14:saturation")
    saturation5.set("sat", "0")
    saturation4.append(saturation5)
1 Answers

I solved this by...

from pptx.oxml.ns import _nsmap as nsmap
nsmap['a14'] = "http://schemas.microsoft.com/office/drawing/2010/main"
Related