我想根据包含员工数据的数据库自动创建名片。(即生成每人一份.pdf
文件,填写一份预先填好的数据库信息的文件)。
.csv
我有一个如下所示的数据库(一个文件):
Employee ID, Last Name, First name, Telephone number, Additional informations
001, Dylan, Bob, 012345, Some stuff
007, Doe, John, 01452,
002, Doe, Jane, , A lot of \emph{informations} with \LaTeX commands that are very long \newline and maybe should be displayed on several lines etc. \begin{itemize} \item fus \item ro \item dah! \end{itemize}
我.tex
定义 PDF 外观的文件是
\documentclass{scrartcl}
\begin{document}
\includegraphics[width=1cm,height=3cm]{#EmplyeeID.jpg}
{\sffamily #FirstName \textsc{#LastName}}
\newline
\section{Phone}
{\tiny #telephone number}
\newpage
#AdditionalInformations
\end{document}
我希望 LaTeX 文件读取数据库的第二行,#Fields
用它们的值替换,生成 PDF,然后以新行重新开始 - 只要有必要。
我查看了datatool
包,我可以加载数据库(\DTLloaddb[autokeys]{DB}{my_db.csv}
)。但我对两点一无所知:
- 如何仅读取第 m 行的第 n 个元素?
- 如何生成 PDF 并重复
m:=m+1
?
感谢您的任何帮助(解决这些问题或指出我应该查看的软件包)!
答案1
下列的吉姆的建议,我写下了一个完成我想要做的事情的 Python 代码。
即使它更面向 Python,我认为在这里发布答案可能会很有趣。(因为我们是 TeXer,可能不是 Python-ers,所以有“傻瓜式”的解释 - 抱歉篇幅太长。)
所需文件
您的工作目录中需要以下文件:
- 一个名为 python 文件,
routine.py
包含以下代码, 一个名为的 csv 数据库
database.csv
,具有以下结构:Employee ID, Last Name, Fist name, Telephone number, Additional informations 001, Dylan, Bob, 012345, Some stuff 007, Doe, John, 01452, 002, Doe, Jane, , "A lot of \emph{informations} with \LaTeX commands, and commas as well. \newline It can be displayed on several lines. \begin{itemize} \item fus \item ro \item dah! \end{itemize}"
(请注意,有一个标题 - 然后 Python 例程将跳过第一行)
- 每位员工一张照片。该照片应按照以下模式命名:(
EmployeeID.extention
例如,在此示例中,您需要三张照片:001.jpg
,,002.png
-007.jpg
请注意,每张照片的格式可能不一样)
Python 代码
考虑到短剑的建议是,您有两个选择:要么为每张名片生成一个 PDF 文件,要么为所有名片生成一个 PDF 文件。您将在下面找到 Python 程序的两个版本。
请查看嵌入的评论以获取解释。
每张名片创建一个 PDF 文件
全球运营情况如下:
- 该例程读取数据库中的一行
.csv
, - 生成定制的 LaTeX 代码,
- 并编译它。
- 然后,它对每一行重复该过程(步骤 1 到 3)。
###== imported packages ==###
import csv
import subprocess # cf "http://stackoverflow.com/questions/19683123/
# compile-latex-from-python" for original example
###== Definition of the LaTeX template (with "blanks") ==###
# caution : you need to escape backslashs with backslashs
# blanks are filed with %(Name)s
# Name are here two letters
# 's' means the variable is a string
LatexContent = '''\\documentclass{scrartcl}
\\usepackage{graphicx}
\\begin{document}
\\includegraphics[width=1cm,height=3cm]{%(Id)s}
{\\sffamily %(Fn)s \\textsc{%(Ln)s}}
\\newline
\\section{Phone}
{\\tiny Phone number: %(Ph)s}
\\newpage
%(Ot)s
\\end{document}'''
###== Look at the database ==##
# open the database into python
my_db_file = open("database.csv","rb")
# read the database
my_db = csv.reader(my_db_file, delimiter=',',skipinitialspace=True)
###== TeX files processing and generating ==###
#skip the header of the database
my_db.next()
#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
ID = str(row[0]) #caution, first item of a row = index '0'
LastName = str(row[1])
FirstName = str(row[2])
Phone = str(row[3])
Other = str(row[4])
#define the TeX file name
TexFileName = ID + '.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 %{"Id" : ID, "Fn" : FirstName,
"Ln" : LastName, "Ph" : Phone, "Ot" : Other })
#close the file
TexFile.close()
## compile the file you've just created with LaTeX
subprocess.Popen(['pdflatex',TexFileName],shell=False)
##repeate for each row
#close the database file
my_db_file.close()
要仅创建一个包含所有名片的 PDF
这里的全局操作略有不同:
- 该例程生成文件的头部
.tex
, - 读取数据库的一行
.csv
, - 生成自定义的 TeX 代码,并将其附加到现有
.tex
文件中, - 对每一行重复步骤 2 和 3
- 将 TeX 代码的底部附加到现有
.tex
文件, - 最后编译
.tex
文件。
- 该例程生成文件的头部
###== imported packages ==###
import csv
import subprocess # cf "http://stackoverflow.com/questions/19683123/
# compile-latex-from-python" for original example
###== Definition of the LaTeX template (with "blanks") ==###
# caution : you need to escape backslashs with backslashs
# blanks are filed with %(Name)s
# Name are here two letters
# 's' means the variable is a string
LaTeXpreamble='''\\documentclass{scrartcl}
\\usepackage{graphicx}
\\begin{document}'''
LaTeXcodePerBusinessCard='''\\includegraphics[width=1cm,height=3cm]{%(Id)s}
{\\sffamily %(Fn)s \\textsc{%(Ln)s}}
\\newline
\\section{Phone}
{\\tiny Phone number: %(Ph)s}
\\newpage
%(Ot)s
\\null'''
LaTeXinBetween='''\\newpage'''
LaTeXcolophon='''\\end{document}'''
###== Look at the database ==##
# open the database into python
my_db_file = open("database.csv","rb")
# read the database
my_db = csv.reader(my_db_file, delimiter=',',skipinitialspace=True)
###== TeX files processing and generating ==###
#skip the header of the database
my_db.next()
#create a new textfile
#define its name
TexFileName = 'businessCards.tex'
#create the file
TexFile = open(TexFileName,'w')
#copy the preamble
TexFile.write(LaTeXpreamble)
#close the file and re-open it in 'append' mode
TexFile.close()
TexFile = open(TexFileName,'a')
#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
ID = str(row[0]) #caution, first item of a row = index '0'
LastName = str(row[1])
FirstName = str(row[2])
Phone = str(row[3])
Other = str(row[4])
## add a piece of code with the blanks filled
#fill the blanks with the previously read informations
LaTeXcodeToAdd = LaTeXcodePerBusinessCard %{"Id" : ID,
"Fn" : FirstName, "Ln" : LastName, "Ph" : Phone, "Ot" : Other }
#append this code to the .tex file
TexFile.write(LaTeXcodeToAdd)
#append the 'in-between' code to separate two business cards
TexFile.write(LaTeXinBetween)
##repeate for each row
#append the colophon to finish the .tex filed
TexFile.write(LaTeXcolophon)
#close the file
TexFile.close()
## compile the .tex file with pdfLaTeX
subprocess.Popen(['pdflatex',TexFileName],shell=False)
#close the database file
my_db_file.close()
如何处理?
- 打开终端
- 浏览到包含上述文件的目录
- 执行命令
python routine.py
就这样 !