AppyPrinciplesGetting started

The first, simplest and most obvious way to insert dynamic content into a document is to misuse LibreOffice fields.

By default, POD is configured to interpret fields of type "conditional text" as Python expressions. Such an expression is evaluated with the context as initially passed to the POD renderer. We will see later that POD statements can update this context.

Historically, with the first versions of POD, POD expressions were to be written in track-changed text. Although still activated for backward compatibility, this approach is not recommanded for writing new POD templates. The principal reason is that, when you copy/paste a chunk of ODT code containing POD expressions written in tracked-changed text, the tracked-changed text is converted to normal text in the copied chunk. It does not occur with fields. Althought I do not encourage writing POD templates by performing blind copy/paste operations from one template to another, or from one portion of ODT document to another, this copy/paste facility has been felt sufficiently useful to change the standard way to write expressions in POD.

Here is an example. The following ODT POD template contains some simple expressions, each one being the name of a Python variable.

Inserting a conditional field is done in LibreOffice via menu Insert > Field > More fields..., or by pressing keys CTRL + F2.

Within popup "Fields", select tab "Functions", and, as field type, select "Conditional text". Always write "true" in field "Condition" (else, the field and its content won't appear in the document), write your Python expression in field "Then", leave the "Else" field empty and click on the "Insert" button.

When you are done with your ODT template, launch the POD Renderer with the template illustrated above, and the following context.

expr1 = 'hello'
i1 = 45
f1 = 78.05

If your template is named Expressions.odt and is in the current working directory, the following code will call the Renderer to produce the result in a file named ExpressionsResult.odt.

from appy.pod.renderer import Renderer
expr1 = 'hello'
i1 = 45
f1 = 78.05
renderer = Renderer('Expressions.odt', globals(), 'ExpressionsResult.odt')

The result looks like this.

Note that, on the second "expr1" field, bold formatting has been applied and was kept in the result.

In the remainder of this section, I will assume you are comfortable with calling the Renderer: contexts will be specified more simply, without the complete call to the Renderer.