Update app.py
Browse files
app.py
CHANGED
@@ -197,21 +197,27 @@ def merge_metadata_with_generated_data(generated_data, excel_metadata):
|
|
197 |
|
198 |
def add_frames_table(doc, mapping):
|
199 |
"""
|
200 |
-
Adds a well-
|
201 |
-
|
|
|
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"
|
|
|
|
|
206 |
hdr_cells = table.rows[0].cells
|
207 |
-
|
208 |
-
|
209 |
-
|
210 |
-
|
211 |
-
|
212 |
-
|
|
|
|
|
|
|
213 |
tick = "✓"
|
214 |
-
# Add one row per frame
|
215 |
for frame, category in mapping.items():
|
216 |
row_cells = table.add_row().cells
|
217 |
row_cells[0].text = frame
|
@@ -219,12 +225,18 @@ def add_frames_table(doc, mapping):
|
|
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
|
224 |
-
|
225 |
-
for
|
226 |
-
for cell in
|
227 |
-
cell.width =
|
228 |
return table
|
229 |
|
230 |
def create_docx_from_data(extracted_data):
|
@@ -241,17 +253,14 @@ def create_docx_from_data(extracted_data):
|
|
241 |
if key in ["Tone", "Hashtags"]:
|
242 |
value = ", ".join(value) if isinstance(value, list) else value
|
243 |
para = doc.add_paragraph()
|
244 |
-
run = para.add_run(f"
|
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 |
-
|
253 |
-
value = data.get("Frames", "N/A")
|
254 |
-
doc.add_paragraph(f"**Frames:** {value}")
|
255 |
doc.add_paragraph("\n")
|
256 |
return doc
|
257 |
|
|
|
197 |
|
198 |
def add_frames_table(doc, mapping):
|
199 |
"""
|
200 |
+
Adds a well-formatted table for the frames mapping into the given Document.
|
201 |
+
The table has 5 columns with headers: Frame, Major Focus, Significant Focus,
|
202 |
+
Minor Mention, and Not Applicable.
|
203 |
"""
|
204 |
# Create a table with 1 header row and 5 columns.
|
205 |
table = doc.add_table(rows=1, cols=5)
|
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:
|
214 |
+
paragraph.alignment = WD_ALIGN_PARAGRAPH.CENTER
|
215 |
+
for run in paragraph.runs:
|
216 |
+
run.font.bold = True
|
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 |
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):
|
|
|
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 |
|