I am generating an XML document in Python using an ElementTree, but the tostring function doesn\'t include an XML declaration when converting to plaintext.
tostring
Another pretty simple option is to concatenate the desired header to the string of xml like this:
xml = (bytes('\n', encoding='utf-8') + ET.tostring(root)) xml = xml.decode('utf-8') with open('invoice.xml', 'w+') as f: f.write(xml)