如何使用python-docx在文字中从右向左设置

时间:2019-04-27 16:31:18

标签: python-3.7 python-docx word-2016

我无法使用python-docx库将文本方向(注意:不是文本对齐)设置为从右到左。你有解决这个问题的主意吗?

from docx import Document
from docx.enum.style import WD_STYLE_TYPE
from docx.enum.text import WD_TAB_ALIGNMENT,WD_PARAGRAPH_ALIGNMENT

doc = Document()

style = doc.styles.add_style('rtl', WD_STYLE_TYPE.CHARACTER)
style.font.rtl = True

paragraph = doc.add_paragraph()
paragraph.paragraph_format.alignment = WD_PARAGRAPH_ALIGNMENT.RIGHT

paragraph.add_run('تست Test تست Test',style = "rtl")

doc.save('test.docx')

1 个答案:

答案 0 :(得分:0)

我首先调用一个带有 rtl 的模板文档(正常样式是 rtl、阿拉伯字体等)

doc = Document(template_path)
p = doc.add_paragraph()
r = p.add_run()
font = r.font
font.complex_script = True
font.rtl = True
r.add_text(text)