if not elem.tail or not elem.tail.strip():
    elem.tail = i
for elem in elem:
    indent(elem, level + 1)
if not elem.tail or not elem.tail.strip():
    elem.tail = i
elif level and (not elem.tail or not elem.tail.strip()):
    elem.tail = i
Изменение отступа по умолчанию для etree.tostring в lxml