Reading CSV files with LaTeX

Reading CSV files is pretty easy in LaTeX, thanks to Nicola Talbot’s datatool package.

When you have an Excel file like the following Schueler.xlsx, simply save it as CSV (Comma-Separated Value).

excli

If you open the CSV file „Schueler.csv“ in a text editor you get the following:

excli2

We start by loading the datatool package. Since the German Excel uses semicolons as column separator, we are required to manually set the delimiter. In the next line we tell LaTeX to use our Schueler.csv file and to reference it as „scores“. In the DTLforeach commands the magic happens. The command has three parameters:

  • the file reference
  • the assignment of the file headers to LaTeX commands
  • the loop where the output for each row is defined
\documentclass[12pt,ngerman]{scrartcl}
\usepackage[utf8]{inputenc} 
\usepackage[T1]{fontenc}
\usepackage{babel}

\usepackage{datatool}

\DTLsetseparator{;}
\DTLloaddb{scores}{Schueler.csv}

\begin{document}

\begin{table}
\caption{Punktübersicht}
\centering
\begin{tabular}{llr}
\bfseries Name &
\bfseries Aufgabe A &
\bfseries Aufgabe B %
\DTLforeach{scores}{%
\name=Schueler,\aufgabea=AufgabeA,\aufgabeb=AufgabeB}{%
\\
\name & \aufgabea & \aufgabeb }
\end{tabular}
\end{table}

\DTLforeach{scores}{%
\name=Schueler,\aufgabea=AufgabeA,\aufgabeb=AufgabeB}{%

\name\ hat in Aufgabe A  \aufgabea~Punkte erzielt, in Aufgabe B \aufgabeb~Punkte.}


\end{document}

excli3

Uwe

Uwe Ziegenhagen likes LaTeX and Python, sometimes even combined. Do you like my content and would like to thank me for it? Consider making a small donation to my local fablab, the Dingfabrik Köln. Details on how to donate can be found here Spenden für die Dingfabrik.

More Posts - Website