把markdown文件转换为html文件

  • 使用pip install markdown模块
  • 只做到了分行;
  • 表格,-,和空格还没能无缝转换
  • 代码如下:
import os
import codecs
import markdown

def convert_markdown_to_html(markdown_file):
    with codecs.open(markdown_file,'r',encoding='utf-8') as file:
        markdown_text = file.read()
        html_text = markdown.markdown(markdown_text,extensions=['nl2br'])

    html_file = os.path.splitext(markdown_file)[0] + '.html'
    with codecs.open(html_file,'w',encoding='utf-8') as file:
        file.write(html_text)

markdown_file = ".//test.md"
convert_markdown_to_html(markdown_file)
posted @ 2024-05-16 13:27  不愿透露姓名的小村村  阅读(3)  评论(0编辑  收藏  举报