How To Convert Medium Articles In Markdown Format
Published: 2023-09-30 18:00:00

> Medium Articles → Markdown → HTML

There are several reasons why you might want to convert a Medium article from its original format to Markdown and then to HTML. In this article, I'll explain to you how you can do it using Python programming language with a few simple steps.

### Why might you wanna do it?

Some of the benefits of using Markdown include:

- Markdown is a simple, lightweight markup language that is easy to read and write. This makes it a convenient format for storing and editing articles, especially when compared to more complex formats such as HTML or XML.

- Markdown supports a wide range of formatting options, such as headings, lists, links, and more, that can be applied using a simple and intuitive syntax. This allows you to add structure and formatting to your articles without having to use complex tags or styles.

- Markdown is supported by a wide range of tools and platforms, including text editors, publishing systems, and more. This makes it easy to work with Markdown content and allows you to integrate your articles with other applications or services.

- Markdown is a widely used and well-documented format with a large and active community of users. This means that you can easily find resources and support for working with Markdown and can share your articles with others who use the format.

Overall, converting a Medium article to Markdown can provide many benefits, such as making it easier to edit and format the article and enabling you to integrate it with other tools and platforms.

Whether or not this is the right approach for you will depend on your specific needs and goals.

Now let's jump to the code!

---

## Python Code to convert Medium Article into Markdown and HTML


{% gist https://gist.github.com/weeping-angel/1debdba9040e1752a98f173f2d559f63 file=article_markdown_to_html.py %}

---

## Code Explanation -

The code you provided is a Python script that uses the `medium_api` library to access the Unofficial Medium API and convert an article from Markdown to HTML.

You can install `medium-api` using the following command:

```bash
pip install medium-api
```

Here is a brief explanation of what the code does:

{% gist https://gist.github.com/weeping-angel/1debdba9040e1752a98f173f2d559f63 file=article_markdown_to_html_1.py %}

- The first two lines import the `os` and `markdown` libraries, which are used to access environmental variables and convert Markdown to HTML, respectively.


{% gist https://gist.github.com/weeping-angel/1debdba9040e1752a98f173f2d559f63 file=article_markdown_to_html_2.py %}


- The third line imports the `Medium` class from the `medium_api` library, which is used to access the Unofficial Medium API.


{% gist https://gist.github.com/weeping-angel/1debdba9040e1752a98f173f2d559f63 file=article_markdown_to_html_3.py %}


- The next three lines extract the ID of an article from its URL, and use the `Medium` object to retrieve the article data from the API. This data is stored in an `article` object.


{% gist https://gist.github.com/weeping-angel/1debdba9040e1752a98f173f2d559f63 file=article_markdown_to_html_4.py %}


- The eighth line uses the `markdown` library to convert the article's Markdown content to HTML, and stores the result in a variable named `html_output`.


{% gist https://gist.github.com/weeping-angel/1debdba9040e1752a98f173f2d559f63 file=article_markdown_to_html_5.py %}


- The last three lines write the HTML to a file named `index.html`, replacing all occurrences of the `( ' )` character with a single quote.

Overall, this code demonstrates how to use the `medium_api` library to access the Unofficial Medium API and convert an article from Markdown to HTML. It is not a complete application but rather a sample of how the API can be used in a Python script.

---

## Resources 🚀

- You can subscribe to Unofficial Medium API here: [Medium API - RapidAPI](http://hub.mediumapi.com/)
- Read its Swagger documentation: [Medium API - Documentation](https://docs.mediumapi.com/)
- Bookmark Python SDK's GitHub repository: [Medium API - GitHub](https://github.com/weeping-angel/medium-api)
- See more example usage here: [Medium API - ReadTheDocs](https://medium-api.readthedocs.io/en/latest/usage.html)

Thanks for reading, and have a nice day!

---

**PS:** We post tutorials, videos, code snippets, and insights related to Medium and our API. So be sure to follow us on:

- Twitter: [twitter.com/medium_api](https://twitter.com/medium_api)
- LinkedIn: [www.linkedin.com/company/medium-api](https://www.linkedin.com/company/medium-api)

---

**Related Articles -**

- [Unofficial Medium API - Documentation](https://mediumapi.com/documentation.html)
- [Unofficial Medium API: Get Posts Using Python](https://mediumapi.com/get-posts-using-python.html)
- [How To List Hundreds of Niche Top Writers of Medium](https://mediumapi.com/list-top-writers-of-medium.html)