From f7e3696568852653f794157a79252848363c8808 Mon Sep 17 00:00:00 2001 From: Gyanu Date: Thu, 27 Aug 2026 00:45:08 +0530 Subject: [PATCH] Keep blank lines when wrapping table cells. _wrap_text_to_colwidths dropped empty segments, so a cell with a blank line collapsed when maxcolwidths was set. --- tabulate/__init__.py | 1 - test/test_regression.py | 16 ++++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/tabulate/__init__.py b/tabulate/__init__.py index 12a2950..94ef275 100644 --- a/tabulate/__init__.py +++ b/tabulate/__init__.py @@ -1658,7 +1658,6 @@ def _wrap_text_to_colwidths( wrapped = [ "\n".join(wrapper.wrap(line)) for line in casted_cell.splitlines() - if line.strip() != "" ] new_row.append("\n".join(wrapped)) else: diff --git a/test/test_regression.py b/test/test_regression.py index 9555676..fbeb076 100644 --- a/test/test_regression.py +++ b/test/test_regression.py @@ -504,6 +504,22 @@ def test_preserve_line_breaks_with_maxcolwidths(): assert_equal(expected, result) +def test_preserve_blank_lines_with_maxcolwidths(): + "Regression: keep blank lines when wrapping cells (github issue #440)" + table = [["foo\n\nbar"]] + expected = "\n".join( + [ + "+-----+", + "| foo |", + "| |", + "| bar |", + "+-----+", + ] + ) + result = tabulate(table, tablefmt="grid", maxcolwidths=10) + assert_equal(expected, result) + + def test_maxcolwidths_accepts_list_or_tuple(): "Regression: maxcolwidths can accept a list or a tuple (github issue #214)" table = [["lorem ipsum dolor sit amet"] * 3]