pandoc是一款通用的文档转换工具,可以支持大量标记语言之间的格式转换,例如:Markdown、Microsoft Word、PowerPoint、Jupyter Notebook、HTML、PDF、LaTeX、Wiki、EPUB格式之间的相互转换,同时pandoc也是一个开源免费的工具,支持Windows、macOS、Linux等系统,堪称文档格式转换神器。

pandoc的安装也非常简单:

下载地址:https://github.com/jgm/pandoc/releases/latest

pandoc支持众多格式的转换:

输入格式:

输出格式:

pandoc命令格式:pandoc <files> <options>


pandoc [OPTIONS] [FILES]
  # 指定输入文件格式,默认为 Markdown
  -f FORMAT, -r FORMAT  --from=FORMAT, --read=FORMAT  
  # 指定输出文件格式,默认为 HTML
  -t FORMAT, -w FORMAT  --to=FORMAT, --write=FORMAT   
  # 指定输出文件名称,该项缺省时,将输出到标准输出                  
  -o FILE               --output=FILE                                   
                        --data-dir=DIRECTORY   
  # 指定元数据                         
  -M KEY[:VALUE]        --metadata=KEY[:VALUE]                          
                        --metadata-file=FILE                            
  -d FILE               --defaults=FILE                                 
                        --file-scope   
  # 生成独立文件                                 
  -s                    --standalone                                    
                        --template=FILE    
  # 变量赋值                             
  -V KEY[:VALUE]        --variable=KEY[:VALUE]                          
                        --wrap=auto|none|preserve                       
                        --ascii                                         
                        --toc, --table-of-contents                      
                        --toc-depth=NUMBER                              
  # 指定章节、块的数量
  -N                    --number-sections                               
                        --number-offset=NUMBERS                         
                        --top-level-division=section|chapter|part       
                        --extract-media=PATH                            
                        --resource-path=SEARCHPATH                     
  # 文件头 
  -H FILE               --include-in-header=FILE                        
  # 主体前包含文件
  -B FILE               --include-before-body=FILE                      
  # 主体后包含文件
  -A FILE               --include-after-body=FILE                       
                        --no-highlight                                  
                        --highlight-style=STYLE|FILE                    
                        --syntax-definition=FILE                        
                        --dpi=NUMBER                                    
                        --eol=crlf|lf|native                            
                        --columns=NUMBER            
  # 保留制表符,而不是将其转换为空格                    
  -p                    --preserve-tabs                                 
                        --tab-stop=NUMBER                               
                        --pdf-engine=PROGRAM                            
                        --pdf-engine-opt=STRING                         
                        --reference-doc=FILE                            
                        --self-contained                                
                        --request-header=NAME:VALUE                     
                        --no-check-certificate                          
                        --abbreviations=FILE                            
                        --indented-code-classes=STRING                  
                        --default-image-extension=extension             
  -F PROGRAM            --filter=PROGRAM                                
  -L SCRIPTPATH         --lua-filter=SCRIPTPATH                         
                        --shift-heading-level-by=NUMBER                 
                        --base-header-level=NUMBER                      
                        --strip-empty-paragraphs                        
                        --track-changes=accept|reject|all               
                        --strip-comments                                
                        --reference-links                               
                        --reference-location=block|section|document     
                        --atx-headers                                   
                        --listings                                      
  -i                    --incremental                                   
                        --slide-level=NUMBER                            
                        --section-divs                                  
                        --html-q-tags                                   
                        --email-obfuscation=none|javascript|references  
                        --id-prefix=STRING                              
  -T STRING             --title-prefix=STRING                           
  -c URL                --css=URL                                       
                        --epub-subdirectory=DIRNAME                     
                        --epub-cover-image=FILE                         
                        --epub-metadata=FILE                            
                        --epub-embed-font=FILE                          
                        --epub-chapter-level=NUMBER                     
                        --ipynb-output=all|none|best                    
  -C                    --citeproc                                      
                        --bibliography=FILE                             
                        --csl=FILE                                      
                        --citation-abbreviations=FILE                   
                        --natbib                                        
                        --biblatex                                      
                        --mathml                                        
                        --webtex[=URL]                                  
                        --mathjax[=URL]                                 
                        --katex[=URL]                                   
                        --gladtex                                       
                        --trace                                         
                        --dump-args                                     
                        --ignore-args                                   
                        --verbose                                       
                        --quiet                                         
                        --fail-if-warnings                              
                        --log=FILE                                      
                        --bash-completion                               
                        --list-input-formats                            
                        --list-output-formats                           
                        --list-extensions[=FORMAT]                      
                        --list-highlight-languages                      
                        --list-highlight-styles                         
  -D FORMAT             --print-default-template=FORMAT                 
                        --print-default-data-file=FILE                  
                        --print-highlight-style=STYLE|FILE  
  # 显示程序的版本号            
  -v                    --version     
  # 显示程序的帮助信息                                  
  -h                    --help

