'Text wrapping in cell spanning over multiple rows - Sphinx ReStructuredText

When creating a table with cell spanning over multiple rows I notice that the contents of the text don't wrap but increase the width of the table to produce horizontal scroll bars.

For example consider the following RST,

+-------+-----------------+
|       |                 |
+-------+-----------------+
|       | This text must  |
+-------+ wrap over to    |
|       | the next line   |
+-------+-----------------+

when built produces,

sphynx-build output

As I increase the length of the text, the table grows horizontally. what should I do for it to wrap? For now, I'm forcing it to wrap by manually inserting line breaks with "|" but the produced output looks bad.



Solution 1:[1]

There is a table directive with a :widths: attribute.

.. table::
    :widths: 20, 80

    +-------+-----------------+
    |       |                 |
    +-------+-----------------+
    |       | This text must  |
    +-------+ wrap over to    |
    |       | the next line   |
    +-------+-----------------+

There might also be a way to control column widths with custom CSS, but I was not able to override the <colgroup> tags in the Sphinx output.

Solution 2:[2]

You won't be able to override the colgroup tag in Sphinx. You could try using the rst2html5 package instead, which doesn't generate colgroup. Another option is using CSS to control column widths:

col:nth-child(1) {
  width: 200px;
}

col:nth-child(2) {
  width: 300px;
}
<table border="1" class="docutils">
<colgroup>
<col width="29%" />
<col width="71%" />
</colgroup>
<tbody valign="top">
<tr><td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr><td>&nbsp;</td>
<td rowspan="2">This text must
wrap over to
the next line</td>
</tr>
<tr><td>&nbsp;</td>
</tr>
</tbody>
</table>

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Steve Piercy
Solution 2 andref