From f1e323252b55f563df435fa46a5ac85ac527c328 Mon Sep 17 00:00:00 2001 From: Marshall Crumiller Date: Sat, 20 Jul 2024 15:17:03 -0400 Subject: [PATCH 1/4] Write format at column level --- py-polars/polars/dataframe/frame.py | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/py-polars/polars/dataframe/frame.py b/py-polars/polars/dataframe/frame.py index ae1a9260bf65..9a48e150f75d 100644 --- a/py-polars/polars/dataframe/frame.py +++ b/py-polars/polars/dataframe/frame.py @@ -3203,11 +3203,7 @@ def write_excel( column_start = [table_start[0] + int(include_header), table_start[1]] for c in df.columns: if c in self.columns: - ws.write_column( - *column_start, - data=df[c].to_list(), - cell_format=column_formats.get(c), - ) + ws.write_column(*column_start, data=df[c].to_list()) column_start[1] += 1 # apply conditional formats @@ -3234,6 +3230,7 @@ def write_excel( for column in df.columns: col_idx, options = table_start[1] + df.get_column_index(column), {} + fmt = column_formats.get(column) if column in hidden_columns: options = {"hidden": True} if column in column_widths: # type: ignore[operator] @@ -3241,11 +3238,11 @@ def write_excel( col_idx, col_idx, column_widths[column], # type: ignore[index] - None, + fmt, options, ) elif options: - ws.set_column(col_idx, col_idx, None, None, options) + ws.set_column(col_idx, col_idx, None, fmt, options) # finally, inject any sparklines into the table for column, params in (sparklines or {}).items(): From 788e80eb93cd5015112cefa512ddc455d6ae0261 Mon Sep 17 00:00:00 2001 From: Marshall Date: Sat, 20 Jul 2024 15:52:10 -0400 Subject: [PATCH 2/4] Ensure fmt applied Co-authored-by: Alexander Beedie --- py-polars/polars/dataframe/frame.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/py-polars/polars/dataframe/frame.py b/py-polars/polars/dataframe/frame.py index 9a48e150f75d..31fb559a7561 100644 --- a/py-polars/polars/dataframe/frame.py +++ b/py-polars/polars/dataframe/frame.py @@ -3241,7 +3241,7 @@ def write_excel( fmt, options, ) - elif options: + elif options or fmt: ws.set_column(col_idx, col_idx, None, fmt, options) # finally, inject any sparklines into the table From 522610ad7e1a5c925aa3167164d287b43ff3da29 Mon Sep 17 00:00:00 2001 From: Marshall Crumiller Date: Sun, 21 Jul 2024 14:21:25 -0400 Subject: [PATCH 3/4] Move data write to add_table --- py-polars/polars/dataframe/frame.py | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/py-polars/polars/dataframe/frame.py b/py-polars/polars/dataframe/frame.py index 31fb559a7561..d123353f8466 100644 --- a/py-polars/polars/dataframe/frame.py +++ b/py-polars/polars/dataframe/frame.py @@ -3188,6 +3188,7 @@ def write_excel( *table_start, *table_finish, { + "data": df.rows(), "style": table_style, "columns": table_columns, "header_row": include_header, @@ -3198,14 +3199,6 @@ def write_excel( }, ) - # write data into the table range, column-wise - if not is_empty: - column_start = [table_start[0] + int(include_header), table_start[1]] - for c in df.columns: - if c in self.columns: - ws.write_column(*column_start, data=df[c].to_list()) - column_start[1] += 1 - # apply conditional formats if conditional_formats: _xl_apply_conditional_formats( From 038c66dc923b38017e8a1cb91d069de5bd80d3ae Mon Sep 17 00:00:00 2001 From: Marshall Crumiller Date: Tue, 23 Jul 2024 18:17:29 -0400 Subject: [PATCH 4/4] Don't set column format --- py-polars/polars/dataframe/frame.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/py-polars/polars/dataframe/frame.py b/py-polars/polars/dataframe/frame.py index d123353f8466..3a47af443eb3 100644 --- a/py-polars/polars/dataframe/frame.py +++ b/py-polars/polars/dataframe/frame.py @@ -3223,7 +3223,6 @@ def write_excel( for column in df.columns: col_idx, options = table_start[1] + df.get_column_index(column), {} - fmt = column_formats.get(column) if column in hidden_columns: options = {"hidden": True} if column in column_widths: # type: ignore[operator] @@ -3231,11 +3230,9 @@ def write_excel( col_idx, col_idx, column_widths[column], # type: ignore[index] - fmt, + None, options, ) - elif options or fmt: - ws.set_column(col_idx, col_idx, None, fmt, options) # finally, inject any sparklines into the table for column, params in (sparklines or {}).items():