Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(python): Write data at table level in write_excel #17757

Merged
merged 4 commits into from
Jul 25, 2024
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 5 additions & 15 deletions py-polars/polars/dataframe/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -3188,6 +3188,7 @@ def write_excel(
*table_start,
*table_finish,
{
"data": df.rows(),
"style": table_style,
"columns": table_columns,
"header_row": include_header,
Expand All @@ -3198,18 +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(),
cell_format=column_formats.get(c),
)
column_start[1] += 1

# apply conditional formats
if conditional_formats:
_xl_apply_conditional_formats(
Expand All @@ -3234,18 +3223,19 @@ 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]
ws.set_column_pixels(
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)
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():
Expand Down