ahm14 commited on
Commit
2244f52
·
verified ·
1 Parent(s): 13ab744

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -21
app.py CHANGED
@@ -195,7 +195,38 @@ def merge_metadata_with_generated_data(generated_data, excel_metadata):
195
  generated_data[post_number] = post_data
196
  return generated_data
197
 
198
- # Create DOCX file matching the uploaded format
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
199
  def create_docx_from_data(extracted_data):
200
  doc = Document()
201
  for post_number, data in extracted_data.items():
@@ -212,34 +243,19 @@ def create_docx_from_data(extracted_data):
212
  para = doc.add_paragraph()
213
  run = para.add_run(f"**{key}:** {value}")
214
  run.font.size = Pt(11)
215
- # Instead of adding the Frames as plain text, add a proper table if FramesMapping is present.
 
216
  if "FramesMapping" in data:
217
  doc.add_paragraph("Frames:")
218
- mapping = data["FramesMapping"]
219
- # Create a table with header row and one row per frame
220
- table = doc.add_table(rows=1, cols=5)
221
- table.style = "Light List Accent 1"
222
- hdr_cells = table.rows[0].cells
223
- hdr_cells[0].text = "Frame"
224
- hdr_cells[1].text = "Major Focus"
225
- hdr_cells[2].text = "Significant Focus"
226
- hdr_cells[3].text = "Minor Mention"
227
- hdr_cells[4].text = "Not Applicable"
228
- tick = "✓"
229
- for frame, category in mapping.items():
230
- row_cells = table.add_row().cells
231
- row_cells[0].text = frame
232
- row_cells[1].text = tick if category == "Major Focus" else ""
233
- row_cells[2].text = tick if category == "Significant Focus" else ""
234
- row_cells[3].text = tick if category == "Minor Mention" else ""
235
- row_cells[4].text = tick if category == "Not Applicable" else ""
236
  else:
237
- # Fallback: if no FramesMapping, simply add the plain text value for "Frames"
238
  value = data.get("Frames", "N/A")
239
  doc.add_paragraph(f"**Frames:** {value}")
240
  doc.add_paragraph("\n")
241
  return doc
242
 
 
243
  # -------------------------------------------------------------------
244
  # Streamlit App UI
245
  # -------------------------------------------------------------------
 
195
  generated_data[post_number] = post_data
196
  return generated_data
197
 
198
+ def add_frames_table(doc, mapping):
199
+ """
200
+ Adds a well-structured table for the frames mapping.
201
+ Each row corresponds to a frame from the mapping and shows which category is ticked.
202
+ """
203
+ # Create a table with 1 header row and 5 columns.
204
+ table = doc.add_table(rows=1, cols=5)
205
+ table.style = "Table Grid" # Use a grid style for clearer borders
206
+ hdr_cells = table.rows[0].cells
207
+ hdr_cells[0].text = "Frame"
208
+ hdr_cells[1].text = "Major Focus"
209
+ hdr_cells[2].text = "Significant Focus"
210
+ hdr_cells[3].text = "Minor Mention"
211
+ hdr_cells[4].text = "Not Applicable"
212
+
213
+ tick = "✓"
214
+ # Add one row per frame in the mapping.
215
+ for frame, category in mapping.items():
216
+ row_cells = table.add_row().cells
217
+ row_cells[0].text = frame
218
+ row_cells[1].text = tick if category == "Major Focus" else ""
219
+ row_cells[2].text = tick if category == "Significant Focus" else ""
220
+ row_cells[3].text = tick if category == "Minor Mention" else ""
221
+ row_cells[4].text = tick if category == "Not Applicable" else ""
222
+
223
+ # Optionally set a fixed column width for consistency.
224
+ # (Adjust the Inches values as necessary.)
225
+ for col in table.columns:
226
+ for cell in col.cells:
227
+ cell.width = Inches(1.5)
228
+ return table
229
+
230
  def create_docx_from_data(extracted_data):
231
  doc = Document()
232
  for post_number, data in extracted_data.items():
 
243
  para = doc.add_paragraph()
244
  run = para.add_run(f"**{key}:** {value}")
245
  run.font.size = Pt(11)
246
+
247
+ # Instead of a plain-text table, use the structured table for frames if available.
248
  if "FramesMapping" in data:
249
  doc.add_paragraph("Frames:")
250
+ add_frames_table(doc, data["FramesMapping"])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
251
  else:
252
+ # Fallback: if no mapping, add the markdown text.
253
  value = data.get("Frames", "N/A")
254
  doc.add_paragraph(f"**Frames:** {value}")
255
  doc.add_paragraph("\n")
256
  return doc
257
 
258
+
259
  # -------------------------------------------------------------------
260
  # Streamlit App UI
261
  # -------------------------------------------------------------------