3

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?

4
  • XML ≠ HTML so why are you using xml module? Commented Dec 19, 2021 at 0:17
  • ElementTree is 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? Commented Dec 19, 2021 at 0:19
  • I am using the xml module because it was mentioned in this answer, however, I'm open to using a different library. stackoverflow.com/questions/6748559/… Commented Dec 19, 2021 at 0:20
  • For my application, I am parsing an existing tree data structure and converting it to an HTML representation. It seemed like the most straightforward thing would be to generate an HTML tree structure using an existing library, and then use that to render the final HTML. I could write my own, if that is required, but it would be nice if I could use an existing solution. Commented Dec 19, 2021 at 0:22

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

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"})
I added a link to the docs, the start() method also takes optional attributes.
The code in the answer does not quite work. You cannot omit the second argument to start() ("TypeError: start expected 2 arguments, got 1").
@mzjn I’m running 3.8.9 and that code works for me. If you look at OP’s response, it seems to work for them, too. What version of Python are you running?
@mzjn, great catch! I just built 3.10 and yeah, I see that error now. I've updated my answer's code.
|
-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')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.