Code Monkey
Code Monkey

Reputation: 844

markdown2 module - how do I convert a a markdown file from within a Python script?

I am using markdown2 module (found here: https://github.com/trentm/python-markdown2) The module is great but documentation is lacking.

I attempted something simple like converting a file using a Python script. I wanted to then use this to convert a batch of files. But even the first part is hitting a dead end. My code:

from markdown2 import Markdown
markdowner = Markdown()
markdowner.convert("file.md", "file.htm")

Could someone show me how?

Upvotes: 2

Views: 872

Answers (1)

Code Monkey
Code Monkey

Reputation: 844

NVM, I got the answer (and not from the repository mentioned above). Here's a simple Pythonic two-liner:

from markdown2 import Markdown
with open("file.htm", 'w') as output_file: output_file.write(Markdown().convert(open("file.md").read()))

Upvotes: 1

Related Questions