Markdown to docx, including complex template

Synesso picture Synesso · Jan 10, 2013 · Viewed 36.2k times · Source

I have automated my build to convert Markdown files to DOCX files using Pandoc. I have even used a reference document for the final document's styling. The command I use is:

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

The reference.docx is picked up by Pandoc from docs/rendering and Pandoc renders mydoc.docx with the same styles as the reference doc.

However, reference.docx contains more than just styles. It contains coporate logos, preamble, etc.

How can I automate the merging of the Markdown content with both the styles and content of reference.docx. My solution needs to work on Linux.

Answer

François Leblanc picture François Leblanc · Jan 3, 2017

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.