Markdown to docx, including complex template

Update

Use the piped version suggested by user Christian Long:

pandoc -t latex mydoc.md | pandoc -f latex --data-dir=docs/rendering/ -o mydoc.docx

I know this is late in coming, but I'll be assuming people are still searching for solutions to this three years after the original question -- I know I was.

My solution was to use LaTeX as an intermediary between markdown and docx (actually, I was converting from org-mode, but same difference). So in your case, I believe a one-liner solution would be:

pandoc -f markdown -t latex -o mydoc.tex mydoc.md && \
pandoc -f latex -t docx --data-dir=docs/rendering/ -o mydoc.docx mydoc.tex

Which might get you closer to your goal. Of course, Pandoc has about hundred arguments it can handle, and there are probably ways to make this prettier. It has also gotten quite a few updates since you first posted your question.


Ideally you could use a custom docx template, but pandoc doesn't support that yet. A reference.docx file only allows custom styles to be embedded in newly created docx files.

Fortunately you can approximate this using odt instead of docx. You can fairly easily modify the default OpenDocument template to include your custom logos, preamble, and other stuff. Use the custom template in conjunction with a reference.odt file to get all the styles and custom content.

Once you have the file in odt format, you can use any number of command line tools to convert from odt to docx. For example, on Linux you can run

libreoffice --invisible --convert-to docx test.odt

Or on OS X:

/Applications/LibreOffice.app/Contents/MacOS/soffice.bin --invisible --convert-to docx test.odt