PyMOTW: Creating XML Documents with ElementTree

In addition to its parsing capabilities, ElementTree also supports creating well-formed XML documents from Element objects constructed in your application. The Element class used when a document is parsed also knows how to generate a serialized form of its contents, which can then be written to a file or other data stream.

Read more at etree/ElementTree/create.html