|
| 1 | +"""Gherkin step implementations for issue #19 SF3 — SlideLayouts.add_layout().""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import io |
| 6 | + |
| 7 | +from behave import given, then, when |
| 8 | + |
| 9 | +from pptx import Presentation |
| 10 | + |
| 11 | +# given =================================================== |
| 12 | + |
| 13 | + |
| 14 | +@given("a default presentation") |
| 15 | +def given_a_default_presentation(context): |
| 16 | + context.prs = Presentation() |
| 17 | + master = context.prs.slide_masters[0] |
| 18 | + context.slide_layouts = master.slide_layouts |
| 19 | + context.layout_count_before = len(context.slide_layouts) |
| 20 | + context.slide_count_before = len(context.prs.slides) |
| 21 | + |
| 22 | + |
| 23 | +# when ==================================================== |
| 24 | + |
| 25 | + |
| 26 | +@when("I call slide_layouts.add_layout()") |
| 27 | +def when_I_call_add_layout_no_name(context): |
| 28 | + context.new_layout = context.slide_layouts.add_layout() |
| 29 | + |
| 30 | + |
| 31 | +@when('I call slide_layouts.add_layout("{name}")') |
| 32 | +def when_I_call_add_layout_with_name(context, name): |
| 33 | + context.new_layout = context.slide_layouts.add_layout(name) |
| 34 | + |
| 35 | + |
| 36 | +@when("I save and reopen the presentation") |
| 37 | +def when_I_save_and_reopen(context): |
| 38 | + buf = io.BytesIO() |
| 39 | + context.prs.save(buf) |
| 40 | + buf.seek(0) |
| 41 | + context.reopened = Presentation(buf) |
| 42 | + |
| 43 | + |
| 44 | +@when("I add a slide based on the new layout") |
| 45 | +def when_I_add_a_slide_based_on_the_new_layout(context): |
| 46 | + context.prs.slides.add_slide(context.new_layout) |
| 47 | + |
| 48 | + |
| 49 | +# then ==================================================== |
| 50 | + |
| 51 | + |
| 52 | +@then("the slide-master layout count increased by exactly 1") |
| 53 | +def then_layout_count_increased_by_1(context): |
| 54 | + assert len(context.slide_layouts) == context.layout_count_before + 1 |
| 55 | + |
| 56 | + |
| 57 | +@then("the new layout has a non-empty name") |
| 58 | +def then_new_layout_has_non_empty_name(context): |
| 59 | + assert context.new_layout.name not in (None, "") |
| 60 | + |
| 61 | + |
| 62 | +@then('slide_layouts.get_by_name("{name}") is the new layout') |
| 63 | +def then_get_by_name_returns_new_layout(context, name): |
| 64 | + assert context.slide_layouts.get_by_name(name) is not None |
| 65 | + assert context.slide_layouts.get_by_name(name).name == context.new_layout.name |
| 66 | + |
| 67 | + |
| 68 | +@then('the reopened presentation has a layout named "{name}"') |
| 69 | +def then_reopened_has_layout_named(context, name): |
| 70 | + layouts = context.reopened.slide_masters[0].slide_layouts |
| 71 | + assert layouts.get_by_name(name) is not None |
| 72 | + |
| 73 | + |
| 74 | +@then("the slide count increased by exactly 1") |
| 75 | +def then_slide_count_increased_by_1(context): |
| 76 | + assert len(context.prs.slides) == context.slide_count_before + 1 |
| 77 | + |
| 78 | + |
| 79 | +# SF2 — save_as_potx ====================================== |
| 80 | + |
| 81 | +_TEMPLATE_CT = b"application/vnd.openxmlformats-officedocument.presentationml.template.main+xml" |
| 82 | +_PRESENTATION_CT = ( |
| 83 | + b"application/vnd.openxmlformats-officedocument.presentationml.presentation.main+xml" |
| 84 | +) |
| 85 | + |
| 86 | + |
| 87 | +@when("I save the presentation as a potx") |
| 88 | +def when_I_save_as_potx(context): |
| 89 | + context.ct_before = context.prs.part.content_type |
| 90 | + context.potx_buf = io.BytesIO() |
| 91 | + context.prs.save_as_potx(context.potx_buf) |
| 92 | + |
| 93 | + |
| 94 | +@then("the saved potx declares the template content-type") |
| 95 | +def then_potx_declares_template_ct(context): |
| 96 | + import zipfile |
| 97 | + |
| 98 | + context.potx_buf.seek(0) |
| 99 | + with zipfile.ZipFile(context.potx_buf) as z: |
| 100 | + ct_xml = z.read("[Content_Types].xml") |
| 101 | + assert _TEMPLATE_CT in ct_xml |
| 102 | + assert _PRESENTATION_CT not in ct_xml |
| 103 | + |
| 104 | + |
| 105 | +@then("the in-memory presentation content-type is unchanged") |
| 106 | +def then_in_memory_ct_unchanged(context): |
| 107 | + assert context.prs.part.content_type == context.ct_before |
| 108 | + assert context.prs.part.content_type == _PRESENTATION_CT.decode("ascii") |
| 109 | + |
| 110 | + |
| 111 | +@then("the saved potx reopens as a valid presentation") |
| 112 | +def then_potx_reopens_valid(context): |
| 113 | + context.potx_buf.seek(0) |
| 114 | + reopened = Presentation(context.potx_buf) |
| 115 | + assert len(reopened.slide_masters) >= 1 |
| 116 | + |
| 117 | + |
| 118 | +# SF5 — master shape authoring ============================ |
| 119 | + |
| 120 | + |
| 121 | +@when("I add a textbox to the slide master") |
| 122 | +def when_I_add_textbox_to_master(context): |
| 123 | + master = context.prs.slide_masters[0] |
| 124 | + tb = master.shapes.add_textbox(0, 0, 914400, 457200) |
| 125 | + tb.text_frame.text = "ACCEPTANCE MASTER TEXT" |
| 126 | + |
| 127 | + |
| 128 | +@then("the reopened slide master has the master textbox text") |
| 129 | +def then_reopened_master_has_textbox_text(context): |
| 130 | + master = context.reopened.slide_masters[0] |
| 131 | + texts = [s.text_frame.text for s in master.shapes if s.has_text_frame] |
| 132 | + assert "ACCEPTANCE MASTER TEXT" in texts |
| 133 | + |
| 134 | + |
| 135 | +# SF4 — copy_from ========================================= |
| 136 | + |
| 137 | + |
| 138 | +@when("I add a textbox to the new layout") |
| 139 | +def when_I_add_textbox_to_new_layout(context): |
| 140 | + context.new_layout.shapes.add_textbox(0, 0, 914400, 457200) |
| 141 | + |
| 142 | + |
| 143 | +@when("I copy the new layout with copy_from") |
| 144 | +def when_I_copy_layout_with_copy_from(context): |
| 145 | + context.source_shape_count = len(list(context.new_layout.shapes)) |
| 146 | + context.copied_layout = context.slide_layouts.copy_from(context.new_layout) |
| 147 | + |
| 148 | + |
| 149 | +@then("the copied layout has the same shape count as its source") |
| 150 | +def then_copied_layout_same_shape_count(context): |
| 151 | + assert len(list(context.copied_layout.shapes)) == context.source_shape_count |
| 152 | + |
| 153 | + |
| 154 | +@then("the source layout is unchanged after copy_from") |
| 155 | +def then_source_layout_unchanged(context): |
| 156 | + assert len(list(context.new_layout.shapes)) == context.source_shape_count |
| 157 | + |
| 158 | + |
| 159 | +# SF7 — cross-master / apply layout ======================= |
| 160 | + |
| 161 | + |
| 162 | +@when("I add a slide on the default layout") |
| 163 | +def when_I_add_slide_on_default_layout(context): |
| 164 | + context.sf7_slide = context.prs.slides.add_slide(context.prs.slide_layouts[0]) |
| 165 | + |
| 166 | + |
| 167 | +@when("I apply the new layout to that slide") |
| 168 | +def when_I_apply_new_layout_to_slide(context): |
| 169 | + context.sf7_slide.slide_layout = context.new_layout |
| 170 | + |
| 171 | + |
| 172 | +@then('the reopened slide uses the layout named "{name}"') |
| 173 | +def then_reopened_slide_uses_layout(context, name): |
| 174 | + assert context.reopened.slides[0].slide_layout.name == name |
| 175 | + |
| 176 | + |
| 177 | +@then("the reopened slide still resolves its slide master") |
| 178 | +def then_reopened_slide_resolves_master(context): |
| 179 | + assert context.reopened.slides[0].slide_layout.slide_master is not None |
| 180 | + |
| 181 | + |
| 182 | +# SF8 — chart into placeholder ============================ |
| 183 | + |
| 184 | + |
| 185 | +@when("I add a layout with a chart placeholder") |
| 186 | +def when_I_add_layout_with_chart_placeholder(context): |
| 187 | + from pptx.enum.shapes import PP_PLACEHOLDER |
| 188 | + |
| 189 | + master = context.prs.slide_masters[0] |
| 190 | + context.chart_layout = master.slide_layouts.add_layout("Chart PH Layout") |
| 191 | + context.chart_layout.placeholders.add( |
| 192 | + 10, |
| 193 | + PP_PLACEHOLDER.CHART, |
| 194 | + left=914400, |
| 195 | + top=914400, |
| 196 | + width=4572000, |
| 197 | + height=2743200, |
| 198 | + ) |
| 199 | + |
| 200 | + |
| 201 | +@when("I add a slide on that chart-placeholder layout") |
| 202 | +def when_I_add_slide_on_chart_layout(context): |
| 203 | + context.chart_slide = context.prs.slides.add_slide(context.chart_layout) |
| 204 | + |
| 205 | + |
| 206 | +@when("I insert a chart into the slide's chart placeholder") |
| 207 | +def when_I_insert_chart_into_placeholder(context): |
| 208 | + from pptx.chart.data import CategoryChartData |
| 209 | + from pptx.enum.chart import XL_CHART_TYPE |
| 210 | + from pptx.enum.shapes import PP_PLACEHOLDER |
| 211 | + |
| 212 | + chart_ph = next( |
| 213 | + p |
| 214 | + for p in context.chart_slide.placeholders |
| 215 | + if p.placeholder_format.type == PP_PLACEHOLDER.CHART |
| 216 | + ) |
| 217 | + chart_data = CategoryChartData() |
| 218 | + chart_data.categories = ["A", "B", "C"] |
| 219 | + chart_data.add_series("S1", (1.0, 2.0, 3.0)) |
| 220 | + chart_ph.insert_chart(XL_CHART_TYPE.COLUMN_CLUSTERED, chart_data) |
| 221 | + |
| 222 | + |
| 223 | +@then("the reopened slide has exactly one chart") |
| 224 | +def then_reopened_slide_has_one_chart(context): |
| 225 | + slide = context.reopened.slides[0] |
| 226 | + charts = [s for s in slide.shapes if s.has_chart] |
| 227 | + assert len(charts) == 1 |
0 commit comments