I don’t think there is a built-in way to do this, but if you find yourself doing it many times, it might be better to write a function that encapsulates the creation of a subitem and the text setting. Example -
def create_SubElement(_parent,_tag,attrib={},_text=None,nsmap=None,**_extra): result = etree.SubElement(_parent,_tag,attrib,nsmap,**_extra) result.text = _text return result
And then create your element as -
a = create_SubElement(root,'a',_text="Some text")
Note that with this, you cannot create an attribute named _text using keyword arguments; you will need to use the attribute keyword argument.
source share