python md2pdf - 命令行Markdown到PDF转换器,支持CSS样式表和自定义字体

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python md2pdf - 命令行Markdown到PDF转换器,支持CSS样式表和自定义字体相关的知识,希望对你有一定的参考价值。

#!/usr/bin/env python

import os
import sys

from markdown2 import markdown
from xhtml2pdf import pisa

"""
## Inspired by 

https://github.com/walle/gimli
https://gist.github.com/SpotlightKid/0efb4d07f28af1c8fc1b

## Documentation of underlying tools
http://xhtml2pdf.appspot.com/static/pisa-en.html

Required packages: markdown2, xhtml2pdf, html5lib
"""

DEFAULT_EXTRAS = [
    'fenced-code-blocks',
    'footnotes',
    'metadata',
    'pyshell',
    'smarty-pants',
    'tag-friendly',
    'wiki-tables'
]

def main(md_file, css_file):
    # read css file
    from xhtml2pdf.default import DEFAULT_CSS
    if css_file is not None:
        with open(css_file) as f:
            DEFAULT_CSS += f.read()
    
    # read markdown
    with open(md_file) as f:
        md = f.read()

    # to html
    html = markdown(md, extras=DEFAULT_EXTRAS)

    base_with_path = os.path.splitext(sys.argv[1])[0]
    basename = os.path.basename(base_with_path)

    with open('%s.pdf'%basename, "wb") as fp:
        # convert HTML to PDF
        pisa.CreatePDF(html, dest=fp, default_css=DEFAULT_CSS)

if __name__ == '__main__':
    if len(sys.argv) == 1:
        print('Usage: md2pdf input.md style.css')
        print('       You can also set a CSS file as env var.')
        print('       E.g. export MD2PDF_CSS=style.css')
        sys.exit()
    if len(sys.argv) == 2:
        css = os.getenv('MD2PDF_CSS')
    if len(sys.argv) == 3:
        css = sys.argv[2]

    main(sys.argv[1], css)

以上是关于python md2pdf - 命令行Markdown到PDF转换器,支持CSS样式表和自定义字体的主要内容,如果未能解决你的问题,请参考以下文章

python开发命令行脚本

Python笔记:命令行参数解析

python命令行补全

用于运行命令行的 Python 脚本,该命令行启动具有特定 Python 版本的 Python 脚本

python执行命令行操作

python3--命令行执行加参数