-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfile_utils.py
137 lines (104 loc) · 4.14 KB
/
file_utils.py
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import base64
from io import BytesIO
import os
from doc2docx import convert
from docx import Document
import docx
import markdown2
from htmldocx import HtmlToDocx
from docx.shared import Pt
from docx.enum.table import WD_TABLE_ALIGNMENT
from docx.oxml import OxmlElement
from docx.oxml.ns import qn
from docx.shared import RGBColor
def generate_document(text, idx,title):
# Convert the Markdown content to HTML using markdown2
html = markdown2.markdown(text, extras=["tables"])
# Create a new Word document
doc = docx.Document()
# Add the title to the document
doc.add_heading(title, 0)
# Convert HTML to Word document using HtmlToDocx
html_to_docx_parser = HtmlToDocx()
html_to_docx_parser.add_html_to_document(html, doc)
# Add borders to all tables in the document
for table in doc.tables:
set_table_border(table, border_size=4)
# Optionally, set a background color for header row
for cell in table.rows[0].cells:
cell.paragraphs[0].runs[0].font.color.rgb = RGBColor(255, 255, 255)
shading_elm = OxmlElement('w:shd')
shading_elm.set(qn('w:fill'), '2F5496')
cell._tc.get_or_add_tcPr().append(shading_elm)
# Save the document to a BytesIO object
file_bytes = BytesIO()
doc.save(file_bytes)
file_bytes.seek(0)
text_bytes = file_bytes.read()
return text_bytes
def generate_download_link(text,title):
# Convert the Markdown content to HTML using markdown2
html = markdown2.markdown(text, extras=["tables"])
# Create a new Word document
doc = docx.Document()
# Add the title to the document
doc.add_heading(title, 0)
# Convert HTML to Word document using HtmlToDocx
html_to_docx_parser = HtmlToDocx()
html_to_docx_parser.add_html_to_document(html, doc)
# Add borders to all tables in the document
for table in doc.tables:
set_table_border(table, border_size=4)
# Optionally, set a background color for header row
for cell in table.rows[0].cells:
cell.paragraphs[0].runs[0].font.color.rgb = RGBColor(255, 255, 255)
shading_elm = OxmlElement('w:shd')
shading_elm.set(qn('w:fill'), '2F5496')
cell._tc.get_or_add_tcPr().append(shading_elm)
# Save the document to a BytesIO object
file_bytes = BytesIO()
doc.save(file_bytes)
file_bytes.seek(0)
# Create a download link for the Word document
download_link = create_download_link(file_bytes.read(),
f"{title}.docx")
return download_link
def create_download_link(val, filename):
b64 = base64.b64encode(val)
return f'<a href="data:application/vnd.openxmlformats-officedocument.wordprocessingml.document;base64,{b64.decode()}" download="{filename}">Download file</a>'
def extract_doc_text(uploaded_file):
tmpdirname = './tmp'
if not os.path.exists(tmpdirname):
os.makedirs(tmpdirname)
# Define the paths for the input and output files
input_file_path = os.path.join(tmpdirname, uploaded_file.name)
output_file_path = os.path.join(tmpdirname, "output.docx")
# Save the uploaded file to the tmp location
with open(input_file_path, 'wb') as f:
f.write(uploaded_file.read())
try:
convert(input_file_path, output_file_path)
all_text = extract_docx_text(output_file_path)
return all_text
finally:
# Clean up the temporary files if needed
if os.path.exists(input_file_path):
os.remove(input_file_path)
if os.path.exists(output_file_path):
os.remove(output_file_path)
def extract_docx_text(docx_file):
doc = Document(docx_file)
full_text = []
for paragraph in doc.paragraphs:
full_text.append(paragraph.text)
return '\n'.join(full_text)
def set_table_border(table, border_size=1):
tbl_pr = table._element.xpath('w:tblPr')[0]
borders = OxmlElement('w:tblBorders')
for border in ['top', 'left', 'bottom', 'right', 'insideH', 'insideV']:
edge = OxmlElement(f'w:{border}')
edge.set(qn('w:sz'), str(border_size))
edge.set(qn('w:val'), 'single')
edge.set(qn('w:color'), '000000')
borders.append(edge)
tbl_pr.append(borders)