Skip to content

Commit 53a80e3

Browse files
committed
feat: update quick start examples and enhance documentation with new concepts and layout customization
1 parent 12a5158 commit 53a80e3

File tree

14 files changed

+308
-82
lines changed

14 files changed

+308
-82
lines changed

docs/docs/codes/apply_sample.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import tppt
2+
3+
4+
def format_text(
5+
text_obj: tppt.Text, text: str, *, bold: bool = False, italic: bool = False
6+
) -> tppt.Text:
7+
paragraph = text_obj.text_frame.add_paragraph()
8+
run = paragraph.add_run()
9+
run.text = text
10+
font = run.font
11+
if bold:
12+
font.bold = True
13+
if italic:
14+
font.italic = True
15+
16+
return text_obj
17+
18+
19+
(
20+
tppt.Presentation.builder()
21+
.slide(
22+
lambda slide: slide.BlankLayout()
23+
.builder()
24+
.text(
25+
tppt.apply(format_text, "Hello, World!", bold=True, italic=True),
26+
left=(1, "in"),
27+
top=(1, "in"),
28+
width=(5, "in"),
29+
height=(2, "in"),
30+
)
31+
)
32+
.build()
33+
.save("simple.pptx")
34+
)
File renamed without changes.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import tppt
2+
from tppt.types import Inches
3+
4+
(
5+
tppt.Presentation.builder()
6+
.slide(
7+
lambda slide: slide.BlankLayout()
8+
.builder()
9+
.text(
10+
"Hello, World!",
11+
left=Inches(1),
12+
top=Inches(1),
13+
width=Inches(5),
14+
height=Inches(2),
15+
)
16+
)
17+
.build()
18+
.save("simple.pptx")
19+
)

docs/docs/home/concept.md

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# TPPTのコンセプト
2+
3+
## なぜTPPTなのか?
4+
5+
生成AIが普及している現在でも、レポートを pptx で提出してほしい、といった要望は多いものです。
6+
(私たちが取り残されているだけなのだろうか?)
7+
8+
tppt は python-pptx のラッパーであり、スライドマスターの型安全な利用などを提供します。
9+
10+
また、簡単に利用でき、直感的に記述されるためのアイデアを投入したものであり、
11+
皆さんの pptx の生成がより安全に、簡単になることを願っています。
12+
13+
## import の削減
14+
もう一度 Quick Start を見てみましょう。
15+
```python
16+
--8<-- "codes/quick_start.py"
17+
```
18+
19+
このコードは、TPPTのみをインポートしています。
20+
21+
極端に少ない import で、PowerPoint のプレゼンテーションを作成できます。
22+
23+
これは、 AI でコード生成をする際、常に tppt を import するだけで済むように配慮することで、
24+
コード生成の成功率を高めることができないか?という実験的なアプローチです。
25+
26+
これの実現には二つのテクニックが利用されています。
27+
28+
### Literal 型の積極的採用
29+
30+
例えば、スライドに追加する Shape の形は従来のように次のように書くことができます。
31+
32+
```python
33+
--8<-- "codes/quick_start_many_import.py"
34+
```
35+
36+
つまり、 `tppt.types.Inches(1)` と同じことを `(1, "in")` と書くことができます。
37+
38+
このように、肩による単位の指定を Literal で表現することで、従来必要であったインポートを減らしています。
39+
40+
これは、書き込みの話であり、プロパティを読み取るには必ず Inches 型に変換されている状態であるため、加減算や比較ができるようになっています。
41+
42+
43+
### 型の引き出し
44+
45+
いくつかの場所で `lambda slide: slide...` のように lambda 式を利用している箇所があります。
46+
これは遅延評価であり、要素を初期化した時点の方を取り出し、それに操作を加えることで要素を完成させます。
47+
48+
このアプローチの特徴は、型を import してから関数の引数に渡すのではなく、
49+
関数の中から型を取り出して利用するというものです。
50+
51+
これにより、コード生成としては import をさらに減らしています。
52+
53+
この方法には別のメリットがあります。
54+
引き出した型に対する操作を関数で切り出すことで、部品を共通化することが容易になります。
55+
56+
```python
57+
--8<-- "codes/apply_sample.py"
58+
```
59+
60+
あなたは `format_text` のような独自の修飾関数を作成し、それを簡単に再利用できます。
61+
62+
### 型安全性
63+
このツールはスライドマスターを元に、新規のスライド作成を行うユーザをメインターゲットとしています。
64+
65+
さて、あなたが私と同じであれば、 python-pptx のスライドマスターに型を与えることに苦労したはずです。
66+
67+
tppt は、 pydantic のような宣言的な型ヒントを利用してスライドマスターに型を与えることができます。
68+
69+
```python
70+
--8<-- "codes/custom_slide_master.py"
71+
```
72+
73+
型定義を自分で書くのは面倒だ?そんな方のために、ささやかなツールも用意しています。
74+
75+
```bash
76+
python -m tppt.tool.ppt2template $YOUR_TEMPLATE.pptx -o $OUTPUT_FILE.py
77+
```
78+
79+
スライドレイアウトのコンストラクタでプレースホルダーの設定をした後、
80+
テキストやピクチャ、表などのデータを記述することができます。
81+
82+
これらの操作は全て型安全です!

