I want to automatically create business card based on a database containing my employees' data. (i.e. to generate one .pdf
file per person, filling in a pre-filled document with the information of the database).
I have a database (a .csv
file) that looks like this:
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}
My .tex
file that defines the look of the PDF is
\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}
I want the LaTeX file to read the second row of the database, replace the #Fields
by their values, generate a PDF and then start again with a new line – as long as necessary.
I looked at the datatool
package, I am able to load the database (\DTLloaddb[autokeys]{DB}{my_db.csv}
). But I'm going blank regarding two points:
- How to read only the n-th element of the m-th row?
- How to generate a PDF and repeat with
m:=m+1
?
Thanks for any help (fixing these issues or indicating packages I should look at)!
Best Answer
Following Jim's suggestion, I wrote down an Python-code that does what I wanted to do.
Even if it is more Python oriented, I think it might be interesting to post the answer here. (Since we're TeXer here, and maybe not Python-ers, there are 'for dummies' explanations - sorry for length.)
Needed files
You need following files in your working directory :
routine.py
containing the code herein below,A csv-database named
database.csv
, with following structure:(Note that there is a header - the python routine will then skip the first line)
EmployeeID.extention
(E.g. in this example, you need three pictures :001.jpg
,002.png
,007.jpg
- note that the format might not be the same over the pictures)The Python code
Considering Dirk's suggestion, you have two options : either you generate one PDF-file per business card, or you generate one PDF-file with all the business card. You'll find both versions of the Python-routine herein below.
Please look at embedded comments for explanations.
To create one PDF-file per business card
The global operating is following :
.csv
database,To create only one PDF with all the business cards
The global operating is here slightly different :
.tex
file,.csv
database,.tex
file,.tex
file,.tex
file.How to process?
python routine.py
That's all !