ahm14 commited on
Commit
dc1177c
·
verified ·
1 Parent(s): da6e954

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -11
app.py CHANGED
@@ -195,6 +195,8 @@ def merge_metadata_with_generated_data(generated_data, excel_metadata):
195
  generated_data[post_number] = post_data
196
  return generated_data
197
 
 
 
198
  def add_frames_table(doc, mapping):
199
  """
200
  Adds a well-formatted table for the frames mapping into the given Document.
@@ -206,8 +208,8 @@ def add_frames_table(doc, mapping):
206
  table.style = "Table Grid"
207
 
208
  # Set header cells with bold, centered text.
209
- hdr_cells = table.rows[0].cells
210
  headers = ["Frame", "Major Focus", "Significant Focus", "Minor Mention", "Not Applicable"]
 
211
  for idx, header in enumerate(headers):
212
  hdr_cells[idx].text = header
213
  for paragraph in hdr_cells[idx].paragraphs:
@@ -217,7 +219,7 @@ def add_frames_table(doc, mapping):
217
  run.font.size = Pt(11)
218
 
219
  tick = "✓"
220
- # Add one row per frame.
221
  for frame, category in mapping.items():
222
  row_cells = table.add_row().cells
223
  row_cells[0].text = frame
@@ -225,18 +227,21 @@ def add_frames_table(doc, mapping):
225
  row_cells[2].text = tick if category == "Significant Focus" else ""
226
  row_cells[3].text = tick if category == "Minor Mention" else ""
227
  row_cells[4].text = tick if category == "Not Applicable" else ""
228
- # Format cells: first cell left-aligned, rest centered.
229
  for idx, cell in enumerate(row_cells):
230
  for paragraph in cell.paragraphs:
231
- paragraph.alignment = WD_ALIGN_PARAGRAPH.CENTER if idx > 0 else WD_ALIGN_PARAGRAPH.LEFT
 
 
 
232
  for run in paragraph.runs:
233
  run.font.size = Pt(11)
234
 
235
  # Optionally, set fixed column widths.
236
- widths = [Inches(2), Inches(1), Inches(1), Inches(1), Inches(1)]
237
  for row in table.rows:
238
  for idx, cell in enumerate(row.cells):
239
- cell.width = widths[idx]
240
  return table
241
 
242
  def create_docx_from_data(extracted_data):
@@ -252,19 +257,21 @@ def create_docx_from_data(extracted_data):
252
  value = data.get(key, "N/A")
253
  if key in ["Tone", "Hashtags"]:
254
  value = ", ".join(value) if isinstance(value, list) else value
255
- para = doc.add_paragraph()
256
- run = para.add_run(f"{key}: {value}")
257
- run.font.size = Pt(11)
258
- # Use a proper table if a FramesMapping is available.
259
  if "FramesMapping" in data:
260
  doc.add_paragraph("Frames:")
261
  add_frames_table(doc, data["FramesMapping"])
262
  else:
 
263
  doc.add_paragraph(f"Frames: {data.get('Frames', 'N/A')}")
264
- doc.add_paragraph("\n")
265
  return doc
266
 
267
 
 
268
  # -------------------------------------------------------------------
269
  # Streamlit App UI
270
  # -------------------------------------------------------------------
 
195
  generated_data[post_number] = post_data
196
  return generated_data
197
 
198
+
199
+
200
  def add_frames_table(doc, mapping):
201
  """
202
  Adds a well-formatted table for the frames mapping into the given Document.
 
208
  table.style = "Table Grid"
209
 
210
  # Set header cells with bold, centered text.
 
211
  headers = ["Frame", "Major Focus", "Significant Focus", "Minor Mention", "Not Applicable"]
212
+ hdr_cells = table.rows[0].cells
213
  for idx, header in enumerate(headers):
214
  hdr_cells[idx].text = header
215
  for paragraph in hdr_cells[idx].paragraphs:
 
219
  run.font.size = Pt(11)
220
 
221
  tick = "✓"
222
+ # Add a row for each frame.
223
  for frame, category in mapping.items():
224
  row_cells = table.add_row().cells
225
  row_cells[0].text = frame
 
227
  row_cells[2].text = tick if category == "Significant Focus" else ""
228
  row_cells[3].text = tick if category == "Minor Mention" else ""
229
  row_cells[4].text = tick if category == "Not Applicable" else ""
230
+ # Center-align cells (except the first one, which is left-aligned).
231
  for idx, cell in enumerate(row_cells):
232
  for paragraph in cell.paragraphs:
233
+ if idx == 0:
234
+ paragraph.alignment = WD_ALIGN_PARAGRAPH.LEFT
235
+ else:
236
+ paragraph.alignment = WD_ALIGN_PARAGRAPH.CENTER
237
  for run in paragraph.runs:
238
  run.font.size = Pt(11)
239
 
240
  # Optionally, set fixed column widths.
241
+ col_widths = [Inches(2), Inches(1), Inches(1), Inches(1), Inches(1)]
242
  for row in table.rows:
243
  for idx, cell in enumerate(row.cells):
244
+ cell.width = col_widths[idx]
245
  return table
246
 
247
  def create_docx_from_data(extracted_data):
 
257
  value = data.get(key, "N/A")
258
  if key in ["Tone", "Hashtags"]:
259
  value = ", ".join(value) if isinstance(value, list) else value
260
+ para = doc.add_paragraph(f"{key}: {value}")
261
+ para.style.font.size = Pt(11)
262
+
263
+ # If a FramesMapping exists, add a proper table.
264
  if "FramesMapping" in data:
265
  doc.add_paragraph("Frames:")
266
  add_frames_table(doc, data["FramesMapping"])
267
  else:
268
+ # Fallback: add plain text.
269
  doc.add_paragraph(f"Frames: {data.get('Frames', 'N/A')}")
270
+ doc.add_paragraph("")
271
  return doc
272
 
273
 
274
+
275
  # -------------------------------------------------------------------
276
  # Streamlit App UI
277
  # -------------------------------------------------------------------