12

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')
stuckintheshuck
  • 233
  • 1
  • 2
  • 6
  • 1
    This looks more on-topic for StackOverflow than us: sounds like the key issue is Python not TeX. – Joseph Wright Oct 02 '13 at 17:48
  • A few months ago I created a presentation for "LaTeX & Python", you'll find it in my blog: http://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 02 '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 02 '13 at 21:19
  • 9
    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 02 '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 02 '13 at 21:30
  • 2
    I've created a library just for this. I hope its still helpfull. https://github.com/JelteF/PyLaTeX – JelteF Oct 01 '14 at 10:58
  • 1
    You want to use templates. Either the template module found in Standard library or, if your needs are more advanced, you can use a 3rd party library like jinja2. – pmav99 Oct 31 '14 at 16:42
  • For an example with the python templating engine jinja2 see http://eosrei.net/articles/2015/11/latex-templates-python-and-jinja2-generate-pdfs – asmaier Nov 04 '16 at 21:34
  • The same question is already asked on the site: https://tex.stackexchange.com/questions/885/how-can-i-use-latex-from-python – user202729 Sep 04 '21 at 09:22

0 Answers0