# Programmatically generating LaTeX with Python [closed]

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

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')

-

## closed as off-topic by Werner, Thorsten, Martin Schröder, karlkoeller, JubobsOct 2 '13 at 19:37

This question appears to be off-topic. The users who voted to close gave this specific reason:

• "This question does not fall within the scope of TeX, LaTeX or related typesetting systems as defined in the help center." – Werner, Thorsten, Martin Schröder, karlkoeller, Jubobs
If this question can be reworded to fit the rules in the help center, please edit the question.

This looks more on-topic for StackOverflow than us: sounds like the key issue is Python not TeX. – Joseph Wright Oct 2 '13 at 17:48
pickle is in the code just to show that the report is generated from an external data source (pickled objects in this case). It really has nothing to do with the core issue. I'll take a look at your presentation. – stuckintheshuck Oct 2 '13 at 21:19
If I were to move this question to stackoverflow, they would tell me to post it here. Please don't let the fact that a question falls in the middle ground between stackoverflow and another QA site be an excuse to not allow others to answer it. I believe there are many more LaTeX users that use programming to augment LaTeX than there are programmers that use LaTeX. Besides, there is plenty of precedence in TeX.sx for this type of question. – stuckintheshuck Oct 2 '13 at 21:21
Why weren't any of the questions in the Related sidebar closed for being off-topic? Most of them have answers! – stuckintheshuck Oct 2 '13 at 21:30
I've created a library just for this. I hope its still helpfull. github.com/JelteF/PyLaTeX – JelteF Oct 1 '14 at 10:58