gregbanks / python-tabulate

fork of https://bitbucket.org/astanin/python-tabulate
MIT License
177 stars 14 forks source link

wrapping text in cells of table #9

Open AtomicNess123 opened 3 years ago

AtomicNess123 commented 3 years ago

Is this possible? THanks.

smichr commented 1 year ago

You could do something like this. You would have to decide on how you would treat existing line breaks within the cell:

def wrap_cell(cell, w):
  from textwrap import wrap
  rv = []
  for i in cell.splitlines():
    rv.extend(wrap(i, w))
  return '\n'.join(rv)

>>> lines = wrap_cell('this is a long piece of text'
' that we want to change by wrapping it '
'into shorter lines\nand this was the second line',20)
>>> print(lines)
this is a long piece
of text that we want
to change by
wrapping it into
shorter lines
and this was the
second line