尝试使用 python 自动将 csv 写入乳胶模板

Trying to automate writing csv to latex template with python

这是我的 CSV 文件:

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

这是我的 Python 代码:

#! /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()

我希望能够执行 Python 脚本,将其读入 CSV 文件,并将值放入 latexcontent 部分,然后使用 pdflatex.

当我按下回车键时,它似乎执行正常,没有错误代码。但是目录中没有创建 .tex 文件。

我应该对 Python 进行哪些更改才能使其正常工作,我知道我很接近...

嗯,我看到的第一个问题是您的 .csv 文件中只有一行,但是您使用 next() 函数来 "skip the header"。您提供的 .csv 中没有 header,因此您跳过了您仅有的数据。

然后当您到达 for row in my_db : 行时,没有任何行可以迭代,因此代码实际上从未进入任何写入语句。

尝试删除代码中的 next() 或修改 .csv 以包含 header,然后 post 使用新输出进行更新。