HTML to Markdown Python
Introduction
Markdown, a lightweight markup language, makes writing structured content that is simple to read and understand on the web possible. On the other hand, web material is organized and displayed using HTML. Converting HTML text to Markdown can come in handy in cases where you wish to simplify the material or make it easier to read.
Using the markdownify module in Python is one method for converting HTML to Markdown. Markdown to HTML text conversion is made easy and effective with this program. Downloading and installing the markdownify package in your Python environment is required before you can start the conversion process. When the package is installed, you can import it and use its features to convert HTML text to Markdown.
Installation
You must install this module individually because Python does not come with it pre-installed. Use the following command in the terminal to install the module.
pip3 install markdownify
There are multiple steps involved in the process of using Python to convert HTML text to Markdown, as listed below.
Import module:
The first step is to include the markdownify module inside your Python script. This module incorporates a multiplicity of utilities, including converting HTML to Markdown.
Create HTML text:
The HTML text that you intend to transform to Markdown text is produced there. You have two options: writing down the content by hand or downloading the content using Python libraries, such as requests inside a file or from a webpage.
Use the markdownify() function and pass the text to it:
Once you have the HTML text, you may convert it to Markdown by using the markdown () function offered by the markdown module. This method accepts an HTML text as input and outputs the corresponding Markdown content.
Display markdowned text:
Finally, the Markdown text can be seen in the console or saved to a file by utilizing the built-in Python routines.
The general flow of this method involves importing the required module, inputting the HTML text, and passing it through the markdownify() function to get the Markdown equivalent. After that, either write or show the output. This procedure can be helpful when you want to convert HTML information to Markdown so that formatting and reading are simpler.
Example 1: Converting HTML to Markdown
Let's now concentrate on the code that will be used to transform plain HTML to markdown.
Examine the code displayed below. In this code, the markdownify module is the module's first import. After that, we make some HTML content which will automatically transform into Markdown. The highlighted section is an early HTML heading and paragraph.
Next, we evolve the HTML text to the Markdown format with the markdownify() function. This function creates the required Markdown text and emits it right after it receives the HTML content as input.
Example
Lastly, we line this up with the print function to display the translated Markdown. The result is the translated Markdown that goes for the initial HTML input.
Main.py
# Import markdownify module
import markdownify
# Create HTML text to be converted
html_text = "<h1>HTML to Markdown Python</h1><p>This is the Example code of converting HTML to Markdown in Python</p>"
# Use markdownify() function to convert HTML to Markdown
markdown_text = markdownify.markdownify(html_text)
# Display the converted Markdown text
print(markdown_text)
Output
# Import markdownify module import markdownify # Create HTML text to be converted html_text = "
HTML to Markdown Python
This is the Example code of converting HTML to Markdown in Python
" # Use markdownify() function to convert HTML to Markdown markdown_text = markdownify.markdownify(html_text) # Display the converted Markdown text print(markdown_text)
Example 2
Let us look at another example of quite complicated HTML code. Look at the code that is displayed below.
Main.py
# Import markdownify module
import markdownify
# Create complex HTML text to be converted
html_text = """
<div class="article">
<h1>HTML to Markdown Python</h1>
<p>This is the Example code of converting HTML to Markdown in Python.</p>
<ul>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
</ul>
<a href="https://www.javatpoint.com">Link to Javatpoint/a>
</div>
"""
# Use markdownify() function to convert HTML to Markdown
markdown_text = markdownify.markdownify(html_text)
# Display the converted Markdown text
print(markdown_text)
Output
# Import markdownify module import markdownify # Create complex HTML text to be converted html_text = """
HTML to Markdown Python
This is the Example code of converting HTML to Markdown in Python.
Item 1
Item 2
Item 3
Conclusion
In conclusion, using Python to convert HTML to Markdown can be a helpful method for formatting and displaying text on the web. This procedure can be done by employing the Markdownify module, which helps to convert html text into markdown format quickly..