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

fix(python): Throw exception if dataframe is too large to be compatible with Excel #20900

11 changes: 11 additions & 0 deletions py-polars/polars/dataframe/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@
from polars.dependencies import pyarrow as pa
from polars.exceptions import (
ColumnNotFoundError,
InvalidOperationError,
ModuleUpgradeRequiredError,
NoRowsReturnedError,
TooManyRowsReturnedError,
Expand Down Expand Up @@ -3506,6 +3507,16 @@ def write_excel(
table_start[1] + len(df.columns) - 1,
)

excel_max_valid_rows = 1048575
excel_max_valid_cols = 16384

if (
table_finish[0] > excel_max_valid_rows
or table_finish[1] > excel_max_valid_cols
):
msg = f"writing {df.height}x{df.width} frame at {position!r} does not fit worksheet dimensions of {excel_max_valid_rows} rows and {excel_max_valid_cols} columns"
raise InvalidOperationError(msg)

# write table structure and formats into the target sheet
if not is_empty or include_header:
ws.add_table(
Expand Down
16 changes: 15 additions & 1 deletion py-polars/tests/unit/io/test_spreadsheet.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@

import polars as pl
import polars.selectors as cs
from polars.exceptions import NoDataError, ParameterCollisionError
from polars.exceptions import (
NoDataError,
ParameterCollisionError,
)
from polars.testing import assert_frame_equal, assert_series_equal
from tests.unit.conftest import FLOAT_DTYPES, NUMERIC_DTYPES

Expand Down Expand Up @@ -1184,6 +1187,17 @@ def test_excel_write_worksheet_object() -> None:
df.write_excel(None, worksheet=ws)


def test_excel_write_beyond_max_rows_cols(tmp_path: Path) -> None:
tmp_path.mkdir(exist_ok=True)
path = tmp_path / "test_max_dimensions.xlsx"
sheet = "mysheet"

df = pl.DataFrame({"col1": range(10), "col2": range(10, 20)})

with pytest.raises(pl.exceptions.InvalidOperationError):
df.write_excel(workbook=path, worksheet=sheet, position="A1048570")


def test_excel_freeze_panes() -> None:
from xlsxwriter import Workbook

Expand Down
Loading