Skip to content

Commit f8607f9

Browse files
committed
feat: add SlidePlaceholder class and implement placeholders property in Slide class
1 parent a2aee12 commit f8607f9

File tree

3 files changed

+70
-0
lines changed

3 files changed

+70
-0
lines changed

src/tppt/_pptx/placeholder.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from typing import Self
2+
3+
from pptx.shapes.placeholder import SlidePlaceholder as PptxSlidePlaceholder
4+
5+
from tppt._pptx.presentation import PptxConvertible
6+
7+
8+
class SlidePlaceholder(PptxConvertible[PptxSlidePlaceholder]):
9+
def __init__(self, pptx_obj: PptxSlidePlaceholder) -> None:
10+
self._pptx = pptx_obj
11+
12+
def to_pptx(self) -> PptxSlidePlaceholder:
13+
return self._pptx
14+
15+
@classmethod
16+
def from_pptx(cls, pptx_obj: PptxSlidePlaceholder) -> Self:
17+
return cls(pptx_obj)

src/tppt/_pptx/slide.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
from pptx.slide import Slide as PptxSlide
77
from pptx.slide import SlideLayout as PptxSlideLayout
8+
9+
from tppt._pptx.placeholder import SlidePlaceholder
810
from tppt.exception import SlideLayoutIndexError
911
from tppt.types import FilePath
1012

@@ -34,10 +36,21 @@ def shapes(self) -> list[Shape]:
3436
@property
3537
def title(self) -> Title | None:
3638
"""Get slide title shape."""
39+
3740
if title := self._pptx.shapes.title:
3841
return Title(title)
3942
return None
4043

44+
@property
45+
def placeholders(self) -> list[SlidePlaceholder]:
46+
"""Get all placeholders in the slide."""
47+
return [
48+
SlidePlaceholder(
49+
placeholder, # type: ignore
50+
)
51+
for placeholder in self._pptx.placeholders
52+
]
53+
4154
def to_pptx(self) -> PptxSlide:
4255
"""Convert to pptx slide."""
4356
return cast(PptxSlide, self._pptx)

tests/test_slide.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
"""Tests for slide module."""
2+
3+
import pytest
4+
5+
import tppt
6+
from tppt._pptx.placeholder import SlidePlaceholder
7+
8+
9+
def test_slide_placeholders() -> None:
10+
"""Test getting placeholders from a slide."""
11+
# Create a presentation with a slide
12+
presentation = (
13+
tppt.Presentation.builder()
14+
.slide(
15+
tppt.SlideBuilder(0) # Use layout 0 which usually has placeholders
16+
)
17+
.build()
18+
)
19+
20+
# Get the slide from the presentation's pptx object
21+
pptx_presentation = presentation.to_pptx()
22+
if not pptx_presentation.slides:
23+
pytest.skip("No slides in presentation")
24+
25+
slide = tppt.Slide(pptx_presentation.slides[0])
26+
27+
# Test that we can get placeholders
28+
placeholders = slide.placeholders
29+
30+
# Check that placeholders is a list
31+
assert isinstance(placeholders, list)
32+
assert len(placeholders) != 0
33+
34+
# Verify each placeholder is a SlidePlaceholder instance
35+
for placeholder in placeholders:
36+
assert isinstance(placeholder, SlidePlaceholder)
37+
38+
# Check that we can convert it to pptx object
39+
pptx_placeholder = placeholder.to_pptx()
40+
assert pptx_placeholder is not None

0 commit comments

Comments
 (0)