Skip to content

Commit

Permalink
accounted for headers which will write a row, starting offset [numeri…
Browse files Browse the repository at this point in the history
…cal offset given in the form of an int tuple only], and checked to ensure max Excel cols are also not exceeded
  • Loading branch information
banflam committed Jan 25, 2025
1 parent cdd74c1 commit b59eb96
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions py-polars/polars/dataframe/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -3507,9 +3507,25 @@ def write_excel(
table_start[1] + len(df.columns) - 1,
)

# verify that the number of rows and columns, after accounting for an offset and a header row, is within the maximum for Excel
excel_max_valid_rows = 1048575
if self.height > excel_max_valid_rows:
msg = "Dataframe too large to be compatible with Excel. Exceeded Excel limit of 1048575 rows of data."
excel_max_valid_cols = 16384

initial_row_offset = 0
initial_col_offset = 0

# it is just a numerical offset given in the form of a tuple
if isinstance(position, tuple):
initial_row_offset, initial_col_offset = position

total_rows = self.height + initial_row_offset
if include_header:
total_rows += 1

total_cols = self.width + initial_col_offset

if total_rows > excel_max_valid_rows or total_cols > excel_max_valid_cols:
msg = "Dataframe too large to be compatible with Excel. Exceeded Excel limit of 1048575 rows and/or 16384 columns of data."
raise InvalidOperationError(msg)

# write table structure and formats into the target sheet
Expand Down

0 comments on commit b59eb96

Please sign in to comment.