78

I need to generate documents from a web application and would like to do this using the Python language and LaTeX, are there any tools that will help me?

Edit
This Application will be hosted on Linux, we can run any external commands using popen, there is currently no defined input document format, nor any storage format, but output to the end user should be PDF.

Edit 2
These documents will have complex tables, graphs, and require typeset equations - hence the reason to use LaTeX. We would also prefer not to use intermediate files such as xml->html->pdf

Ideally I would like something like pyTeX or plasTeX that could render directly to PDF.

Frozenskys
  • 881
  • 1
  • 7
  • 6

15 Answers15

35

Recently I've written a library exactly for this purpose. It supports tables, plots, matrices and more. https://github.com/JelteF/PyLaTeX

JelteF
  • 481
  • 5
  • 5
17

This question should be closed because it actually has nothing to do with LaTeX and is more suited for StackOverflow. In any case the answer is that just like with HTML the best way is to use a templating system like Jinja2 and just output a LaTeX file. Once you have a LaTeX file simply use the subprocess module to run pdflatex (obviously you need it installed on your server). Don't forget to use the "-interaction nonstopmode" flag. I could go into technical details but again it's really more suited for a different site.

16

PyTeX is an Open Source project allowing to use TeX from within Python.

Stefan Kottwitz
  • 231,401
15

Bit late for an answer, but would like to share my experience. I had a similar problem. Basically needed to get output from Python application in pdf form. Had a look at various alternatives

  1. Jinja2
  2. Reportlab
  3. Pollyreports
  4. As well as the options listed above

Eventually I settled on using Latex. Basically just wrote a small class that assembled my elements into a tex file and then ran pdflatex on the generated tex file. Other options had a lot of control, but tex already has great formatting predefined and I just needed a professional container for my figures and tables.

Joop
  • 293
9

Is this what you're asking about? ( I used TeX to save code-space, but LaTeX is the same). If it is the what you're asking about, and you cut and paste this example, make sure the indents are correct after the paste.

#!/usr/bin/env python
import sys, os, os.path
filename = sys.argv[1]
opfile = sys.argv[1] + '.tex'
outfile = open(opfile, 'w')
pageAry = []
def a_tex_file(title):
    global pageAry
    pageAry.append('\\vskip2em\n\\font\\titlefont=cmr12 at 14.4pt\n\\font\\default=cmr12\n')
    pageAry.append('\\def\\today{January 21, 2011}\n')
    pageAry.append('\\centerline{\\titlefont ' + title + '}\n\\vskip5pt\n\\vskip5pt\\centerline{\\default blahblahblah}\n')
    pageAry.append('\n\\bye')
return 1

a_tex_file("blunk")

for i in pageAry:
    outfile.writelines(i)
outfile.close()
os.system('tex '+ opfile)
os.system('xdvi ' + filename + '.dvi & ')
doncherry
  • 54,637
bev
  • 1,669
8

Depending on what you want to do, Sphinx may suit you. I think its the best Python-based tool for technical documentation, and it supports restructured text.

8

Depending on exactly what you want to do, you may want to take a look at plasTeX. It's a python version of the TeX engine. It's not a true LaTeX interpreter, but if you have control over the input format of the documents then it could be possible to write them in such a manner that plasTeX can render them. At present, it renders the document to XHTML.

So if you wanted web-viewable copies, you could have it so that your documents were sufficiently simple that plasTeX can read them, then use plasTeX for XHTML-rendering and call pdflatex externally for PDF.

Andrew Stacey
  • 153,724
  • 43
  • 389
  • 751
7

While I love LaTeX, you probably don't want or need TeX to solve your problem. Check out ReportLab Toolkit

ptman
  • 171
7

PyX is a useful package if you want graphs and charts.

Jared Updike
  • 1,493
4

If you want to convert a web page into a pdf, maybe the better way is using the python-pisa package, perform a direct conversion, I used it in a django projects for this purpose.

4

A very good programming guideline is separation of logic from presentation.

This is done in professional Web development (see e.g. Django). If you think not about html pages, but pdf, the answer is the same.

The more you mix your code logic with presentation, the more difficult it is to support and modify that. Are you planning to re-launch all your computations if you want to change a caption of one table? Are you going to reprocess all rendering when you want to change some internal implementation of a function?

That's why I think that calling Python from LaTeX or creating LaTeX strings from Python is in general not a very good idea. It is possible though when you store logic in one place (Python files) and presentation in another place (LaTeX templates), and join them in the third place (aka Model-Template-View or Model-View-Controller architecture).

Unfortunately, it is not straightforward to just take Jinja2 (as @user573 proposed) and render LaTeX templates with that: you have to define the template language for LaTeX (which is not so hard). Then you have to run pdflatex (or other command) via Python's subprocess module. I also run pdftoppm to generate png images from pdfs (those are easier to slide and can be used in Web, if one needs that). I don't copy the exact code here (see yourself the link below), because it would lead to further questions.

I created a data analysis framework in Python, which allows to easily generate many figures with LaTeX quality, while keeping your data, logic and presentation separate. It can be used outside data analysis, because it is a general architectural framework (you can render figures, or tables, or any text - there are no artificial limitations for that). It follows functional programming style (and in some places object-oriented approach). You can check examples and the approach in the first part of Lena tutorial. If an "architectural framework" sounds frightening, I should add that the 1st part would already be enough to create many plots.

UPD: I forgot to mention that Lena doesn't recreate already produced plots if they were not changed (neither the data, nor the template). This is a useful optimization when creating many figures (and adding new ones).

3

check latexmake. It allows easily to create your pdf files with python.

pmav99
  • 6,060
2

Where does the data for the PDF you want to generate come from? A database?

I ask because (despite being a big Python fan) I once used PHP to generate a latex file with data populated from a database (this was for a very small conference proceedings). It's a bit messy, but works reasonably well; you can easily intermingle PHP code which pulls from the database with latex source, in the same way that you can mix PHP with HTML. Then just compile the resulting latex file to get a PDF.

Neil Olver
  • 4,942
1

For folks interested in a newer solution, I also ran into this problem and coded up a workaround. Repo has more information: (py2tex)

Basically, you surround your Python code with tags. Anything you print inside these tags is inserted into your final tex file! For example:

\documentclass{article}
\begin{document}

print('Python \textrightarrow \LaTeX!')

\end{document}

becomes:

\documentclass{article}
\begin{document}

Python \textrightarrow \LaTeX!

\end{document}

All you need to do is run the py2tex.py utility from the linked GitHub repo. Alternatively, you can hook into it from latexmk using a custom dependency (instruction for this also in the git repo).

This tool saved me a lot of time and heartache. I hope you like it!

mrplants
  • 111
1

Python library tikzpy is an option, it allows creating tikz images code via Python

fco
  • 129