How to Convert Markdown Files to Word DOCX in Linux Terminal Using Pandoc

Markdown has become the ubiquitous standard for software documentation, technical blogging, and developer notes thanks to its distraction-free syntax and version-control friendliness. However, corporate environments, clients, and publishing editors frequently demand documents formatted in proprietary Microsoft Word (.docx) format. In Linux environments, you do not need to install heavy office suites or copy-paste text manually into web editors. The universal document converter Pandoc provides a robust command-line mechanism to transform complex Markdown files into professionally formatted Word documents, complete with embedded tables, syntax-highlighted code blocks, and custom corporate styling templates.

Why Use Pandoc for Markdown to DOCX Conversion?

Pandoc is widely recognized as the Swiss Army knife of document processing, offering unmatched flexibility:

  • Semantic Preservation: Headers (#, ##), bulleted lists, footnotes, hyperlinks, blockquotes, and bold text are translated into native Word heading styles and structural elements.
  • Table and Math Support: Markdown pipe tables are rendered as formatted Word tables, and LaTeX math equations are converted into native Office MathML equation objects.
  • Custom Reference Styling: Pandoc allows you to supply a custom reference Word document (--reference-doc), automatically applying your corporate branding, typography, margins, and colour schemes to the output file.

Step 1: Installing Pandoc on Linux

Pandoc is packaged across all major Linux distributions:

  • Ubuntu, Debian, and Linux Mint:
    sudo apt update && sudo apt install pandoc
  • Fedora, RHEL, and CentOS:
    sudo dnf install pandoc
  • Arch Linux and Manjaro:
    sudo pacman -S pandoc

To verify that Pandoc is installed and check its version, run pandoc --version in your terminal.

Step 2: Basic Markdown to DOCX Conversion

The standard syntax for converting a single Markdown document into a Word file is straightforward:

pandoc report.md -o report.docx

In this command, report.md represents your source Markdown text, and the -o flag specifies the output file name and target format (which Pandoc detects from the .docx extension).

Step 3: Preserving Code Syntax Highlighting

If your documentation contains technical code snippets, Pandoc can automatically colorize and highlight programming syntax using native Word styles. You can select from various built-in themes such as tango, pygments, kate, or monochrome:

pandoc guide.md -o guide.docx --highlight-style=tango

To inspect all available syntax highlighting themes supported by your Pandoc installation, run pandoc --list-highlight-styles.

Step 4: Applying Custom Brand Styles Using a Reference Document

By default, Pandoc applies generic Word styling (such as standard Calibri font and 1-inch margins). To enforce your own corporate brand guidelines—including custom typography, header colours, line spacing, and page borders—you can supply a reference Word template.

1. Generate a Default Template Document

Instruct Pandoc to export its default internal Word reference file:

pandoc -o custom-template.docx --print-default-data-file reference.docx

2. Customise the Reference Template in Word or LibreOffice

Open custom-template.docx in Microsoft Word or LibreOffice Writer. Modify the standard styles directly:

  • Edit the Normal style to change your body text font, line spacing, and paragraph margins.
  • Edit Heading 1, Heading 2, and Heading 3 styles to apply brand colours, weights, and sizes.
  • Customise the Table style to define header row shading and border styling.
  • Save and close the file.

3. Execute the Conversion with the Reference Document Flag

Pass your modified template to Pandoc during conversion:

pandoc proposal.md -o proposal.docx --reference-doc=custom-template.docx

Pandoc will read the styles from custom-template.docx and inject your Markdown content into that exact design framework without altering your original template.

Step 5: Batch Converting Multiple Markdown Files

When working across multi-chapter documentation or repositories of notes, you can batch convert files using a simple Bash loop:

for f in *.md; do
    pandoc "$f" -o "${f%.md}.docx" --reference-doc=custom-template.docx
done

Alternatively, if you want to concatenate multiple Markdown files into a single unified Word document, pass all filenames in order:

pandoc chapter1.md chapter2.md chapter3.md -o complete-manual.docx --toc

The --toc flag automatically generates an interactive Table of Contents at the start of your Word document based on your Markdown header hierarchy.

Related Terminal Audio and System Automation Guides

To further extend your Linux terminal productivity, check out these related command-line tutorials:

Integrating Pandoc into your Linux workflow bridges the gap between lightweight developer documentation and corporate document delivery, enabling automated, flawless document generation in seconds.

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.