Mr-Vicky-01 commited on
Commit
726d935
1 Parent(s): 8f294e3

Update file_creator.py

Browse files
Files changed (1) hide show
  1. file_creator.py +35 -39
file_creator.py CHANGED
@@ -1,40 +1,36 @@
1
- from docx import Document
2
- from docx.enum.text import WD_PARAGRAPH_ALIGNMENT
3
- from docx.shared import Pt
4
- from io import BytesIO
5
- import markdown
6
-
7
- class Create_Doc:
8
- def __init__(self) -> None:
9
- self.doc = Document()
10
-
11
- def markdown_to_word(self,markdown_text):
12
- # Convert Markdown to HTML
13
- # html = markdown.markdown(markdown_text)
14
-
15
- # Parse the Markdown text and add formatted content to the document
16
- for line in markdown_text.split('\n'):
17
- if line.startswith('# '):
18
- heading = line[2:]
19
- p = self.doc.add_heading(heading, level=1)
20
- elif line.startswith('## '):
21
- heading = line[3:]
22
- p = self.doc.add_heading(heading, level=2)
23
- elif line.startswith('### '):
24
- heading = line[4:]
25
- p = self.doc.add_heading(heading, level=3)
26
- elif line.startswith('- '):
27
- item = line[2:]
28
- p = self.doc.add_paragraph(item, style='ListBullet')
29
- else:
30
- p = self.doc.add_paragraph(line)
31
-
32
- # Adjust paragraph formatting (optional)
33
- p.alignment = WD_PARAGRAPH_ALIGNMENT.LEFT
34
- p.style.font.size = Pt(12)
35
-
36
- # Save the document to a BytesIO object
37
- buffer = BytesIO()
38
- self.doc.save(buffer)
39
- buffer.seek(0)
40
  return buffer
 
1
+ from docx import Document
2
+ from docx.enum.text import WD_PARAGRAPH_ALIGNMENT
3
+ from docx.shared import Pt
4
+ from io import BytesIO
5
+
6
+ class Create_Doc:
7
+ def __init__(self) -> None:
8
+ self.doc = Document()
9
+
10
+ def markdown_to_word(self,markdown_text):
11
+ # Parse the Markdown text and add formatted content to the document
12
+ for line in markdown_text.split('\n'):
13
+ if line.startswith('# '):
14
+ heading = line[2:]
15
+ p = self.doc.add_heading(heading, level=1)
16
+ elif line.startswith('## '):
17
+ heading = line[3:]
18
+ p = self.doc.add_heading(heading, level=2)
19
+ elif line.startswith('### '):
20
+ heading = line[4:]
21
+ p = self.doc.add_heading(heading, level=3)
22
+ elif line.startswith('- '):
23
+ item = line[2:]
24
+ p = self.doc.add_paragraph(item, style='ListBullet')
25
+ else:
26
+ p = self.doc.add_paragraph(line)
27
+
28
+ # Adjust paragraph formatting (optional)
29
+ p.alignment = WD_PARAGRAPH_ALIGNMENT.LEFT
30
+ p.style.font.size = Pt(12)
31
+
32
+ # Save the document to a BytesIO object
33
+ buffer = BytesIO()
34
+ self.doc.save(buffer)
35
+ buffer.seek(0)
 
 
 
 
36
  return buffer