无法在python-docx中为文本设置“从右到左”的对齐方式

时间:2018-04-16 15:44:59

标签: python python-3.x docx python-docx

我正在尝试使用python-docx将一些文本写入docx文件。我想从右到左对齐文本,并为此添加了一个样式,但这样做无效。

以下是代码:

from docx.enum.style import WD_STYLE_TYPE

missingwords= Document()
styles = missingwords.styles
style = missingwords.styles.add_style('rtl', WD_STYLE_TYPE.PARAGRAPH)
style.font.rtl = True

paragraph =missingwords.add_paragraph("Hello world",style='rtl')

1 个答案:

答案 0 :(得分:1)

我还没有开始使用docx(我主要使用的是Excel python模块),但是based on the documentation here看起来你正在修改样式的错误属性。 Font属性per this definition of the rtl property只会修改added run(通过myparagraph.add_run("Hello World", style = "rtl"))。据我所知,您要查找的代码是:

missingwords = Document()
style = missingwords.styles.add_style('rtl', WD_STYLE_TYPE.PARAGRAPH)
style.paragraph_format.alignment = WD_PARAGRAPH_ALIGNMENT.RIGHT

然后你可以继续添加像你一样的段落

paragraph = missingwords.add_paragraph("Hello world",style='rtl')

再次,只是关闭文档,所以让我知道这是否有效。

相关问题