I am using Python to programmatically generate HTML. The HTML I want to generate is this:
<p>Hello <b>world</b> how are you?</p>
However, I do not know how to add the hello before the <b> tag and the string how are you? after the bold tag.
My code looks like this:
from xml.etree import ElementTree
p = ElementTree.Element('p')
b = ElementTree.Element('b')
b.text = 'world'
p.append(b)
Where would I add hello and how are you? The paragraph element only has one p.text field, and there does not seem to be a way to intersperse text and other HTML tags when building the document.
How can I programmatically generate an HTML document with both tags and text mixed together?
xmlmodule?ElementTreeis not the right tool for generating HTML. HTML is more free-form than XML. There are many template processors available that would be a better choice (jinja2, Cheetah, Django). Is there some reason you don't just want to generate strings here?xmlmodule because it was mentioned in this answer, however, I'm open to using a different library. stackoverflow.com/questions/6748559/…