SaltedPork
SaltedPork

Reputation: 377

Trying to automate writing csv to latex template with python

Here is my CSV file:

Simon,/home/user/Desktop/simon.jpeg

Here is my Python code:

#! /usr/bin/python3
import csv
import subprocess
LatexContent = '''\\documentclass[12pt, twocolumn, letterpaper]{report}
                        \\usepackage[utf8]{inputenc}
                        \\usepackage{graphicx}
                        \\renewcommand{\familydefault}{\\sfdefault} 
                            \\begin{document}
                            Run Satus: \\textsc{%(sampleid)s}
                            %(sampleid)s
                            \\includegraphics[width=20cm]{%(coveragegraph)s}
                                \\end{document}'''


###== Look at the database ==##
# open the database into python
my_db_file = open('automate.testing.csv', 'r') 

# read the database
my_db = csv.reader(my_db_file, delimiter=',',skipinitialspace=True)

###== TeX files processing and generating ==###
#skip the header of the database
next(my_db)

#then for each row of the database
for row in my_db :
    ## Assign the items of the row to the variables that will fill up the 
    ##    blanks of the LaTeX code
    sampleid = str(row[0])            #caution, first item of a row = index '0'
    coveragegraph = str(row[1])


        #define the TeX file name
    TexFileName = sampleid + '.tex'

    ## create a new LaTeX file with the blanks filled
        #create a new file
    TexFile = open(TexFileName,'w')

        #fill the blanks with the previously read informations
    TexFile.write(LatexContent %{"sampleid" : sampleid, "coveragegraph" : coveragegraph})

        #close the file
    TexFile.close()

    ## compile the file you've just created with LaTeX        
    subprocess.Popen(['pdflatex',TexFileName],shell=False)      

    ##repeat for each row

#close the database file
my_db_file.close()

I want to be able to execute the Python script, have it read in the CSV file, and put the values into the latexcontent section which will then be executed with pdflatex.

When I hit enter it appears to execute fine, no error codes. But there is no .tex file created in the directory.

What changes should I make to the Python to make it work, I know I am close...

Upvotes: 0

Views: 1132

Answers (1)

Das_Geek
Das_Geek

Reputation: 2845

Well the first issue I see is that you only have one row in your .csv file, but you use the next() function to "skip the header". There is no header in the .csv you gave, so you're skipping over the only data you have.

Then when you get to the for row in my_db : line, there aren't any rows to iterate over, so the code never actually makes it to any write statements.

Try deleting the next() in your code or modifying your .csv to include headers, then post an update with the new output.

Upvotes: 2

Related Questions