1818from . import Shape
1919
2020# Define DataFrame type alias
21- DataFrame : TypeAlias = list [list [str ]] | PandasDataFrame | PolarsDataFrame
21+ DataFrame : TypeAlias = (
22+ list [list [str ]] | PandasDataFrame | PolarsDataFrame | PolarsLazyFrame
23+ )
2224
2325
2426class TableCellStyle (TypedDict ):
@@ -48,7 +50,7 @@ class TableData(TableProps):
4850
4951 type : Literal ["table" ]
5052
51- data : DataFrame
53+ data : list [ list [ str ]]
5254
5355
5456class Table (Shape [GraphicFrame ]):
@@ -68,63 +70,46 @@ def __init__(
6870 table = pptx_obj .table
6971
7072 # Apply first row as header if specified
71- if props .get ("first_row_header" ):
72- table .first_row = True
73+ if ( first_row := props .get ("first_row_header" )) is not None :
74+ table .first_row = first_row
7375
7476 # Apply table data if provided
75- if "data" in props :
76- data = props ["data" ]
77-
78- # Convert different DataFrame types to list of lists
79- if USE_POLARS and issubclass (
80- data .__class__ , (PolarsDataFrame , PolarsLazyFrame )
81- ):
82- # Convert pandas DataFrame to list of lists
83- data = cast (PandasDataFrame , data )
84- table_data = [data .columns .tolist ()] + data .values .tolist ()
85- elif USE_PANDAS and issubclass (data .__class__ , PandasDataFrame ): # type: ignore
86- # Convert polars DataFrame to list of lists
87- data = cast (PandasDataFrame , data )
88- table_data = [data .columns ] + data .to_numpy ().tolist ()
89- else :
90- table_data = data
91-
77+ if data := props .get ("data" ):
9278 # Now apply the data to the table
93- for i , row in enumerate (table_data ):
79+ for i , row in enumerate (data ):
9480 if i < len (table .rows ):
9581 for j , cell_text in enumerate (row ):
9682 if j < len (table .columns ):
9783 table .cell (i , j ).text = str (cell_text )
9884
9985 # Apply cell styles if provided
100- if "cell_styles" in props :
101- cell_styles = props ["cell_styles" ]
86+ if (cell_styles := props .get ("cell_styles" )) is not None :
10287 for i , row_styles in enumerate (cell_styles ):
10388 if i < len (table .rows ):
10489 for j , cell_style in enumerate (row_styles ):
10590 if j < len (table .columns ):
10691 cell = table .cell (i , j )
10792
108- if "text_align" in cell_style :
93+ if ( text_align := cell_style . get ( "text_align" )) is not None :
10994 align_map = {
11095 "left" : PP_ALIGN .LEFT ,
11196 "center" : PP_ALIGN .CENTER ,
11297 "right" : PP_ALIGN .RIGHT ,
11398 "justify" : PP_ALIGN .JUSTIFY ,
11499 }
115100 paragraph = cell .text_frame .paragraphs [0 ]
116- paragraph .alignment = align_map [
117- cell_style ["text_align" ]
118- ]
101+ paragraph .alignment = align_map [text_align ]
119102
120- if "vertical_align" in cell_style :
103+ if (
104+ vertical_align := cell_style .get ("vertical_align" )
105+ ) is not None :
121106 valign_map = {
122107 "top" : MSO_VERTICAL_ANCHOR .TOP ,
123108 "middle" : MSO_VERTICAL_ANCHOR .MIDDLE ,
124109 "bottom" : MSO_VERTICAL_ANCHOR .BOTTOM ,
125110 }
126111 cell .text_frame .vertical_anchor = valign_map [
127- cell_style [ " vertical_align" ]
112+ vertical_align
128113 ]
129114
130115 # Apply text formatting
@@ -161,3 +146,29 @@ def to_pptx(self) -> GraphicFrame:
161146 def from_pptx (cls , pptx_obj : GraphicFrame ) -> Self :
162147 """Create from pptx table frame."""
163148 return cls (pptx_obj )
149+
150+
151+ def dataframe2list (data : DataFrame ) -> list [list [str ]]:
152+ """Convert different DataFrame types to list of lists."""
153+ if USE_POLARS :
154+ if isinstance (data , PolarsLazyFrame ):
155+ # For LazyFrame, collect it first
156+ polars_df = data .collect ()
157+ columns = list (polars_df .columns )
158+ rows = polars_df .to_numpy ().tolist ()
159+ return [columns ] + rows
160+ elif isinstance (data , PolarsDataFrame ):
161+ polars_df = data
162+ columns = list (polars_df .columns )
163+ rows = polars_df .to_numpy ().tolist ()
164+ return [columns ] + rows
165+
166+ if USE_PANDAS and isinstance (data , PandasDataFrame ): # type: ignore
167+ # Convert pandas DataFrame to list of lists
168+ pandas_df = data
169+ columns = pandas_df .columns .tolist ()
170+ rows = pandas_df .values .tolist ()
171+ return [columns ] + rows
172+
173+ # Assume it's a list of lists
174+ return cast (list [list [str ]], data )
0 commit comments