【问题标题】:How to rotate text in table cells?如何旋转表格单元格中的文本?
【发布时间】:2023-10-19 20:50:02
【问题描述】:

我正在尝试制作这样的表格:

如您所见,标题是垂直方向的。 如何使用 python-docx 实现这一点?

附:对不起,未翻译的表格。

【问题讨论】:

    标签: python-docx


    【解决方案1】:

    为那些太累而无法寻找的人摘录:

    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)
    

    【讨论】:

      最近更新 更多