doc = parseString(
    """<html>
    <head>
        <script type="text/javascript">
            var a = 'I love &aacute; letters'
        </script>
    </head>
    <body>
        <h1>And I like the fact that 3 > 1</h1>
    </body>
    </html>"""
    )
with open('foo.xhtml', 'w') as f:
    f.write(doc.toxml())
Инструменты для написания XML для Python