Python : How to convert markdown formatted text to text

后端 未结 4 1356
迷失自我
迷失自我 2020-12-23 09:23

I need to convert markdown text to plain text format to display summary in my website. I want the code in python.

4条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-23 10:02

    This module will help do what you describe:

    http://www.freewisdom.org/projects/python-markdown/Using_as_a_Module

    Once you have converted the markdown to HTML, you can use a HTML parser to strip out the plain text.

    Your code might look something like this:

    from BeautifulSoup import BeautifulSoup
    from markdown import markdown
    
    html = markdown(some_html_string)
    text = ''.join(BeautifulSoup(html).findAll(text=True))
    

提交回复
热议问题