编程 Pandoc 文档格式转换:从 Markdown 到 Word/PDF/EPUB 的安装与命令行用法

2026-09-04 21:20:26

最近在整理文档格式转换时用到了 Pandoc,跨 Markdown、Word、HTML、EPUB、LaTeX 等格式互转基本靠它一个工具就能解决。这里把下载、安装和基础用法记录一下。

Pandoc 是什么

Pandoc 由 John MacFarlane 开发,是一个通用文档转换工具,支持大量标记语言之间的格式转换,例如 Markdown、Microsoft Word、PowerPoint、Jupyter Notebook、HTML、PDF、LaTeX、Wiki、EPUB 等格式相互转换。官方称之为文档转换领域的“瑞士军刀”。项目开源免费,源码托管在 GitHub,使用 Haskell 实现。

支持格式范围如下,其中:

  • 表示可从该格式转换为其他格式
  • 表示可转换为该格式
  • ↔︎ 表示支持双向转换

轻量级标记格式:

  • ↔︎ Markdown(包括 CommonMark 和 GitHub-flavored Markdown)
  • ↔︎ reStructuredText
  • → AsciiDoc
  • ↔︎ Emacs Org-Mode
  • ↔︎ Emacs Muse
  • ↔︎ Textile
  • ← txt2tags

HTML 格式:

  • ↔︎ (X)HTML 4
  • ↔︎ HTML5

Ebooks:

  • ↔︎ EPUB 2 / EPUB 3
  • ↔︎ FictionBook2

文档格式:

  • → GNU TexInfo
  • ↔︎ Haddock markup

Roff 格式:

  • ↔︎ roff man
  • → roff ms

TeX 格式:

  • ↔︎ LaTeX
  • → ConTeXt

XML 格式:

  • ↔︎ DocBook 4 / DocBook 5
  • ↔︎ JATS
  • → TEI Simple

大纲格式:

  • ↔︎ OPML

数据格式:

  • ← CSV 表格

文字处理格式:

  • ↔︎ Microsoft Word docx
  • ↔︎ OpenOffice/LibreOffice ODT
  • → OpenDocument XML
  • → Microsoft PowerPoint

交互式笔记格式:

  • ↔︎ Jupyter notebook (ipynb)

页面布局格式:

  • → InDesign ICML

Wiki 标记语言格式:

  • ↔︎ MediaWiki 标记语
  • ↔︎ DokuWiki 标记语
  • ← TikiWiki 标记语
  • ← TWiki 标记语
  • ← Vimwiki 标记语
  • → XWiki 标记语
  • → ZimWiki 标记语
  • ↔︎ Jira wiki 标记语

幻灯片放映格式:

  • → LaTeX Beamer
  • → Slidy
  • → reveal.js
  • → Slideous
  • → S5
  • → DZSlides

自定义格式:

  • → 支持使用 Lua 编写自定义转换器

PDF:

  • → 通过 pdflatex、lualatex、xelatex、latexmk、tectonic、wkhtmltopdf、weasyprint、prince、context、pdfroff 等工具转为 PDF

下载与安装

Pandoc 提供 Haskell 代码库和命令行程序,支持 Windows、macOS、Linux、Chrome OS、BSD、Docker、GitHub Actions 以及源码编译。最简单的安装方式是直接下载编译好的安装文件。

Windows

Windows 系统提供编译后的 MSI 安装包,可运行安装;也可下载免安装的 zip 压缩包解压使用。或使用 Chocolatey 安装:

choco install pandoc

macOS

macOS 系统提供编译后的 pkg 安装包,可运行安装;或下载免安装的 zip 压缩包解压使用。或使用 Homebrew 安装:

brew install pandoc

Linux

Debian、Ubuntu、Slackware、Arch、Fedora、NixOS、openSUSE、Gentoo 等主流发行版可直接使用系统包管理器安装。Pandoc 同时为 amd64 架构提供二进制安装包。

其他操作系统和安装方式见官方文档。

初步使用

进入安装目录,运行 pandoc(Windows 下为 pandoc.exe)确认版本:

d:\Software\pandoc-2.10.1>pandoc.exe --version
pandoc.exe 2.10.1
Compiled with pandoc-types 1.21, texmath 0.12.0.2, skylighting 0.8.5
Default user data directory: C:\Users\dongx\AppData\Roaming\pandoc
Copyright (C) 2006-2020 John MacFarlane
Web:  https://pandoc.org
This is free software; see the source for copying conditions.
There is no warranty, not even for merchantability or fitness
for a particular purpose.

当前目录创建 test.md 作为测试文件:

---
title: Test
...

# Test!

This is a test of *pandoc*.

- list one
- list two

转换为 HTML:

pandoc.exe test.md -f markdown -t html -s -o test.html

参数含义:

  • test.md:源文件
  • -f:输入文件格式
  • -t:输出文件格式
  • -s:生成“独立”文件,带页眉和页脚

默认转换方向就是 Markdown 到 HTML,所以上面的命令也可以省略 -f-t

生成的 test.html


Test

code{white-space: pre-wrap;}
span.smallcaps{font-variant: small-caps;}
span.underline{text-decoration: underline;}
div.column{display: inline-block; vertical-align: top; width: 50%;}
div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;}
ul.task-list{list-style: none;}
.display.math{display: block; text-align: center; margin: 0.5rem auto;}

Test

Test!

This is a test of *pandoc*.

- list one

- list two

转换为 LaTeX:

pandoc.exe test.md -f markdown -t latex -s -o test.tex

Pandoc 会根据文件扩展名推断输入和输出格式。例如直接指定 .docx 输出 Word 文档:

pandoc.exe test.md -s -o test.docx

如果安装了 LaTeX,可输出 PDF:

pandoc.exe test.md -f markdown -s -o test.pdf

输入 pandoc --help 查看选项帮助,详细用法见用户手册。

另外,Pandoc 还提供在线转换工具和格式转换示例。

Pandoc 集成

除命令行外,很多编辑器与工具集成了 Pandoc,例如 Markdown 编辑器 PanWriter、Typora,文本编辑器 Atom、Sublime Text、Emacs、Vim,以及 R Markdown、PanConvert、Manubot 等。

相关资源

  • Pandoc 官方网站:https://pandoc.org/
  • Pandoc GitHub 源码:https://github.com/jgm/pandoc
  • Pandoc 用户手册:https://pandoc.org/MANUAL.html

小结

Pandoc 是一款免费开源的文档格式转换工具,覆盖多种标记格式之间的双向或单向转换,适用于日常写作和电子书出版流程。

复制全文 生成海报 Pandoc 文档转换 Markdown 命令行

推荐文章

程序员茄子在线接单