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.
Save a Markdown File as a Word DocumentOpen Microsoft Word and open any Markdown file. Use the Save as… command and choose Word Document from the Save as type field.
Pandoc converts Markdown to LaTeX through a template. The template is a LaTeX file containing Pandoc variables, and Pandoc will replace these variables with their values. Below is a simple template that only contains a single variable $body$ : \documentclass{article} \begin{document} $body$ \end{document}
By default, pandoc will use LaTeX to create the PDF, which requires that a LaTeX engine be installed (see --pdf-engine below). Alternatively, pandoc can use ConTeXt, roff ms, or HTML as an intermediate format.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With