代码之家  ›  专栏  ›  技术社区  ›  Dmitry Arkhipenko

如何旋转表格单元格中的文本?

  •  3
  • Dmitry Arkhipenko  · 技术社区  · 8 年前

    我试着把桌子做成这样:

    Table with vertical header cells

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

    P、 很抱歉没有翻译表。

    1 回复  |  直到 8 年前
        1
  •  4
  •   MadisonTrash    8 年前

    为那些太累而找不到的人编写的代码片段:

    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)