如何使用 python 在 docx 中旋转单元格文本?
How to get cell text rotation in docx with python?
我正在编写 Python 脚本以从 MS Word table 获取数据并绘制这样的 table 另一个应用程序。所以我无法解决如何在现有 Word 文档中获取单元格文本旋转信息的问题。
我在设置单元格文本旋转时使用了。
def get_vertical_cell_direction(cell: _Cell):
tc = cell._tc
tcPr = tc.get_or_add_tcPr()
return tcPr
这个代码returns这个:"CT_TcPr at 0x2e102875958>"
阅读 docx 文档我很困惑接下来要做什么才能获得 textDirection 属性.
和我问的一样,它需要检查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
我正在编写 Python 脚本以从 MS Word table 获取数据并绘制这样的 table 另一个应用程序。所以我无法解决如何在现有 Word 文档中获取单元格文本旋转信息的问题。
我在设置单元格文本旋转时使用了
def get_vertical_cell_direction(cell: _Cell):
tc = cell._tc
tcPr = tc.get_or_add_tcPr()
return tcPr
这个代码returns这个:"CT_TcPr at 0x2e102875958>" 阅读 docx 文档我很困惑接下来要做什么才能获得 textDirection 属性.
和我问的
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