#!/usr/bin/env python
#coding=utf-8

import numpy as np
import matplotlib.pyplot as plt
import jinja2
import tempfile
import subprocess
import shutil
import os
import pandas as pd

participants = pd.DataFrame.from_csv('participants.csv', index_col=None)
with file('certificate.tex') as fid:
    t = jinja2.Template(fid.read().decode('utf-8'))

tmpdir = tempfile.mkdtemp()
shutil.copy('software-carpentry-banner.png', tmpdir)

for pid in range(len(participants)):

    participant = participants.ix[pid]
    name = participant['firstname'] + ' ' + participant['lastname']
    latex_src = t.render(name=name.decode('utf-8'))


    latex_fname = os.path.join(tmpdir, 'participant{}.tex'.format(pid))

    with file(latex_fname, 'w') as latex_file:
        latex_file.write(latex_src.encode('utf-8'))

    fname, ext = os.path.splitext(latex_fname)

    subprocess.call(['xelatex', latex_fname], cwd=tmpdir)
    import glob

    all_pdfs = glob.glob(os.path.join(tmpdir, '*.pdf'))
subprocess.call(['pdfjoin', '-o', 'certificates.pdf'] + all_pdfs)

@@ -172,16 +172,269 @@
 as as pd
+%0Aimport argparse%0A%0Aparser = argparse.ArgumentParser()%0Aparser.add_argument('participants', help='CSV file with participants')%0Aparser.add_argument('--template', '-t', default='certificate.tex', help='LaTeX certificate template')%0A%0Aargs = parser.parse_args()
 %0A%0Apartic
@@ -464,17 +464,21 @@
 rom_csv(
-'
+args.
 particip
@@ -485,13 +485,8 @@
 ants
-.csv'
 , in
@@ -509,33 +509,29 @@
 th file(
-'certificate.tex'
+args.template
 ) as fid
@@ -1217,16 +1217,76 @@
 *.pdf'))
+%0A%0Apdf_filename = os.path.splitext(args.template)%5B0%5D + '.pdf'
 %0Asubproc
@@ -1316,26 +1316,20 @@
 o', 
-'certificates.pdf'
+pdf_filename
 %5D + 

add arguments to certificate generation script