Pandoc 支持不同格式文件的转换:包括 Markdown, HTML, LaTeX,Word docx。

安装

brew install pandoc

常见格式

格式 说明 说明
pptx powerpoint 输出
docx word 输入/输出
pdf pdf 输出
html html 输入/输出
json JSON version of native AST 输入/输出
markdown Pandoc’s Markdown 输入/输出
markdown_mmd MultiMarkdown 输入/输出
markdown_phpextra (PHP Markdown Extra 输入/输出
markdown_strict original unextended Markdown 输入/输出

输入格式

'biblatex', 'bibtex', 'commonmark', 'commonmark_x', 'creole', 'csljson', 'csv', 'docbook', 'docx', 'dokuwiki', 'endnotexml', 'epub', 'fb2', 'gfm', 'haddock', 'html', 'ipynb', 'jats', 'jira', 'json', 'latex', 'man', 'markdown', 'markdown_github', 'markdown_mmd', 'markdown_phpextra', 'markdown_strict', 'mediawiki', 'muse', 'native', 'odt', 'opml', 'org', 'ris', 'rst', 'rtf', 't2t', 'textile', 'tikiwiki', 'tsv', 'twiki', 'vimwiki'

输出格式

'asciidoc', 'asciidoctor', 'beamer', 'biblatex', 'bibtex', 'commonmark', 'commonmark_x', 'context', 'csljson', 'docbook', 'docbook4', 'docbook5', 'docx', 'dokuwiki', 'dzslides', 'epub', 'epub2', 'epub3', 'fb2', 'gfm', 'haddock', 'html', 'html4', 'html5', 'icml', 'ipynb', 'jats', 'jats_archiving', 'jats_articleauthoring', 'jats_publishing', 'jira', 'json', 'latex', 'man', 'markdown', 'markdown_github', 'markdown_mmd', 'markdown_phpextra', 'markdown_strict', 'markua', 'mediawiki', 'ms', 'muse', 'native', 'odt', 'opendocument', 'opml', 'org', 'pdf', 'plain', 'pptx', 'revealjs', 'rst', 'rtf', 's5', 'slideous', 'slidy', 'tei', 'texinfo', 'textile', 'xwiki', 'zimwiki'

选项

--list-input-formats 查看输入格式
--list-output-formats 查看输出格式
-f 指定输入格式
-t 指定输出格式
-o 指定输出文件
-p, --preserve-tabs
-s, --standalone** 输出选项。输出单文件 (pdf, epub, epub3, fb2, docx, odt 输出格式)
–extract-media=DIR 输入选项。读取文件时,将源文件内容中的图片指定DIR目录

格式转换例子

pandoc -f markdown -t latex hello.txt
pandoc -f docx -t markdown --extract-media ./images -o test.md test.docx
pandoc -f docx -t markdown --extract-media ./images -o test.md e:\\test.docx
pandoc test.txt -o test.pdf
pandoc -f html -t markdown --request-header User-Agent:"Mozilla/5.0" https://www.fsf.org

说明:
pandoc默认使用LaTeX生成PDF,需要安装LaTeX引擎。

字符编码

输入输出默认为UTF-8编码 。如果不是,则使用管道进行编码

iconv -t utf-8 input.txt | pandoc | iconv -f utf-8

Python调用Pandoc

安装pypandoc包

pip install pypandoc

调用

import pypandoc

print(pypandoc.get_version())
# 2.19.1

print(pypandoc.get_pandoc_formats()[0])  # 列出支持的源文件格式
# ['biblatex', 'bibtex', 'commonmark', 'commonmark_x', 'creole', 'csljson', 'csv', 'docbook', 'docx', 'dokuwiki', 'endnotexml', 'epub', 'fb2', 'gfm', 'haddock', 'html', 'ipynb', 'jats', 'jira', 'json', 'latex', 'man', 'markdown', 'markdown_github', 'markdown_mmd', 'markdown_phpextra', 'markdown_strict', 'mediawiki', 'muse', 'native', 'odt', 'opml', 'org', 'ris', 'rst', 'rtf', 't2t', 'textile', 'tikiwiki', 'tsv', 'twiki', 'vimwiki']

print(pypandoc.get_pandoc_formats()[1])  # 列出支持的输出格式
# ['asciidoc', 'asciidoctor', 'beamer', 'biblatex', 'bibtex', 'commonmark', 'commonmark_x', 'context', 'csljson', 'docbook', 'docbook4', 'docbook5', 'docx', 'dokuwiki', 'dzslides', 'epub', 'epub2', 'epub3', 'fb2', 'gfm', 'haddock', 'html', 'html4', 'html5', 'icml', 'ipynb', 'jats', 'jats_archiving', 'jats_articleauthoring', 'jats_publishing', 'jira', 'json', 'latex', 'man', 'markdown', 'markdown_github', 'markdown_mmd', 'markdown_phpextra', 'markdown_strict', 'markua', 'mediawiki', 'ms', 'muse', 'native', 'odt', 'opendocument', 'opml', 'org', 'pdf', 'plain', 'pptx', 'revealjs', 'rst', 'rtf', 's5', 'slideous', 'slidy', 'tei', 'texinfo', 'textile', 'xwiki', 'zimwiki']