| Version 2 (modified by ged, 5 years ago) |
|---|
In this page I'll show you how you can create OpenOffice? documents using relatorio. First we need some objects to work on. Let's create a fake invoice object:
class Invoice(dict): @property def total(self): return sum(l['amount'] for l in self['lines']) @property def vat(self): return self.total * 0.21 inv = Invoice(customer={'name': 'John Bonham', 'address': {'street': 'Smirnov street', 'zip': 1000, 'city': 'Montreux'}}, lines=[{'item': {'name': 'Vodka 70cl', 'reference': 'VDKA-001', 'price': 10.34}, 'quantity': 7, 'amount': 7*10.34}, {'item': {'name': 'Cognac 70cl', 'reference': 'CGNC-067', 'price': 13.46}, 'quantity': 12, 'amount': 12*13.46}, {'item': {'name': 'Sparkling water 25cl', 'reference': 'WATR-007', 'price': 0.4}, 'quantity': 1, 'amount': 0.4}, {'item': {'name': 'Good customer rebate', 'reference': 'BONM-001', 'price': -20}, 'quantity': 1, 'amount': -20}, ], id='MZY-20080703', status='late')
So we created an invoice for the famous Led Zeppelin's drummer and his favorite addiction.
The next thing to do is to create a template for invoices. We will use the one displayed below. To create the genshi directives, you just need to create a text-type placeholder field, and fill it with the expression you want to use.
You can now start to use relatorio to create John Bonham's invoice.
from relatorio.templates.odt import Template basic = Template(source=None, filepath='basic.odt') file('bonham_basic.odt', 'w').write(basic.generate(o=inv).getvalue())
On the first line we import the odt Template engine. This class has the same signature as the one from genshi but uses only the filepath arguments. It returns a StringIO object that can be used to write the report on the disc.
And so here is our invoice with all the fields completed according to the Invoice object we created earlier. Notice how the style we set in the template are also applied in the resulting invoice.
We made use of the py:for directive. But it is not the only genshi directive supported by the relatorio odt plugin, it also support py:if, py:choose'''/'''py:when'''/'''py:otherwise and py:with.


