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?
2 Answers 2
Regardless of how lenient/permissive the parsing of HTML by the rendering engine is, OP is asking how to responsibly build structured text.
Here's how to do build structure with ElementTree's TreeBuilder class, it's very straight-forward:
#!/usr/bin/env python3
#!/usr/bin/env python3
import xml.etree.ElementTree as ET
builder = ET.TreeBuilder()
builder.start('p', {})
builder.data('Hello ')
builder.start('b', {})
builder.data('world')
builder.end('b')
builder.data(' how are you?')
builder.end('p')
root = builder.close() # close to "finalize the tree" and return an Element
ET.dump(root) # print the Element
For what it’s worth, I see
<p>Hello <b>world...
as being very analogous to
<para>Hello <emphasis>world...
in Docbook XML.
7 Comments
TreeBuilder was exactly what I was looking for. How would you add "attributes" to this? (<p style="font-weight: bold">)? In my original example, you might use ElementTree.Element('p', attrib={"style": "font-weight: bold"})start() method also takes optional attributes.start() ("TypeError: start expected 2 arguments, got 1").You CAN do this, but you'd need to put the pieces of text into <span> tags. In my opinion, this is just a bad idea. HTML is not XML. There are much better tools.
import sys
from xml.etree import ElementTree as ET
html = ET.Element('html')
body = ET.Element('body')
html.append(body)
para = ET.Element('p')
b1 = ET.Element('span')
b1.text = "Hello"
b2 = ET.Element('b')
b2.text = "world,"
b3 = ET.Element('span')
b3.text = "how are you?"
para.append(b1)
para.append(b2)
para.append(b3)
html.append(para)
ET.ElementTree(html).write(sys.stdout, encoding='unicode', method='html')
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/…