【问题标题】:How to get cell text rotation in docx with python?如何使用python在docx中获取单元格文本旋转?
【发布时间】:2020-01-11 07:21:30
【问题描述】:

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

我在设置单元格文本旋转时使用了existing solution

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.x python-docx


    【解决方案1】:

    other question我问的一样,需要检查一个单元格的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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-10
      • 1970-01-01
      • 2018-07-30
      相关资源
      最近更新 更多