docs/docs/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ A modern Python library for creating beautiful PowerPoint presentations with cod
77
## Quick Start
88

99
```python
10-
--8<-- "codes/quick-start.py"
10+
--8<-- "codes/quick_start.py"
1111
```
1212

1313
## Features

docs/docs/usage/slide-layout.md

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,87 @@
1+
# スライドレイアウト
12

3+
TPPTでは、スライドのレイアウトを柔軟にカスタマイズすることができます。ここでは、基本的なレイアウトの設定方法と、よく使用されるパターンについて説明します。
4+
5+
## 基本的なレイアウト設定
6+
7+
### 1. スライドサイズの設定
8+
```python
9+
from tppt import Presentation
10+
11+
# 16:9のワイドスクリーン形式
12+
presentation = Presentation(slide_width=16, slide_height=9)
13+
14+
# 4:3の標準形式
15+
presentation = Presentation(slide_width=4, slide_height=3)
16+
```
17+
18+
### 2. マージンの設定
19+
```python
20+
# スライドの余白を設定
21+
slide = presentation.add_slide()
22+
slide.set_margins(left=1.0, right=1.0, top=0.5, bottom=0.5)
23+
```
24+
25+
## 一般的なレイアウトパターン
26+
27+
### 1. タイトルスライド
28+
```python
29+
slide = presentation.add_slide()
30+
slide.add_title("プレゼンテーションのタイトル")
31+
slide.add_subtitle("サブタイトル")
32+
```
33+
34+
### 2. 2カラムレイアウト
35+
```python
36+
slide = presentation.add_slide()
37+
left_column = slide.add_column(width=0.5)
38+
right_column = slide.add_column(width=0.5)
39+
40+
left_column.add_text("左側のコンテンツ")
41+
right_column.add_text("右側のコンテンツ")
42+
```
43+
44+
### 3. 画像とテキストの組み合わせ
45+
```python
46+
slide = presentation.add_slide()
47+
slide.add_image("path/to/image.png", width=0.6)
48+
slide.add_text("画像の説明文", position="bottom")
49+
```
50+
51+
## レイアウトのカスタマイズ
52+
53+
### 1. グリッドシステムの利用
54+
```python
55+
slide = presentation.add_slide()
56+
grid = slide.add_grid(rows=2, cols=2)
57+
grid[0, 0].add_text("セル1")
58+
grid[0, 1].add_text("セル2")
59+
grid[1, 0].add_text("セル3")
60+
grid[1, 1].add_text("セル4")
61+
```
62+
63+
### 2. フレックスボックスレイアウト
64+
```python
65+
slide = presentation.add_slide()
66+
flex = slide.add_flex_container()
67+
flex.add_item("アイテム1", flex_grow=1)
68+
flex.add_item("アイテム2", flex_grow=2)
69+
```
70+
71+
## ベストプラクティス
72+
73+
1. **一貫性の維持**
74+
- 同じ種類のスライドでは、常に同じレイアウトを使用する
75+
- マージンやパディングの値を統一する
76+
77+
2. **視覚的な階層構造**
78+
- 重要な情報は大きく、目立つように配置
79+
- 補足情報は小さく、控えめに配置
80+
81+
3. **余白の活用**
82+
- 適切な余白を確保し、見やすいレイアウトを心がける
83+
- 要素同士の間隔を適切に設定する
84+
85+
4. **レスポンシブ対応**
86+
- 異なる画面サイズでも見やすいレイアウトを考慮
87+
- コンテンツの自動調整機能を活用する

