首页 > 解决方案 > 如何使用python在docx中获取单元格文本旋转?

问题描述

我正在编写 Python 脚本以从 MS Word 表中获取数据并在另一个应用程序中绘制此类表。所以我无法解决如何在现有 Word 文档中获取有关单元格文本旋转的信息的问题。

我使用现有的解决方案来设置单元格文本旋转。

def get_vertical_cell_direction(cell: _Cell):       
    tc = cell._tc
    tcPr = tc.get_or_add_tcPr()    
    return tcPr

此代码返回:“CT_TcPr at 0x2e102875958>” 阅读 docx 文档我很困惑接下来要做什么来获取 textDirection 属性。

标签: python-3.xpython-docx

解决方案


与我问的其他问题相同,它需要检查单元格的 xml 代码:

def get_text_direction(cell):    
    # direction: tbRl -- top to bottom, btLr -- bottom to top
    pattern = re.compile('w:textDirection w:val=\"(\S*)\"')
    match = pattern.search(cell._tc.xml)
    if match:
        txt = match.group(1)
        if txt == 'btLr':
            return 90
        elif  txt == 'tbRl':
            return -90
    return 0

推荐阅读