Reputation: 19372
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?
Upvotes: 3
Views: 2089
Reputation: 11223
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.
Upvotes: 3
Reputation: 54743
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')
Upvotes: -1