amoldwalunj commited on
Commit
1f634e0
1 Parent(s): 54b4c8b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -38
app.py CHANGED
@@ -84,26 +84,29 @@ from bs4 import BeautifulSoup
84
 
85
  import pdfkit
86
 
87
- def save_as_pdf(text):
88
- # create a PDF from the HTML content
89
- pdf_file = BytesIO()
90
- options = {
91
- 'quiet': '',
92
- 'page-size': 'Letter',
93
- 'margin-top': '0.75in',
94
- 'margin-right': '0.75in',
95
- 'margin-bottom': '0.75in',
96
- 'margin-left': '0.75in',
97
- }
98
- pdfkit.from_string(text, pdf_file, options=options)
99
 
100
- # encode the PDF to base64
101
- b64 = base64.b64encode(pdf_file.getvalue()).decode('utf-8')
 
 
102
 
103
- # generate the filename
104
- filename = 'output.pdf'
 
 
 
 
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.session_state.edited_text = edited_text
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
- # create a new HTML document with the CSS styles inline
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
- # display a download button for the PDF file
227
- st.download_button(
228
- label='Download PDF',
229
- data=pdf_data,
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