Skip to content

Commit

Permalink
ENH: Add check for character limit (pandas-dev#57103)
Browse files Browse the repository at this point in the history
Co-authored-by: Matthew Roeschke <[email protected]>
  • Loading branch information
luke396 and mroeschke authored Feb 1, 2024
1 parent d9f9e12 commit bb42fc0
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 2 deletions.
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v3.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ enhancement2

Other enhancements
^^^^^^^^^^^^^^^^^^
-
- :func:`DataFrame.to_excel` now raises an ``UserWarning`` when the character count in a cell exceeds Excel's limitation of 32767 characters (:issue:`56954`)
-

.. ---------------------------------------------------------------------------
Expand Down
4 changes: 4 additions & 0 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -2334,6 +2334,10 @@ def to_excel(
Once a workbook has been saved it is not possible to write further
data without rewriting the whole workbook.
pandas will check the number of rows, columns,
and cell character count does not exceed Excel's limitations.
All other limitations must be checked by the user.
Examples
--------
Expand Down
10 changes: 9 additions & 1 deletion pandas/io/excel/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1326,7 +1326,15 @@ def _value_with_fmt(
fmt = "0"
else:
val = str(val)

# GH#56954
# Excel's limitation on cell contents is 32767 characters
# xref https://support.microsoft.com/en-au/office/excel-specifications-and-limits-1672b34d-7043-467e-8e27-269d656771c3
if len(val) > 32767:
warnings.warn(
"Cell contents too long, truncated to 32767 characters",
UserWarning,
stacklevel=find_stack_level(),
)
return val, fmt

@classmethod
Expand Down
12 changes: 12 additions & 0 deletions pandas/tests/io/excel/test_writers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1385,6 +1385,18 @@ def test_to_excel_empty_frame(self, engine, ext):
expected = DataFrame()
tm.assert_frame_equal(result, expected)

def test_to_excel_raising_warning_when_cell_character_exceed_limit(
self, path, engine
):
# GH#56954
df = DataFrame({"A": ["a" * 32768]})
msg = "Cell contents too long, truncated to 32767 characters"
with tm.assert_produces_warning(
UserWarning, match=msg, raise_on_extra_warnings=False
):
buf = BytesIO()
df.to_excel(buf)


class TestExcelWriterEngineTests:
@pytest.mark.parametrize(
Expand Down

0 comments on commit bb42fc0

Please sign in to comment.