# creating multiple generic letters from pulling info from an excel/csv file

I wanted to create 100 invitation letters for a group of 100 people whose name and phone number I have in an excel sheet. I have written one generic letter and wanted to the put in the name and phone number from the list in the excel sheet and save the 100 pdf files in a folder. What is the best way to do it please using latex/knitr/markdown? An example of the letter looks like this..

Hello XXXX,

Please show up at my place on Mon 4th July,

Best, Tom

XXXX = names that shall be called from an external excel list

• You may want to ask this question at super user. – zyy Nov 8 '18 at 1:34
• Look at csvsimple package. – CarLaTeX Nov 8 '18 at 9:49
• Csv data plus datatool package. Make the letter into a macro that takes the non static data as arguments, feed the data to it via a datatool loop. This generates one large PDF with all the letters. If you need separate files, there are several tools to split the pdf up – daleif Nov 8 '18 at 18:50

Let's assume you have the following data in a file named nameslist.csv:

NAME
Akilah
Amie
Andree
Angel
Angelika
Annabell
Aracely
Bailey
Bambi
Bridgett
Cameron
Candra
Carole
Carson
Cassi
Cecila
Cecilia
Ceola
Chau
Cherlyn
Clorinda
Cristal
Danuta
Darin
Darnell
Debra
Denice
Denise
Denver
Diedra
Donovan
Dorothea
Edgar
Eliza
Elliott
Elvira
Emely
Emerald
Ethelyn
Florance
Franklyn
Gertha
Gisele
Guillermina
Hanh
Harvey
Herta
Hye
Jamie
Jarod
Joellen
Julianna
Julie
Kai
Karena
Kay
Kenya
Kimbery
Kyung
Lessie
Loree
Lucina
Ma
Macy
Magaret
Marcelina
Maricruz
Marisol
Marivel
Marquerite
Matilda
Mika
Natalie
Neoma
Olen
Oscar
Pei
Pok
Raven
Raymon
Sarah
Serina
Shanda
Shani
Shanon
Shante
Shirly
Susy
Tashina
Teodora
Tommy
Tracy
Valeria
Verna
Vernia
Vinnie
Violeta
Zachariah


It includes 100 random names. Also, you have the following LaTeX template called letter_input.tex for a letter that you wrote that you want to send to each of the above names:

\documentclass{article}

\setlength{\parindent}{0pt} % Remove paragraph indent

\begin{document}

Hello \firstname,

\medskip

Please show up at my place on Monday, July 4th.

\medskip

Best, Tom.

\end{document}


Note how we've coded the addressee's name as \inputname.

You can not process these as a single file and get multiple outputs under LaTeX. The default is one-to-one. The same would be the case for Markdown or Knitr. But, you can process the file multiple times and rename the output using an R script like the following:

library(tidyverse)

file = 'nameslist.csv'
)

for (curname in namelist\$NAME) {

system(
paste0(
'pdflatex \\def\\firstname{',
curname,
'} \\input{letter_input}'
),
show.output.on.console = FALSE
)

curname <- file.rename(
from = 'input_letter.pdf',
to = paste0(
'letter_to_',
curname,
'.pdf'
)
)

}


The above code reads in nameslist.csv to get a data frame (tibble) with all the names. Then, it cycles through each <name> and

1. compiles using pdflatex \def\inputname{<name>} \input{letter_input}.

This sets the \inputname to the appropriate one in the list of names using a \def and then \inputs your letter_input.tex.

2. renames (moves) the output PDF letter_input.pdf to a new name called letter_to_<name>.pdf.

Here's a sample of the output called letter_to_Tracy.pdf:

If you want to insert more content into the letter, add more columns, define a new variable and add it to the list of \defs before \input.