-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathslide_builder.py
More file actions
174 lines (131 loc) · 5.57 KB
/
slide_builder.py
File metadata and controls
174 lines (131 loc) · 5.57 KB
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
"""
OpenDeck Slide Builder
Assembles a complete PPTX presentation from structured data + theme.
Handles gradient backgrounds, slide creation, and speaker notes.
"""
from lxml import etree
from pptx import Presentation
from pptx.util import Inches, Pt, Emu
from pptx.oxml.ns import qn
from themes import Theme, GradientStop
from layouts import render_slide, SLIDE_WIDTH, SLIDE_HEIGHT
# ─── GRADIENT BACKGROUND ──────────────────────────────────────────────────────
def _make_gradient_element(stops: list[GradientStop], angle: int):
"""
Build an XML element for a gradient fill.
Uses raw lxml because python-pptx doesn't support gradient backgrounds natively.
"""
nsmap = {"a": "http://schemas.openxmlformats.org/drawingml/2006/main"}
grad_fill = etree.SubElement(
etree.Element("dummy"), qn("a:gradFill")
)
grad_fill.set("rotWithShape", "1")
gs_lst = etree.SubElement(grad_fill, qn("a:gsLst"))
for stop in stops:
gs = etree.SubElement(gs_lst, qn("a:gs"))
gs.set("pos", str(stop.position))
srgb = etree.SubElement(gs, qn("a:srgbClr"))
srgb.set("val", stop.color)
lin = etree.SubElement(grad_fill, qn("a:lin"))
lin.set("ang", str(angle))
lin.set("scaled", "1")
return grad_fill
def _apply_gradient_background(slide, stops: list[GradientStop], angle: int):
"""Apply a gradient fill to a slide's background."""
if not stops:
return
bg = slide.background
bg_elem = bg._element
# Ensure <p:bgPr> exists
bg_pr = bg_elem.find(qn("p:bgPr"))
if bg_pr is None:
bg_pr = etree.SubElement(bg_elem, qn("p:bgPr"))
# Remove any existing fill
for child in list(bg_pr):
tag = child.tag
if "Fill" in tag or "fill" in tag:
bg_pr.remove(child)
# Build the gradient fill
grad_fill = _make_gradient_element(stops, angle)
# Insert at the beginning of bgPr
bg_pr.insert(0, grad_fill)
# Ensure effectLst exists (required by PowerPoint)
effect_lst = bg_pr.find(qn("a:effectLst"))
if effect_lst is None:
etree.SubElement(bg_pr, qn("a:effectLst"))
def _apply_solid_background(slide, color_hex: str):
"""Apply a solid color background to a slide."""
bg = slide.background
fill = bg.fill
fill.solid()
from pptx.dml.color import RGBColor
fill.fore_color.rgb = RGBColor.from_string(color_hex)
# ─── SPEAKER NOTES ────────────────────────────────────────────────────────────
def _add_speaker_notes(slide, notes_text: str):
"""Add speaker notes to a slide."""
if not notes_text:
return
notes_slide = slide.notes_slide
notes_tf = notes_slide.notes_text_frame
notes_tf.text = notes_text
# ─── SLIDE NUMBER ─────────────────────────────────────────────────────────────
def _add_slide_number(slide, current: int, total: int, theme: Theme):
"""Add a subtle slide number in the bottom-right corner."""
from layouts import _add_text_box
from pptx.enum.text import PP_ALIGN
_add_text_box(
slide,
left=Inches(11.5),
top=Inches(7.0),
width=Inches(1.5),
height=Inches(0.4),
text=f"{current} / {total}",
font_name=theme.body_font,
font_size=Pt(10),
font_color=theme.subtitle_color,
alignment=PP_ALIGN.RIGHT,
)
# ─── MAIN BUILDER ─────────────────────────────────────────────────────────────
def build_presentation(data: dict, theme: Theme, output_path: str) -> str:
"""
Build a complete PPTX presentation.
Args:
data: Structured presentation data from LLM.
theme: Theme object to apply.
output_path: File path for the output .pptx file.
Returns:
The output file path.
"""
prs = Presentation()
# Set slide dimensions to 16:9 widescreen
prs.slide_width = SLIDE_WIDTH
prs.slide_height = SLIDE_HEIGHT
slides_data = data.get("slides", [])
total_slides = len(slides_data)
for i, slide_data in enumerate(slides_data):
slide_type = slide_data.get("type", "content")
# Use blank layout (index 6) for full control
slide_layout = prs.slide_layouts[6]
slide = prs.slides.add_slide(slide_layout)
# Choose gradient based on slide type
if slide_type == "title":
gradient = theme.title_bg_gradient or theme.bg_gradient
elif slide_type in ("section", "closing"):
gradient = theme.section_bg_gradient or theme.bg_gradient
else:
gradient = theme.bg_gradient
# Apply gradient background
if gradient:
_apply_gradient_background(slide, gradient, theme.bg_gradient_angle)
# Render the slide content using the appropriate layout
render_slide(slide, slide_data, theme)
# Add speaker notes
notes = slide_data.get("notes", "")
if notes:
_add_speaker_notes(slide, notes)
# Add slide number (skip title slide)
if slide_type != "title" and slide_type != "closing":
_add_slide_number(slide, i + 1, total_slides, theme)
# Save the presentation
prs.save(output_path)
return output_path