Skip to content

Commit d59bf07

Browse files
committed
feat: add example for creating presentations from Polars dataframe
1 parent d5c1f72 commit d59bf07

File tree

1 file changed

+85
-0
lines changed

1 file changed

+85
-0
lines changed

examples/dataflame_table.py

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
"""Example of creating tables from Polars dataframe."""
2+
3+
from pathlib import Path
4+
5+
import tppt
6+
from tppt.types import Color
7+
8+
# Flag to determine whether to use Polars
9+
USE_POLARS = True
10+
EXAMPLE_DIR = Path(__file__).parent
11+
12+
13+
def main():
14+
"""Run the sample."""
15+
import polars as pl
16+
17+
# Create dataframe with Polars
18+
df = pl.DataFrame(
19+
{
20+
"Product": ["Product A", "Product B", "Product C", "Product D"],
21+
"Price": ["$10.00", "$25.00", "$32.00", "$18.00"],
22+
"Stock": ["10 units", "5 units", "8 units", "12 units"],
23+
"Rating": ["★★★★☆", "★★★☆☆", "★★★★★", "★★★☆☆"],
24+
}
25+
)
26+
27+
# Convert dataframe to nested list
28+
columns = list(df.columns)
29+
rows = df.rows()
30+
table_data = [columns]
31+
32+
for row in rows:
33+
table_data.append([str(item) for item in row])
34+
35+
# Create presentation using builder pattern
36+
presentation = (
37+
tppt.Presentation.builder()
38+
.slide(
39+
tppt.SlideBuilder()
40+
.text(
41+
"Polars DataFrame Example",
42+
left=(50, "pt"),
43+
top=(50, "pt"),
44+
width=(500, "pt"),
45+
height=(50, "pt"),
46+
size=(44, "pt"),
47+
bold=True,
48+
color=Color("#0066CC"),
49+
)
50+
.text(
51+
"Easily convert Polars dataframes to presentations with tppt library",
52+
left=(50, "pt"),
53+
top=(120, "pt"),
54+
width=(500, "pt"),
55+
height=(30, "pt"),
56+
)
57+
.table(
58+
table_data,
59+
left=(50, "pt"),
60+
top=(100, "pt"),
61+
width=(500, "pt"),
62+
height=(200, "pt"),
63+
)
64+
.text(
65+
"Product list and inventory status created from a Polars dataframe",
66+
left=(50, "pt"),
67+
top=(320, "pt"),
68+
width=(500, "pt"),
69+
height=(50, "pt"),
70+
)
71+
)
72+
.build()
73+
)
74+
75+
# Save the presentation
76+
presentation.save(EXAMPLE_DIR / "dataflame_table.pptx")
77+
78+
print("Successfully created presentation from Polars dataframe!")
79+
80+
81+
if __name__ == "__main__":
82+
if USE_POLARS:
83+
main()
84+
else:
85+
print("Polars is not installed. Please install it to run this example.")

0 commit comments

Comments
 (0)