Generating pdf-latex with python script
Question: I'm a college guy, and in my college, to present any kind of homework, it has
to have a standard coverpage (with the college logo, course name, professor's
name, my name and bla bla bla).

So, I have a .tex document, which generate my standard coverpages pdfs. It
goes something like:

    
    
    ...
    \begin{document}
    %% College logo
    \vspace{5cm}
    \begin{center}
    \textbf{\huge "School and Program Name" \\}
    \vspace{1cm}
    \textbf{\Large "Homework Title" \\}
    \vspace{1cm}
    \textbf{\Large "Course Name" \\}
    \end{center}
    \vspace{2.5cm}
    \begin{flushright}
    {\large "My name" }
    \end{flushright}
    ...
    

So, I was wondering if there's a way to make a Python script that asks me for
the title of my homework, the course name and the rest of the strings and use
them to generate the coverpage. After that, it should compile the .tex and
generate the pdf with the information given.

Any opinions, advice, snippet, library, is accepted.


Answer: You can start by defining the template tex file as a string:

    
    
    content = r'''\documentclass{article}
    \begin{document}
    ...
    \textbf{\huge %(school)s \\}
    \vspace{1cm}
    \textbf{\Large %(title)s \\}
    ...
    \end{document}
    '''
    

Next, use `argparse` to accept values for the course, title, name and school:

    
    
    parser = argparse.ArgumentParser()
    parser.add_argument('-c', '--course')
    parser.add_argument('-t', '--title')
    parser.add_argument('-n', '--name',) 
    parser.add_argument('-s', '--school', default='My U')
    

A bit of string formatting is all it takes to stick the args into `content`:

    
    
    args = parser.parse_args()
    content%args.__dict__
    

After writing the content out to a file, cover.tex,

    
    
    with open('cover.tex','w') as f:
        f.write(content%args.__dict__)
    

you could use `subprocess` to call `pdflatex cover.tex`.

    
    
    proc = subprocess.Popen(['pdflatex', 'cover.tex'])
    proc.communicate()
    

You could add an `lpr` command here too to add printing to the workflow.

Remove unneeded files:

    
    
    os.unlink('cover.tex')
    os.unlink('cover.log')
    

The script could then be called like this:

    
    
    make_cover.py -c "Hardest Class Ever" -t "Theoretical Theory" -n Me
    

* * *

Putting it all together,

    
    
    import argparse
    import os
    import subprocess
    
    content = r'''\documentclass{article}
    \begin{document}
    ... P \& B 
    \textbf{\huge %(school)s \\}
    \vspace{1cm}
    \textbf{\Large %(title)s \\}
    ...
    \end{document}
    '''
    
    parser = argparse.ArgumentParser()
    parser.add_argument('-c', '--course')
    parser.add_argument('-t', '--title')
    parser.add_argument('-n', '--name',) 
    parser.add_argument('-s', '--school', default='My U')
    
    args = parser.parse_args()
    
    with open('cover.tex','w') as f:
        f.write(content%args.__dict__)
    
    cmd = ['pdflatex', '-interaction', 'nonstopmode', 'cover.tex']
    proc = subprocess.Popen(cmd)
    proc.communicate()
    
    retcode = proc.returncode
    if not retcode == 0:
        os.unlink('cover.pdf')
        raise ValueError('Error {} executing command: {}'.format(retcode, ' '.join(cmd))) 
    
    os.unlink('cover.tex')
    os.unlink('cover.log')
    

