-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathmd2docx_python.py
More file actions
43 lines (37 loc) · 1.53 KB
/
md2docx_python.py
File metadata and controls
43 lines (37 loc) · 1.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import markdown
from docx import Document
from bs4 import BeautifulSoup
def markdown_to_word(markdown_file, word_file):
# Reading the Markdown file
with open(markdown_file, 'r', encoding='utf-8') as file:
markdown_content = file.read()
# Converting Markdown to HTML
html_content = markdown.markdown(markdown_content)
# Creating a new Word Document
doc = Document()
# Converting HTML to text and add it to the Word Document
soup = BeautifulSoup(html_content, 'html.parser')
# Adding content to the Word Document
for element in soup:
if element.name == 'h1':
doc.add_heading(element.text, level=1)
elif element.name == 'h2':
doc.add_heading(element.text, level=2)
elif element.name == 'h3':
doc.add_heading(element.text, level=3)
elif element.name == 'p':
paragraph = doc.add_paragraph()
for child in element.children:
if child.name == 'strong':
paragraph.add_run(child.text).bold = True
elif child.name == 'em':
paragraph.add_run(child.text).italic = True
else:
paragraph.add_run(child)
elif element.name == 'ul':
for li in element.find_all('li'):
doc.add_paragraph(li.text, style='List Bullet')
elif element.name == 'ol':
for li in element.find_all('li'):
doc.add_paragraph(li.text, style='List Number')
doc.save(word_file)