Spaces:
Sleeping
Sleeping
Mr-Vicky-01
commited on
Commit
•
726d935
1
Parent(s):
8f294e3
Update file_creator.py
Browse files- 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 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
heading =
|
19 |
-
|
20 |
-
|
21 |
-
heading =
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
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
|