eea / odfpy

API for OpenDocument in Python
GNU General Public License v2.0
308 stars 63 forks source link

add parent attribute to Element #65

Closed vpoulailleau closed 6 years ago

vpoulailleau commented 6 years ago

Add a 'parent' attribute to Element that allows auto-addElement (the old syntax is still supported).

AFAIK 'parent' is not an attribute of any OpenDocument element, so we can use it to specify the Element in which to be created.

See the example passwd-as-ods.py:

f = open('/etc/passwd')
for line in f:
    rec = line.strip().split(":")
    tr = TableRow()
    table.addElement(tr)
    for val in rec:
        tc = TableCell()
        tr.addElement(tc)
        p = P(stylename=tablecontents,text=unicode(val,PWENC))
        tc.addElement(p)

becomes

with open('/etc/passwd') as f:
    for line in f:
        rec = line.strip().split(':')
        tr = TableRow(parent=table)
        for val in rec:
            tc = TableCell(parent=tr)
            p = P(parent=tc, stylename=tablecontents, text=val)

I haven't updated every example and documentation yet, because I wanted to get your opinion on this API change.