examples/formatted_text.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
def main():
1212
def formatted_text(text: tppt.pptx.shape.text.Text, /) -> tppt.pptx.shape.text.Text:
13-
run = text.text_frame().add_paragraph().add_run()
13+
run = text.text_frame.add_paragraph().add_run()
1414
run.text = "Hello, world!"
1515
font = run.font
1616
font.color.rgb = "#00FFFF"
@@ -27,7 +27,7 @@ def functional_text(
2727
italic: bool = False,
2828
color: tppt.types.Color | tppt.types.LiteralColor | None = None,
2929
) -> tppt.pptx.shape.text.Text:
30-
run = text_obj.text_frame().add_paragraph().add_run()
30+
run = text_obj.text_frame.add_paragraph().add_run()
3131
run.text = text
3232
font = run.font
3333

src/tppt/__init__.py

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,17 @@
33
import importlib.metadata
44
from typing import Callable, Concatenate, ParamSpec, TypeVar
55

6-
from tppt.pptx.presentation import Presentation
7-
from tppt.template.slide_layout import Placeholder, SlideLayout
8-
from tppt.template.slide_master import Layout, SlideMaster, slide_master
6+
from tppt.pptx.presentation import Presentation as Presentation
7+
from tppt.pptx.shape.picture import Picture as Picture
8+
from tppt.pptx.shape.text import Text as Text
9+
from tppt.pptx.slide import Slide as Slide
10+
from tppt.pptx.table import Table as Table
11+
from tppt.pptx.table import TableCellStyle as TableCellStyle
12+
from tppt.template.slide_layout import Placeholder as Placeholder
13+
from tppt.template.slide_layout import SlideLayout as SlideLayout
14+
from tppt.template.slide_master import Layout as Layout
15+
from tppt.template.slide_master import SlideMaster as SlideMaster
16+
from tppt.template.slide_master import slide_master as slide_master
917

1018
from . import types as types
1119

@@ -35,15 +43,3 @@ def wrapper(x: T) -> T:
3543
return func(x, *args, **kwargs)
3644

3745
return wrapper
38-
39-
40-
__all__ = [
41-
"Presentation",
42-
"SlideLayout",
43-
"Placeholder",
44-
"SlideLayout",
45-
"SlideMaster",
46-
"Layout",
47-
"slide_master",
48-
"apply",
49-
]

src/tppt/pptx/converter.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
from tppt.types._length import (
2424
CentiMeters,
2525
EnglishMetricUnits,
26-
Inchs,
26+
Inches,
2727
Length,
2828
LiteralLength,
2929
MilliMeters,
@@ -61,7 +61,7 @@ def to_pptx_length(length: Length | LiteralLength | None) -> PptxLength | None:
6161
length = to_length(length)
6262

6363
match length:
64-
case Inchs():
64+
case Inches():
6565
return PptxInches(length.value)
6666
case CentiMeters():
6767
return PptxCm(length.value)

src/tppt/pptx/shape/text.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ def __init__(self, pptx_obj: PptxShape, data: TextData | None = None, /) -> None
7070

7171
self._pptx = pptx_obj
7272

73+
@property
7374
def text_frame(self) -> TextFrame:
7475
return TextFrame(self._pptx.text_frame)
7576

0 commit comments

Comments
 (0)