Quick Hack: Generating PDFs with Python and XeTeX

A friend of mine is following a PhD in a non-technical field. And his boss is a bully. Work mainly happens with high-level statistical analysis tools. No one knows anything about programming and most problems are solved by hand. While on a positive note this means good chances to get a student job, it also means that progress moves slowly, especially when it comes to working with large datasets.

Due to some kind of disorder related to social dominance, his boss regularly gives him harassing tasks. The ones that would take two students a month to complete - with a deadline set for the day after tomorrow. Obviously, this is impossible to complete if you don't know how to automate the task. That's where I come in - and boy was it satisfying to own his boss.

To maintain his anonymity, I will not show direct samples of the dataset or mention the use-case of it. Just be assured that it took a fuckton of sanitization work to bring it into a decent format that can easily be processed. The main idea here is that we parse the relevant data, render it onto a XeTeX template and save the rendered file on the disk to later render it into a PDF using xelatex.

First of all the template:



\textbf{Column 1} & \textbf{Column 2} & \textbf{Column 3} \\ \hline

\section{Table 2}
\textbf{Column 1} & \textbf{Column 2} & \textbf{Column 3} & \textbf{Column 4} & \textbf{Column 5} \\ \hline
1 & $t2_row1 \\ \hline
2 & $t2_row2 \\ \hline
3 & $t2_row3 \\ \hline
\caption{Table 2 caption}

\section{Table 3}
\begin{longtabu} to \textwidth {|X[1,c]|X[5,c]|X[1,c]|X[1,c]|X[1,c]|X[1,c]|} \hline
\multicolumn{1}{|c|}{\textbf{Column 1}} & \multicolumn{1}{|c|}{\textbf{Column 2}} & \multicolumn{1}{|c|}{\textbf{Column 3}} & \multicolumn{1}{|c|}{\textbf{Column 4}} & \multicolumn{1}{|c|}{\textbf{Column 5}} & \multicolumn{1}{|c|}{\textbf{Column 6}} \\ \hline

{{\bfseries \tablename\ \thetable{} -- continued}} \\
\multicolumn{1}{|c|}{\textbf{Column 1}} & \multicolumn{1}{|c|}{\textbf{Column 2}} & \multicolumn{1}{|c|}{\textbf{Column 3}} & \multicolumn{1}{|c|}{\textbf{Column 4}} & \multicolumn{1}{|c|}{\textbf{Column 5}} & \multicolumn{1}{|c|}{\textbf{Column 6}} \\ \hline

\hline \multicolumn{6}{|r|}{{Continue on the next page}} \\ \hline



\caption{Table 3 caption}

So we will render our data on a landscape A4 page in three tables. With the help of the longtabu package we can break tables on page break and continue them on the next page. You will notice some uncommon variables such as $table1 or $t2_row1. These are placeholders for Python's string.Template object, which allows you to do simple substitutions in strings. Example:

In [1]: from string import Template

In [2]: t = Template("This is a $tmpl_type template")

In [3]: t.substitute(tmpl_type="sample")
Out[3]: 'This is a sample template'

With our substitution variables set, we just have to parse the dataset, sanitize all format fuckups, and bring it into the XeTeX table line format. For that we can easily provide line templates for each of the three tables

T1_LINE = "{col_1} & {col2} & {col_3} \\\\ \\hline"
T2_LINE = "{col_1} & {col_2} & {col_3} & {col_4}"
T3_LINE = "{col_1} & {col_2} & {col_3} & {col_4} & {col_5} & {col_6} \\\\ \\hline"

Don't forget to escape the backslashes. Ugly, I know, but XeTeX needs double backslashes for manual line breaks. Now after sanitization, we replace some special table characters, and simply generate our table rows like this:

t3_rows = []
for cat, cat_data in
    text = content[cat.upper()].replace('&', '\\&')

All that's left now is to do the substitution on the template file and save it to the output directory:

with open(output_filename, 'w') as outfile:
    output = p_template.substitute(

In my case this resulted in 146 .tex files. In the output directory I just had to call xelatex *.tex, wait 5 minutes, and done! Due to the dataset's size, this resulted in roughly 500 pages or output. Impossible to do by hand. Said friend went to the office, printed all of the tables (using recycling paper, of course!) and put the stack on the professor's table. Owned. And boy, do those tables look pretty!

A little extra: The shell command I used to determine how many PDF pages were generated in the output directory:

for file in results/*.pdf; do pdfinfo $file | grep Pages | awk '{print $2}'; done | awk '{s+=$1} END {print s}'