Spaces:
Runtime error
Runtime error
Commit
·
1f634e0
1
Parent(s):
54b4c8b
Update app.py
Browse files
app.py
CHANGED
@@ -84,26 +84,29 @@ from bs4 import BeautifulSoup
|
|
84 |
|
85 |
import pdfkit
|
86 |
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
}
|
98 |
-
pdfkit.from_string(text, pdf_file, options=options)
|
99 |
|
100 |
-
#
|
101 |
-
|
|
|
|
|
102 |
|
103 |
-
#
|
104 |
-
|
|
|
|
|
|
|
|
|
105 |
|
106 |
-
return b64, filename
|
107 |
|
108 |
|
109 |
|
@@ -207,29 +210,14 @@ def editor_page():
|
|
207 |
edited_text = st_quill(quill_text)
|
208 |
|
209 |
st.write("Here is the edited obituary:")
|
210 |
-
st.
|
211 |
-
|
212 |
-
if st.button("Save as PDF"):
|
213 |
-
# parse the HTML content and extract the CSS styles
|
214 |
-
soup = BeautifulSoup(edited_text, 'html.parser')
|
215 |
-
css = ""
|
216 |
-
for style in soup.find_all('style'):
|
217 |
-
css += style.string
|
218 |
-
style.decompose()
|
219 |
|
220 |
-
|
221 |
-
new_html = f"""<html><head><style>{css}</style></head><body>{soup}</body></html>"""
|
222 |
-
|
223 |
-
# Save the output text as a PDF
|
224 |
-
pdf_data, filename = save_as_pdf(new_html)
|
225 |
|
226 |
-
|
227 |
-
|
228 |
-
|
229 |
-
|
230 |
-
file_name=filename,
|
231 |
-
mime='application/pdf'
|
232 |
-
)
|
233 |
|
234 |
# Add some custom CSS to style the editor
|
235 |
st.markdown("""
|
@@ -247,6 +235,7 @@ def editor_page():
|
|
247 |
.ql-editor {
|
248 |
height: 100%;
|
249 |
}
|
|
|
250 |
</style>
|
251 |
""", unsafe_allow_html=True)
|
252 |
|
|
|
84 |
|
85 |
import pdfkit
|
86 |
|
87 |
+
from docx import Document
|
88 |
+
from io import BytesIO
|
89 |
+
import base64
|
90 |
+
|
91 |
+
def save_as_doc(text):
|
92 |
+
# create a Word document
|
93 |
+
doc = Document()
|
94 |
+
|
95 |
+
# add the edited text to the document
|
96 |
+
doc.add_paragraph(text)
|
|
|
|
|
97 |
|
98 |
+
# save the document to a BytesIO object
|
99 |
+
doc_file = BytesIO()
|
100 |
+
doc.save(doc_file)
|
101 |
+
doc_file.seek(0)
|
102 |
|
103 |
+
# encode the document to base64
|
104 |
+
b64 = base64.b64encode(doc_file.getvalue()).decode('utf-8')
|
105 |
+
|
106 |
+
# generate a download link for the document
|
107 |
+
href = f'<a href="data:application/octet-stream;base64,{b64}" download="output.docx">Download DOCX</a>'
|
108 |
+
st.markdown(href, unsafe_allow_html=True)
|
109 |
|
|
|
110 |
|
111 |
|
112 |
|
|
|
210 |
edited_text = st_quill(quill_text)
|
211 |
|
212 |
st.write("Here is the edited obituary:")
|
213 |
+
st.write(edited_text)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
214 |
|
215 |
+
st.session_state.edited_text = edited_text
|
|
|
|
|
|
|
|
|
216 |
|
217 |
+
if st.button("Save as DOCX"):
|
218 |
+
# Save the output text as a Word document
|
219 |
+
save_as_doc(st.session_state.edited_text)
|
220 |
+
st.write("The custom obituary has been saved as a Word document.")
|
|
|
|
|
|
|
221 |
|
222 |
# Add some custom CSS to style the editor
|
223 |
st.markdown("""
|
|
|
235 |
.ql-editor {
|
236 |
height: 100%;
|
237 |
}
|
238 |
+
{edited_text.get('css')}
|
239 |
</style>
|
240 |
""", unsafe_allow_html=True)
|
241 |
|