Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to rotate text in table cells?

Tags:

python-docx

I'm trying to make table like this:

Table with vertical header cells

As you can see, the header is vertically orientated. How can I achieve this using python-docx?

P.S. Sorry for non-translated table.

like image 402
Dmitry Arkhipenko Avatar asked Sep 05 '25 03:09

Dmitry Arkhipenko


1 Answers

Snippet for those who are too tired to seek:

from docx.oxml import OxmlElement
from docx.oxml.ns import qn
from docx.table import _Cell


def set_vertical_cell_direction(cell: _Cell, direction: str):
    # direction: tbRl -- top to bottom, btLr -- bottom to top
    assert direction in ("tbRl", "btLr")
    tc = cell._tc
    tcPr = tc.get_or_add_tcPr()
    textDirection = OxmlElement('w:textDirection')
    textDirection.set(qn('w:val'), direction)  # btLr tbRl
    tcPr.append(textDirection)
like image 103
MadisonTrash Avatar answered Sep 08 '25 00:09

MadisonTrash