Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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')
share|improve this question

closed as off-topic by Werner, Thorsten, Martin Schröder, karlkoeller, Jubobs Oct 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.

1  
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
    
A few months ago I created a presentation for "LaTeX & Python", you'll find it in my blog: uweziegenhagen.de/?p=2658 It is still in German (I will translate it) but in it's current form it may be helpful. I haven't worked with pickle, so I'll try your code as well. –  Uwe Ziegenhagen Oct 2 '13 at 19:17
1  
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
3  
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
1  
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

Browse other questions tagged or ask your own question.