forked from scanny/python-pptx
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadd_layout.py
More file actions
227 lines (148 loc) · 7.19 KB
/
add_layout.py
File metadata and controls
227 lines (148 loc) · 7.19 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
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
"""Gherkin step implementations for issue #19 SF3 — SlideLayouts.add_layout()."""
from __future__ import annotations
import io
from behave import given, then, when
from pptx import Presentation
# given ===================================================
@given("a default presentation")
def given_a_default_presentation(context):
context.prs = Presentation()
master = context.prs.slide_masters[0]
context.slide_layouts = master.slide_layouts
context.layout_count_before = len(context.slide_layouts)
context.slide_count_before = len(context.prs.slides)
# when ====================================================
@when("I call slide_layouts.add_layout()")
def when_I_call_add_layout_no_name(context):
context.new_layout = context.slide_layouts.add_layout()
@when('I call slide_layouts.add_layout("{name}")')
def when_I_call_add_layout_with_name(context, name):
context.new_layout = context.slide_layouts.add_layout(name)
@when("I save and reopen the presentation")
def when_I_save_and_reopen(context):
buf = io.BytesIO()
context.prs.save(buf)
buf.seek(0)
context.reopened = Presentation(buf)
@when("I add a slide based on the new layout")
def when_I_add_a_slide_based_on_the_new_layout(context):
context.prs.slides.add_slide(context.new_layout)
# then ====================================================
@then("the slide-master layout count increased by exactly 1")
def then_layout_count_increased_by_1(context):
assert len(context.slide_layouts) == context.layout_count_before + 1
@then("the new layout has a non-empty name")
def then_new_layout_has_non_empty_name(context):
assert context.new_layout.name not in (None, "")
@then('slide_layouts.get_by_name("{name}") is the new layout')
def then_get_by_name_returns_new_layout(context, name):
assert context.slide_layouts.get_by_name(name) is not None
assert context.slide_layouts.get_by_name(name).name == context.new_layout.name
@then('the reopened presentation has a layout named "{name}"')
def then_reopened_has_layout_named(context, name):
layouts = context.reopened.slide_masters[0].slide_layouts
assert layouts.get_by_name(name) is not None
@then("the slide count increased by exactly 1")
def then_slide_count_increased_by_1(context):
assert len(context.prs.slides) == context.slide_count_before + 1
# SF2 — save_as_potx ======================================
_TEMPLATE_CT = b"application/vnd.openxmlformats-officedocument.presentationml.template.main+xml"
_PRESENTATION_CT = (
b"application/vnd.openxmlformats-officedocument.presentationml.presentation.main+xml"
)
@when("I save the presentation as a potx")
def when_I_save_as_potx(context):
context.ct_before = context.prs.part.content_type
context.potx_buf = io.BytesIO()
context.prs.save_as_potx(context.potx_buf)
@then("the saved potx declares the template content-type")
def then_potx_declares_template_ct(context):
import zipfile
context.potx_buf.seek(0)
with zipfile.ZipFile(context.potx_buf) as z:
ct_xml = z.read("[Content_Types].xml")
assert _TEMPLATE_CT in ct_xml
assert _PRESENTATION_CT not in ct_xml
@then("the in-memory presentation content-type is unchanged")
def then_in_memory_ct_unchanged(context):
assert context.prs.part.content_type == context.ct_before
assert context.prs.part.content_type == _PRESENTATION_CT.decode("ascii")
@then("the saved potx reopens as a valid presentation")
def then_potx_reopens_valid(context):
context.potx_buf.seek(0)
reopened = Presentation(context.potx_buf)
assert len(reopened.slide_masters) >= 1
# SF5 — master shape authoring ============================
@when("I add a textbox to the slide master")
def when_I_add_textbox_to_master(context):
master = context.prs.slide_masters[0]
tb = master.shapes.add_textbox(0, 0, 914400, 457200)
tb.text_frame.text = "ACCEPTANCE MASTER TEXT"
@then("the reopened slide master has the master textbox text")
def then_reopened_master_has_textbox_text(context):
master = context.reopened.slide_masters[0]
texts = [s.text_frame.text for s in master.shapes if s.has_text_frame]
assert "ACCEPTANCE MASTER TEXT" in texts
# SF4 — copy_from =========================================
@when("I add a textbox to the new layout")
def when_I_add_textbox_to_new_layout(context):
context.new_layout.shapes.add_textbox(0, 0, 914400, 457200)
@when("I copy the new layout with copy_from")
def when_I_copy_layout_with_copy_from(context):
context.source_shape_count = len(list(context.new_layout.shapes))
context.copied_layout = context.slide_layouts.copy_from(context.new_layout)
@then("the copied layout has the same shape count as its source")
def then_copied_layout_same_shape_count(context):
assert len(list(context.copied_layout.shapes)) == context.source_shape_count
@then("the source layout is unchanged after copy_from")
def then_source_layout_unchanged(context):
assert len(list(context.new_layout.shapes)) == context.source_shape_count
# SF7 — cross-master / apply layout =======================
@when("I add a slide on the default layout")
def when_I_add_slide_on_default_layout(context):
context.sf7_slide = context.prs.slides.add_slide(context.prs.slide_layouts[0])
@when("I apply the new layout to that slide")
def when_I_apply_new_layout_to_slide(context):
context.sf7_slide.slide_layout = context.new_layout
@then('the reopened slide uses the layout named "{name}"')
def then_reopened_slide_uses_layout(context, name):
assert context.reopened.slides[0].slide_layout.name == name
@then("the reopened slide still resolves its slide master")
def then_reopened_slide_resolves_master(context):
assert context.reopened.slides[0].slide_layout.slide_master is not None
# SF8 — chart into placeholder ============================
@when("I add a layout with a chart placeholder")
def when_I_add_layout_with_chart_placeholder(context):
from pptx.enum.shapes import PP_PLACEHOLDER
master = context.prs.slide_masters[0]
context.chart_layout = master.slide_layouts.add_layout("Chart PH Layout")
context.chart_layout.placeholders.add(
10,
PP_PLACEHOLDER.CHART,
left=914400,
top=914400,
width=4572000,
height=2743200,
)
@when("I add a slide on that chart-placeholder layout")
def when_I_add_slide_on_chart_layout(context):
context.chart_slide = context.prs.slides.add_slide(context.chart_layout)
@when("I insert a chart into the slide's chart placeholder")
def when_I_insert_chart_into_placeholder(context):
from pptx.chart.data import CategoryChartData
from pptx.enum.chart import XL_CHART_TYPE
from pptx.enum.shapes import PP_PLACEHOLDER
chart_ph = next(
p
for p in context.chart_slide.placeholders
if p.placeholder_format.type == PP_PLACEHOLDER.CHART
)
chart_data = CategoryChartData()
chart_data.categories = ["A", "B", "C"]
chart_data.add_series("S1", (1.0, 2.0, 3.0))
chart_ph.insert_chart(XL_CHART_TYPE.COLUMN_CLUSTERED, chart_data)
@then("the reopened slide has exactly one chart")
def then_reopened_slide_has_one_chart(context):
slide = context.reopened.slides[0]
charts = [s for s in slide.shapes if s.has_chart]
assert len(charts) == 1