Recently the Pandoc upgrades to Pandoc 2. After struggling with reference for docx in Pandoc, I finally make the workflow elegant. Below is a Makefile that can generate docx from md, where the old docx is used as template. Thus the changed style in the docx is reserved, and you can change the md file to get proper docx file.

The workflow then makes clear:

  1. Put the Makefile somewhere, e.g. in ~/.

  2. Write something as a markdown file, say /tmp/answer.md.

  3. In the /tmp/ run make -f ~/Makefile answer.docx will generate the .docx file.

  4. Simply run make -f ~/Makefile will convert all the .md files to .docx.


我写了一个Makefile,方便进行.md → .docx的转换。这个Makefile的优势是可以保留对docx格式的修改,原理是使用老的docx作为模板,这样新文档继承老文档的格式,如果你的修改是对样式而非内容的(修改方法见以前的分享),格式信息会保留。

一个典型的流程是

  1. 把上面的Makefile保存在本地,比如在~/

  2. 把.md保存在任意路径,比如/tmp/answer.md

  3. answer.md所在目录执行make -f ~/Makefile answer.docx,生成相应的.docx文件。

  4. 如果执行make -f ~/Makefile则当前目录内所有的.md文件都转为.docx。