Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 0 additions & 9 deletions src/tppt/pptx/slide.py
Original file line number Diff line number Diff line change
Expand Up @@ -472,15 +472,6 @@ def _register(slide: Slide) -> Shape:
self._shape_registry.append(_register)
return self

def customize(self, callback: Callable[[PptxSlide], None]) -> Self:
"""Register a callback for direct access to the python-pptx Slide object."""

def _register(slide: Slide) -> None:
callback(slide.to_pptx())

self._shape_registry.append(_register)
return self

def tap(self, callback: Callable[[Slide], None]) -> Self:
"""Register a callback for direct slide access."""
self._shape_registry.append(callback)
Expand Down
28 changes: 3 additions & 25 deletions tests/test_slide.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

from typing import cast

from pptx.slide import Slide as PptxSlide
from pptx.shapes.autoshape import Shape as PptxShape
from pptx.enum.dml import MSO_FILL_TYPE
from pptx.enum.shapes import MSO_AUTO_SHAPE_TYPE
Expand Down Expand Up @@ -126,10 +125,10 @@ def test_slide_builder_tap_with_raw_pptx(output) -> None:

def add_raw_textbox(slide: tppt.pptx.slide.Slide) -> None:
pptx_slide = slide.to_pptx()
txBox = pptx_slide.shapes.add_textbox(
tx_box = pptx_slide.shapes.add_textbox(
Inches(1), Inches(1), Inches(3), Inches(1)
)
txBox.text_frame.text = "Added via tap()"
tx_box.text_frame.text = "Added via tap()"

presentation = (
tppt.Presentation.builder()
Expand All @@ -139,31 +138,10 @@ def add_raw_textbox(slide: tppt.pptx.slide.Slide) -> None:

slide = presentation.slides[0]
texts = [getattr(shape.to_pptx(), "text", "") for shape in slide.shapes]
assert any("Added via tap()" in t for t in texts)
assert any("Added via tap()" in text for text in texts)
presentation.save(output / "tap_raw_pptx_test.pptx")


def test_slide_builder_customize_with_raw_pptx(output) -> None:
"""Test that customize() invokes the callback with the raw python-pptx Slide."""
from pptx.util import Inches

def add_raw_textbox(slide: PptxSlide) -> None:
tx_box = slide.shapes.add_textbox(Inches(1), Inches(1), Inches(3), Inches(1))
tx_box.text_frame.text = "Added via customize()"

presentation = (
tppt.Presentation.builder()
.slide(lambda slide: slide.BlankLayout().builder().customize(add_raw_textbox))
.build()
)

texts = [
getattr(shape, "text", "") for shape in presentation.to_pptx().slides[0].shapes
]
assert any("Added via customize()" in text for text in texts)
presentation.save(output / "customize_raw_pptx_test.pptx")


def test_add_shape_with_styling_options(output) -> None:
"""Test add_shape() styling helpers for fill, line, and text."""
presentation = (
Expand Down
44 changes: 17 additions & 27 deletions tests/test_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
Dataclass,
PydanticModel,
)
from tppt.pptx.table.table import Column, ColumnCollection, Row, RowCollection
from tppt.pptx.table.table import ColumnCollection, RowCollection


def test_create_table_with_list_data(output: pathlib.Path) -> None:
Expand Down Expand Up @@ -288,27 +288,23 @@ def test_table_rows_columns_banding(output) -> None:
["1", "2", "3"],
["4", "5", "6"],
]
callback_called = False

def inspect_table(slide) -> None:
nonlocal callback_called
callback_called = True

pptx_table = cast(PptxGraphicFrame, slide.to_pptx().shapes[0]).table
table = tppt.pptx.table.Table(pptx_table)

def customize_table(
table: tppt.pptx.table.Table,
) -> tppt.pptx.table.Table:
# Test rows
rows = table.rows
assert isinstance(rows, RowCollection)
assert len(rows) == 3
row = rows[0]
assert isinstance(row, Row)
cells = row.cells
assert len(cells) == 3

# Test columns
columns = table.columns
assert isinstance(columns, ColumnCollection)
assert len(columns) == 3
col = columns[0]
assert isinstance(col, Column)

# Test banding properties with chaining
result = (
table.set_first_row(True)
.set_last_row(False)
Expand All @@ -325,11 +321,8 @@ def customize_table(
assert table.horz_banding is True
assert table.vert_banding is False

# Test iter_cells
cell_count = sum(1 for _ in table.iter_cells())
assert cell_count == 9 # 3x3

return table
assert cell_count == 9

presentation = (
tppt.Presentation.builder()
Expand All @@ -343,19 +336,13 @@ def customize_table(
width=(400, "pt"),
height=(200, "pt"),
)
.tap(
lambda slide: customize_table(
tppt.pptx.table.Table(slide.to_pptx().slides[0].shapes[0].table)
if hasattr(slide.to_pptx(), "slides")
else None
)
if False
else None
)
.tap(inspect_table)
)
.build()
)

assert callback_called

# Access table from the built presentation and test
pptx_pres = presentation.to_pptx()
pptx_table = cast(PptxGraphicFrame, pptx_pres.slides[0].shapes[0]).table
Expand All @@ -369,9 +356,12 @@ def customize_table(
assert isinstance(columns, ColumnCollection)
assert len(columns) == 3

table.set_first_row(True).set_horz_banding(True)
assert table.first_row is True
assert table.last_row is False
assert table.first_col is True
assert table.last_col is False
assert table.horz_banding is True
assert table.vert_banding is False

cell_count = sum(1 for _ in table.iter_cells())
assert cell_count == 9
Expand Down
Loading