[portland] Using LaTeX for Python Reports
Jeff Schwaber
freyley at gmail.com
Wed Oct 10 21:24:52 CEST 2007
On 10/10/07, Rich Shepard <rshepard at appl-ecosys.com> wrote:
> I've received some great suggestions from the LyX/LaTeX mail list. The
> most cogent, I believe, is the recommendation of using the string.Template()
> class for substitutions.
>
> Tell me what you think of this approach:
>
> The printing module imports that class:
>
> from string import Template
Rich,
This gets you into the world of templating, where there are many
options. But templating, the separation of style from data, is
definitely a good place to be going.
Cheetah is a python templating engine that takes this to the next
level. With files that are templates, you simply pass the template
arguments and it puts them in where you told it to. So for example,
your python code can look like this:
from Cheetah.Template import Template
varcounter = 'what'
print Template ( file = 'counter.tmpl', searchList = [{ 'counter':
varcounter }] )
And then in counter.tmpl you would simply use $counter as a variable
wherever you wanted it in the LaTeX.
The benefits of Cheetah (or any other similarly full-featured template
engine, of which there are several) are many:
1) Your style, your LaTeX code, isn't in your Python. You can change
it with impunity without messing up the code.
2) Objects can be passed into the template. You can use
$varcounter.strip() or $varcounter.endswith('stuff') as another, which
gets me into ...
3) Branching, repeating, etc. Cheetah supports while, if, and for.
Thanks,
Jeff
More information about the Portland
mailing list