I want to load an XML template from one file, modify it, and save the results a new file with formatting. However pretty printing is not adding the desired formatting. Other solutions on stack are for when the tree is written back to the same file, but not a new one. For example:
from lxml import etree as ET
parser = ET.XMLParser(remove_blank_text=True)
tree = ET.parse("template.xml", parser)
root = tree.getroot()
A = ET.SubElement(root, "A")
ET.SubElement(A, "a")
B = ET.SubElement(root, "B")
ET.SubElement(B, "b")
tree.write("output.xml", pretty_print=True)
template.xml
<document>
</document>
output.xml is written without formatting
<document>
<A><a/></A><B><b/></B></document>