Spaces:
Sleeping
Sleeping
File size: 8,037 Bytes
fdfe81a | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 | """Annotated PPTX export for the Farient brand-review API.
Opens the original uploaded deck (``source.pptx``) and overlays
reviewer annotations on each slide that has findings:
1. A small **summary card** in the top-right corner of every flagged
slide: severity-colored header + count and max-severity label.
2. A **detail box** below the summary listing each finding (title +
severity tag + truncated suggested fix).
3. For high-severity slides only, a **navy border rectangle** drawn
around the slide's content area (no fill, navy stroke).
Implementation choice: **annotation-shape overlay** (option B in the task
spec). Native PowerPoint review comments would require raw OOXML
manipulation — python-pptx 1.0.x does not expose a stable comment
authoring API yet. Overlay shapes work reliably in both Microsoft
PowerPoint and LibreOffice, and survive the round-trip through
``Presentation()`` validation.
Public API: ``annotate_pptx(source_path, audit) -> bytes``.
Raises ``FileNotFoundError`` if the source PPTX is missing — the route
handler maps that to 501 with a clear remediation message for old jobs
that pre-date source preservation.
"""
from __future__ import annotations
import io
from pathlib import Path
from pptx import Presentation
from pptx.dml.color import RGBColor
from pptx.enum.shapes import MSO_SHAPE
from pptx.util import Emu, Pt
from schemas.api import AuditExportJson, Finding, SlideEnvelope
# Farient navy
BRAND_NAVY = RGBColor(0x1F, 0x2D, 0x5F)
WHITE = RGBColor(0xFF, 0xFF, 0xFF)
DARK_GREY = RGBColor(0x1F, 0x29, 0x37)
SEVERITY_FILL = {
"high": RGBColor(0xDC, 0x26, 0x26), # red-600
"medium": RGBColor(0xD9, 0x77, 0x06), # amber-600
"low": RGBColor(0x16, 0xA3, 0x4A), # green-600
}
_SEV_RANK = {"high": 3, "medium": 2, "low": 1}
def _max_severity(findings: list[Finding]) -> str:
"""Return the highest severity present, or 'low' if list is empty."""
if not findings:
return "low"
return max(findings, key=lambda f: _SEV_RANK.get(f.severity, 0)).severity
def _truncate(text: str, n: int) -> str:
"""Trim *text* to *n* chars, appending an ellipsis if truncated."""
text = text.replace("\n", " ").replace("\r", " ").strip()
if len(text) <= n:
return text
return text[: n - 1].rstrip() + "…"
def _add_summary_card(
slide, slide_w: int, finding_count: int, max_sev: str
) -> None:
"""Add a small severity-colored summary card to the top-right corner."""
card_w = Emu(int(2.4 * 914400)) # 2.4 inches
card_h = Emu(int(0.55 * 914400)) # 0.55 inches
margin = Emu(int(0.15 * 914400))
left = slide_w - card_w - margin
top = margin
card = slide.shapes.add_shape(
MSO_SHAPE.ROUNDED_RECTANGLE, left, top, card_w, card_h
)
card.fill.solid()
card.fill.fore_color.rgb = SEVERITY_FILL.get(max_sev, BRAND_NAVY)
card.line.color.rgb = BRAND_NAVY
card.line.width = Pt(0.75)
tf = card.text_frame
tf.margin_left = Emu(int(0.08 * 914400))
tf.margin_right = Emu(int(0.08 * 914400))
tf.margin_top = Emu(int(0.04 * 914400))
tf.margin_bottom = Emu(int(0.04 * 914400))
tf.word_wrap = True
p = tf.paragraphs[0]
run = p.add_run()
run.text = "BRAND REVIEW"
run.font.bold = True
run.font.size = Pt(8)
run.font.color.rgb = WHITE
p2 = tf.add_paragraph()
r2 = p2.add_run()
plural = "" if finding_count == 1 else "s"
r2.text = f"{finding_count} finding{plural} · max: {max_sev.upper()}"
r2.font.size = Pt(9)
r2.font.bold = True
r2.font.color.rgb = WHITE
def _add_detail_box(
slide,
slide_w: int,
slide_h: int,
findings: list[Finding],
) -> None:
"""Add a detailed annotation list below the summary card."""
if not findings:
return
box_w = Emu(int(3.4 * 914400))
margin = Emu(int(0.15 * 914400))
summary_h = Emu(int(0.55 * 914400))
top = margin + summary_h + Emu(int(0.08 * 914400))
left = slide_w - box_w - margin
# Cap the box height to ~70% of the slide height so it never
# bleeds off the bottom on slides with lots of findings.
max_h = int(slide_h * 0.7)
box_h = Emu(min(int(0.45 * 914400) + len(findings) * int(0.55 * 914400), max_h))
box = slide.shapes.add_shape(
MSO_SHAPE.RECTANGLE, left, top, box_w, box_h
)
box.fill.solid()
box.fill.fore_color.rgb = WHITE
box.line.color.rgb = BRAND_NAVY
box.line.width = Pt(1.0)
tf = box.text_frame
tf.margin_left = Emu(int(0.10 * 914400))
tf.margin_right = Emu(int(0.10 * 914400))
tf.margin_top = Emu(int(0.08 * 914400))
tf.margin_bottom = Emu(int(0.08 * 914400))
tf.word_wrap = True
# Header
p_header = tf.paragraphs[0]
r_header = p_header.add_run()
r_header.text = "Reviewer notes"
r_header.font.bold = True
r_header.font.size = Pt(10)
r_header.font.color.rgb = BRAND_NAVY
# One paragraph per finding, capped at 6 to avoid text overflow.
for f in findings[:6]:
p = tf.add_paragraph()
r_sev = p.add_run()
r_sev.text = f"[{f.severity.upper()}] "
r_sev.font.bold = True
r_sev.font.size = Pt(8)
r_sev.font.color.rgb = SEVERITY_FILL.get(f.severity, DARK_GREY)
r_title = p.add_run()
r_title.text = _truncate(f.title, 70)
r_title.font.size = Pt(8)
r_title.font.bold = True
r_title.font.color.rgb = DARK_GREY
# Suggested fix on a new line, dimmer
p_fix = tf.add_paragraph()
r_fix = p_fix.add_run()
r_fix.text = _truncate(f"Fix: {f.suggested_fix}", 110)
r_fix.font.size = Pt(7)
r_fix.font.italic = True
r_fix.font.color.rgb = DARK_GREY
if len(findings) > 6:
p_more = tf.add_paragraph()
r_more = p_more.add_run()
r_more.text = f"… and {len(findings) - 6} more"
r_more.font.size = Pt(7)
r_more.font.italic = True
r_more.font.color.rgb = DARK_GREY
def _add_high_severity_border(
slide, slide_w: int, slide_h: int
) -> None:
"""Outline the slide with a 4-pt navy stroke (no fill) for high-sev decks."""
inset = Emu(int(0.05 * 914400))
border = slide.shapes.add_shape(
MSO_SHAPE.RECTANGLE,
inset,
inset,
slide_w - 2 * inset,
slide_h - 2 * inset,
)
# No fill; only the outline is visible.
border.fill.background()
border.line.color.rgb = BRAND_NAVY
border.line.width = Pt(4)
# Send to back so it doesn't obscure other annotations.
# python-pptx doesn't expose z-order directly; rely on the fact
# that our summary/detail boxes are added AFTER the border, so
# they paint on top.
def annotate_pptx(source_path: Path, audit: AuditExportJson) -> bytes:
"""Open *source_path* and return the annotated PPTX as bytes.
Findings are indexed by ``slide_number`` (1-indexed). Slides whose
number is out of range for the deck are silently skipped.
"""
if not source_path.exists():
raise FileNotFoundError(
f"Source PPTX not found at {source_path}; cannot annotate."
)
findings_by_slide: dict[int, list[Finding]] = {}
for env in audit.slides:
if env.findings:
findings_by_slide[env.slide_number] = list(env.findings)
prs = Presentation(str(source_path))
slide_w = prs.slide_width or Emu(int(13.333 * 914400))
slide_h = prs.slide_height or Emu(int(7.5 * 914400))
for idx, slide in enumerate(prs.slides, start=1):
findings = findings_by_slide.get(idx)
if not findings:
continue
max_sev = _max_severity(findings)
# Border first, so the annotation boxes paint on top.
if max_sev == "high":
_add_high_severity_border(slide, slide_w, slide_h)
_add_summary_card(slide, slide_w, len(findings), max_sev)
_add_detail_box(slide, slide_w, slide_h, findings)
out = io.BytesIO()
prs.save(out)
return out.getvalue()
__all__ = ["annotate_pptx"]
|