scanny / python-pptx

Create Open XML PowerPoint documents in Python
MIT License
2.4k stars 519 forks source link

How to access XML elements that are not yet available via python-pptx api #626

Open slo1958 opened 4 years ago

slo1958 commented 4 years ago

Hi. I'm using the library to create drawings using shapes and elbow connectors. Those connectors appear as an horizontal line fragment, a vertical line fragment and another horizontal line fragment. By default, the vertical line fragment is at 50% distance between starting point and ending point.

Playing with msPowerpoint and checking the generated xml, I discovered that changes to the default setting are recorded under the tag, which is empty and appears as in the xml generated by python-pptx. I would like to add a child element there, in order to get something like </a:avLst>

The current python-pptx api does not provide a direct way to alter the position of the intermediate line via a property or a function .

I do not ask to update the api, but just an example how to create the child element under

Thanks,

willkim1 commented 4 years ago

If you have a child element, try using object.element.insert_element_before().

It's in build\lib\pptx\oxml\xmlchemy.py as def insert_element_before.

    def insert_element_before(self, elm, *tagnames):
        successor = self.first_child_found_in(*tagnames)
        if successor is not None:
            successor.addprevious(elm)
        else:
            self.append(elm)
        return elm

https://python-pptx.readthedocs.io/en/latest/dev/xmlchemy.html

If that doesn't work, I think you need to fork and write your own function.