簡體   English   中英

嘗試使用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一起執行。

當我按Enter鍵時,它似乎可以執行,沒有錯誤代碼。 但是目錄中沒有創建.tex文件。

我應該對Python進行哪些更改以使其運行,我知道我已經接近了...

好吧,我看到的第一個問題是您的.csv文件中只有一行,但是您使用了next()函數來“跳過標題”。 您提供的.csv沒有標題,因此您將跳過僅有的數據。

然后,當您到達for row in my_db :for row in my_db : ,就沒有要迭代的行,因此該代碼實際上從未進入任何write語句。

嘗試刪除代碼中的next()或修改.csv以包括標頭,然后使用新輸出發布更新。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM