Generating HTML from python

Magnus Lycka lycka at carmen.se
Tue Jun 14 06:10:28 EDT 2005


Philippe C. Martin wrote:
> I now need to generate the HTML wxHtmlEasyPrinting can print: I need to have
> a title followed by lines of text that do not look too ugly. If possible I
> would like to use an existing module.

How to do this really depends on what your data looks like, and how you
get it. E.g. if you just want uniform pages with paragraphs of plain
texts with headings in between, you can just make a template HTML file
with the main block of text replaced with %s, and then do something like:

text = []

for heading, paragraph in data_source:
     text.append('<h2>%s</h2>' % heading)
     text.append(paragraph)

templ = open('template.html').read()
print templ % '\n'.join(text)


If your data has more structure, you might find a tool like Fredrik
Lundh's elementtree useful.



More information about the Python-list mailing list