Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Markdown to docx, including complex template

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.

like image 708
Synesso Avatar asked Jan 10 '13 02:01

Synesso


People also ask

How do I convert a markdown to a Word document?

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.

What is a Pandoc template?

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}

Does Pandoc need LaTeX?

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.


2 Answers

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.

like image 165
François Leblanc Avatar answered Sep 24 '22 18:09

François Leblanc


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 
like image 26
Andrew Avatar answered Sep 23 '22 18:09

Andrew