Spaces:
Sleeping
Sleeping
Download services/export_pptx.py from DerXanity/farient-deckcheck: direct link, hf CLI and curl.
- Browser
- Download file 8.04 kB
-
https://huggingface.co/spaces/DerXanity/farient-deckcheck/resolve/main/services/export_pptx.py
- Command line
-
hf download hf://spaces/DerXanity/farient-deckcheck/services/export_pptx.py
-
curl -L -o export_pptx.py https://huggingface.co/spaces/DerXanity/farient-deckcheck/resolve/main/services/export_pptx.py
8.04 kB
| """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"] | |