Skip to content

Commit 241d62f

Browse files
authored
Merge pull request #26 from AdityaJ7/html2markdown
HTML To Markdown script added
2 parents 8095700 + 7a043ec commit 241d62f

File tree

5 files changed

+68
-0
lines changed

5 files changed

+68
-0
lines changed

HTML_to_Markdown/README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
## HTML to Markdown converter
2+
3+
This script converts HTML Files into Markdown.
4+
5+
## Requirements for this script:
6+
7+
1.html2markdown
8+
9+
install this by running the following command:
10+
11+
pip install -r requirements.txt
12+
13+
## How to use this script?
14+
15+
Just type the following in your command prompt:
16+
17+
python html_to_markdown.py
18+
19+
## Sample of the script in action:
20+
21+
<p align = "center">
22+
<img src="how_to_use.PNG" alt="score">
23+
</p>
24+
<p align = "center">
25+
<img src="sample.PNG" alt="score">
26+
</p>

HTML_to_Markdown/how_to_use.PNG

10.7 KB
Loading

HTML_to_Markdown/html_to_markdown.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import html2markdown
2+
import sys
3+
4+
5+
def convert_me_to_markdown(input_html, output_md):
6+
"""This function take an HTML file as input and
7+
converts it into a markdown file. The name of
8+
the markdown file is set according to the
9+
output_md string.
10+
11+
Args:
12+
input_html (string): The input HTML file
13+
output_md (string): The name of the output markdown file
14+
"""
15+
16+
# Used context manger to open the input and output files.
17+
with open(input_html, 'r') as input_file:
18+
with open(output_md, 'w') as output_file:
19+
for data in input_file:
20+
# Writing the conveted data to the output file.
21+
output_file.write(html2markdown.convert(data))
22+
23+
24+
def main():
25+
26+
# The html file which undergoes conversion
27+
input_html = input("Enter your html file please: ")
28+
29+
# To make sure the user inputs only html files
30+
# HMTL files can end with .html or .htm
31+
if any(input_html.endswith(s) for s in ['.html', '.htm']):
32+
# To have a matching file name for the output file
33+
output_md = input_html.split('.')[0] + ".md"
34+
convert_me_to_markdown(input_html, output_md)
35+
else:
36+
sys.exit("Bruh this script takes html files only")
37+
38+
39+
if __name__ == "__main__":
40+
main()
41+
print("Your HTML file has been converted to Markdown successfully!!")

HTML_to_Markdown/requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
html2markdown==0.1.7

HTML_to_Markdown/sample.PNG

83.6 KB
Loading

0 commit comments

Comments
 (0)