I remember from my MS Office days the Mail merge feature: I had a table in an Excel sheet, a template in Word and Mail Merge created a Word document from each row in the Excel sheet.

I now have completely changed over to plain text files: CSV for tables and Markdown for text and I would like to process the CSV and template into a stack of Markdown files (which I then convert via Pandoc to HTML/PDF).

A) Mail Merge"-type of tool

Does such a "Mail Merge"-type of tool exists for this workflow? I'd prefer a command line tool, because everything else in the tool chain is cli-based.

B) Tool for both directions

The coolest feature would be if the converting were possible two ways:

CSV+Markdown template --> Bunch of Markdown documents

and vice versa

Bunch of Markdown documents --> CSV+Markdown template

  • As a start: I came across Fletcher Penney's tdp which can be used with mustache templates.This is not a coherent solution but at starting point...
    – halloleo
    Dec 19, 2019 at 5:45

3 Answers 3


I was looking for something similar, and since I could not find it, I wrote a small python script following the principles outlined by rd_nielsen.


You can call it like this:

python3 pandoc-merge.py --csv addresses.csv -o merged_letter.pdf letter_template.md 

In your template, you can use {{ variable }} and other jinja2 constructs to insert content from the fields of your CSV file.


If you were into R and modern R scripting:

You could use R's knitr package, write a little .Rmd file and "knit" the document to markdown+html (+ optionally .docx and pdf) in one fell swoop with the Rscript command line tool.

knitr has a "keep_md" option in the YAML front matter.

Together with the blogdown R package and static site generator Hugo you can build a nice templating solution. (You don't have to build a web site).

The "reverse way" you've mentioned (from page to template+csv) is probably not possible.


Consider using a templating language such as Jinja (https://palletsprojects.com/p/jinja/) or Airspeed (https://github.com/purcell/airspeed) as your Markdown template. Then you simply need a little bit of code (both the links above are to Python libraries, so Python would be a sensible language to use) to read the CSV file and run it through the template to generate your final Markdown.

These two steps could be carried out with the IMPORT and EXPORT metacommands of execsql.py. Disclaimer: I wrote it. It can even create a SQLite database on the fly to import the data into. It's a command-line tool. It won't meet your desire to reverse the mail-merge process, however.

  • Note that python comes with a built in library for reading csv files and has pip install-able libraries for producing markdown from template languages such as Jinja and for converting markdown to pdf & html (or even docx without having word installed). you could even convert to pdf &/or html and email/upload without saving to disk. It is also possible to process a batch of markdown files to extract the common text into a template and the differences into a .csv file but that will take much more skill to implement. Dec 20, 2019 at 5:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.