示例:

  1. 生成HTML:pandoc MANUAL.txt -o example1.html
  2. 生成独立HTML文件:pandoc -s MANUAL.txt -o example2.html
  3. 生成HTML文件和自定义格式:pandoc -s --toc -c pandoc.css -A footer.html MANUAL.txt -o example3.html
  4. LaTeX文件:pandoc -s MANUAL.txt -o example4.tex
  5. 从LaTeX转换成markdown:pandoc -s example4.tex -o example5.text
  6. 文档重构:pandoc -s -t rst --toc MANUAL.txt -o example6.text
  7. 富文本文件(RTF):pandoc -s MANUAL.txt -o example7.rtf
  8. Beamer格式:pandoc -t beamer SLIDES -o example8.pdf
  9. DocBook格式pandoc -s -t docbook MANUAL.txt -o example9.db
  10. Man页面:pandoc -s -t man pandoc.1.md -o example10.1
  11. ConTeXt格式:pandoc -s -t context MANUAL.txt -o example11.tex
  12. 将web页面转换成markdown:pandoc -s -r html http://www.gnu.org/software/make/ -o example12.text
  13. 将markdown转换为PDF:pandoc MANUAL.txt --pdf-engine=xelatex -o example13.pdf
  14. PDF 和自定义文件头:pandoc -N --template=template.tex --variable mainfont="Palatino" --variable sansfont="Helvetica" --variable monofont="Menlo" --variable fontsize=12pt --variable version=2.0 MANUAL.txt --pdf-engine=xelatex --toc -o example14.pdf
  15. ipynb (Jupyter notebook)文件格式:pandoc example15.md -o example15.ipynb
  16. HTML幻灯片模式:pandoc -s --mathml -i -t dzslides SLIDES -o example16a.htmlpandoc -s --webtex -i -t slidy SLIDES -o example16b.htmlpandoc -s --mathjax -i -t revealjs SLIDES -o example16d.html
  17. 在HTML中呈现TeX math公式:pandoc math.text -s -o mathDefault.htmlpandoc math.text -s --mathml -o mathMathML.htmlpandoc math.text -s --webtex -o mathWebTeX.htmlpandoc math.text -s --mathjax -o mathMathJax.htmlpandoc math.text -s --katex -o mathKaTeX.html
  18. 代码块高亮:pandoc code.text -s --highlight-style pygments -o example18a.htmlpandoc code.text -s --highlight-style kate -o example18b.htmlpandoc code.text -s --highlight-style monochrome -o example18c.htmlpandoc code.text -s --highlight-style espresso -o example18d.htmlpandoc code.text -s --highlight-style haddock -o example18e.htmlpandoc code.text -s --highlight-style tango -o example18f.htmlpandoc code.text -s --highlight-style zenburn -o example18g.html
  19. GNU Texinfo, 转换成info和HTML格式:pandoc MANUAL.txt -s -o example19.teximakeinfo --no-validate --force example19.texi -o example19.infomakeinfo --no-validate --force example19.texi --html -o example19
  20. OpenDocument XML格式:pandoc MANUAL.txt -s -t opendocument -o example20.xml
  21. ODT (OpenDocument Text,OpenOffice)格式:pandoc MANUAL.txt -o example21.odt
  22. MediaWiki 格式:pandoc -s -t mediawiki --toc MANUAL.txt -o example22.wiki
  23. EPUB 格式:pandoc MANUAL.txt -o MANUAL.epub
  24. Markdown 引用:pandoc -s --bibliography biblio.bib --citeproc CITATIONS -o example24a.htmlpandoc -s --bibliography biblio.json --citeproc --csl chicago-fullnote-bibliography.csl CITATIONS -o example24b.htmlpandoc -s --bibliography biblio.yaml --citeproc --csl ieee.csl CITATIONS -t man -o example24c.1
  25. Textile 文件:pandoc -s MANUAL.txt -t textile -o example25.textile
  26. Textile 阅读:pandoc -s example25.textile -f textile -t html -o example26.html
  27. Org-模式:pandoc -s MANUAL.txt -o example27.org
  28. Ascii文档:pandoc -s MANUAL.txt -t asciidoc -o example28.txt
  29. Word docx文档:pandoc -s MANUAL.txt -o example29.docx
  30. 从LaTeX转化到docx:pandoc -s math.tex -o example30.docx
  31. 从DocBook转换为markdown:pandoc -f docbook -t markdown -s howto.xml -o example31.text
  32. 从MediaWiki转化为html5:pandoc -f mediawiki -t html5 -s haskell.wiki -o example32.html
  33. 自定义格式:pandoc -t sample.lua example33.text -o example33.html
  34. 指定模版的Docx格式:pandoc --reference-doc twocolumns.docx -o UsersGuide.docx MANUAL.txt
  35. 从Docx转化为markdown格式:pandoc -s example30.docx -t markdown -o example35.md
  36. 从EPUB转换为text文本:pandoc MANUAL.epub -t plain -o example36.text
  37. 用指定模版呈现结构化数据:pandoc fishwatch.yaml -t rst --template fishtable.rst -o fish.rst # see also the partial species.rst
  38. 从BibTeX转化为CSL JSON:pandoc biblio.bib -t csljson -o biblio.json
  39. 指定引用数据内容:pandoc biblio.bib --citeproc --csl ieee.csl -s -o biblio.html

在线演示:https://pandoc.org/try/

参考资料:

1.https://pandoc.org/