Templated-docs: office documents for Django¶
Templated-docs is a Django package that allows you to generate documents, including texts, spreadsheets and presentations, from templates. It utilises LibreOffice as an underlying conversion document mechanism.
Features¶
- Generate any LibreOffice-supported document from within Django.
- Use Django template language inside office documents.
- Create custom generation management commands to integrate with other systems.
Installation¶
To install templated-docs:
- Make sure you have LibreOffice >= 4.3.0 installed on the same machine
- Install a package with
pip install templated-docs - Add
templated_docstoINSTALLED_APPS
If you are using cpython you may need the libffi development package in
order to compile this module’s dependencies.
- Ubuntu:
apt-get install libffi-dev - MacOS:
brew install libffi
Usage¶
To generate a document from a template sample.odt you can write a view
like this:
from templated_docs import fill_template
from templated_docs.http import FileResponse
def get_document(request):
"""
A view to get a document filled with context variables.
"""
context = {'user': request.user, 'foo': 'bar'}
filename = fill_template('sample.odt', context, output_format='pdf')
visible_filename = 'greeting.pdf'
return FileResponse(filename, visible_filename)
templated_docs.fill_template(template_name, context, output_format='odt')- Fill a template
template_nameusing acontextdictionary as a context, optionally converting it to theoutput_format. Returns a filename of a generated file.