Skip to content

Commit 20e357a

Browse files
committed
Add check for character limmit
1 parent 7281475 commit 20e357a

File tree

3 files changed

+19
-0
lines changed

3 files changed

+19
-0
lines changed

pandas/core/generic.py

+4
Original file line numberDiff line numberDiff line change
@@ -2363,6 +2363,10 @@ def to_excel(
23632363
Once a workbook has been saved it is not possible to write further
23642364
data without rewriting the whole workbook.
23652365
2366+
Pandas will check the number of rows, columns,
2367+
and cell character count does not exceed Excel's limitations.
2368+
All other limitations must be checked by the user.
2369+
23662370
Examples
23672371
--------
23682372

pandas/io/excel/_base.py

+6
Original file line numberDiff line numberDiff line change
@@ -1326,6 +1326,12 @@ def _value_with_fmt(
13261326
fmt = "0"
13271327
else:
13281328
val = str(val)
1329+
# GH#56954
1330+
if len(val) > 32767:
1331+
warnings.warn(
1332+
"String value too long, truncated to 32767 characters",
1333+
UserWarning,
1334+
stacklevel=find_stack_level())
13291335

13301336
return val, fmt
13311337

pandas/tests/io/excel/test_writers.py

+9
Original file line numberDiff line numberDiff line change
@@ -1505,3 +1505,12 @@ def test_subclass_attr(klass):
15051505
attrs_base = {name for name in dir(ExcelWriter) if not name.startswith("_")}
15061506
attrs_klass = {name for name in dir(klass) if not name.startswith("_")}
15071507
assert not attrs_base.symmetric_difference(attrs_klass)
1508+
1509+
1510+
def test_to_excel_raising_warning_when_cell_character_exceed_limit():
1511+
# GH#56954
1512+
df = DataFrame({"A": ["a" * 32768]})
1513+
msg = "String value too long, truncated to 32767 characters"
1514+
with tm.assert_produces_warning(UserWarning, match=msg):
1515+
buf = BytesIO()
1516+
df.to_excel(buf)

0 commit comments

Comments
 (0)