I'm fairly new to LaTeX and I want to generate some reports using Python. Here is some code that creates a simple table. After coding this up I began to wonder if there isn't a better way to do this. As you can see, it isn't pretty. I found this question on Tex.sx: Generating reports with LaTeX programmatically but it doesn't say much about Python.
I want to do more than just create tables. The reports can get quite complex. Are there better options for generating LaTeX using Python? I've seen Cheetah mentioned for templating but I've never used it before and I'm not sure that a templating engine would be sufficient. It also appears that LaTeX has its own programming language. I need to source some existing Python code so I don't see how I can use that. What do you recommend?
import pickle
projects = pickle.load(open('projects.p', 'rb'))
with open('projects.tex', 'w') as texfile:
texfile.write('\\documentclass[a4paper,twoside]{report}\n')
texfile.write('\\begin{document}\n')
texfile.write('\\textbf{Active Projects}\n')
texfile.write('\\begin{tabular}{|c|c|c|c|c|c|}\n')
row_fields = ('plant_owner', 'tiv_usd', 'project_name', 'city_state', 'rfq', 'completion')
for summary, details in projects.values():
values = [details.__dict__[field] for field in row_fields]
texfile.write('\\hline %s \\\\ \n' % ' & '.join(values))
texfile.write('\\end{tabular}\n')
texfile.write('\\end{document}\n')
Besides, there is plenty of precedence in TeX.sx for this type of question.
– stuckintheshuck Oct 02 '13 at 21:21templatemodule found in Standard library or, if your needs are more advanced, you can use a 3rd party library likejinja2. – pmav99 Oct 31 '14 at 16:42jinja2see http://eosrei.net/articles/2015/11/latex-templates-python-and-jinja2-generate-pdfs – asmaier Nov 04 '16 at 21:34