Skip to content

Instantly share code, notes, and snippets.

@vuklip
Last active June 23, 2017 12:18
Show Gist options
  • Save vuklip/ef728632759858607f6a2751844aa245 to your computer and use it in GitHub Desktop.
Save vuklip/ef728632759858607f6a2751844aa245 to your computer and use it in GitHub Desktop.
md2docx

$ pandoc --reference-docx=style.docx test.md -o test.docx

A little bit of man:

--reference-docx=FILE Use the specified file as a style reference in producing a docx file. For best results, the reference docx should be a modified version of a docx file produced using pandoc. The contents of the reference docx are ignored, but its stylesheets and document properties (including margins, page size, header, and footer) are used in the new docx. If no reference docx is specified on the command line, pandoc will look for a file reference.docx in the user data directory (see --data-dir). If this is not found either, sensible defaults will be used.

To produce a custom reference.docx, first get a copy of the default reference.docx: pandoc --print-default-data-file reference.docx > custom-reference.docx. Then open custom-reference.docx in Word, modify the styles as you wish, and save the file. For best results, do not make changes to this file other than modifying the styles used by pandoc: [paragraph] Normal, Body Text, First Paragraph, Compact, Title, Subtitle, Author, Date, Abstract, Bibliography, Heading 1, Heading 2, Heading 3, Heading 4, Heading 5, Heading 6, Block Text, Footnote Text, Definition Term, Definition, Caption, Table Caption, Image Caption, Figure, Figure With Caption, TOC Heading; [character] Default Paragraph Font, Body Text Char, Verbatim Char, Footnote Reference, Hyperlink; [table] Normal Table.

#!/bin/bash
#
# Generate a Word version of markdown file with style.
#
# You should have style.docx file with you styles.
#
which pandoc > /dev/null
rc=$?
if [[ $rc != 0 ]]; then
echo "FATAL missing pandoc. You can install with 'brew install pandoc' or similar"
exit 9
fi
if [ -z "$1" ]; then
echo "Usage:"
echo ""
echo " sh md2docx.sh filename"
exit 13
fi
if [ ! -f "$1" ]; then
echo "FATAL missing file '$1.docx'"
exit 11
fi
re='^([^.]+)'
if [[ $1 =~ $re ]]; then
name=${BASH_REMATCH[0]}
fi
pandoc --reference-docx=.style.docx "$1" -o "$name".docx
if [ ! -f "$name".docx ]; then
echo "FATAL somthing bad was happend"
exit 11
else
echo " OK :)"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment