无法在 python-docx 中为文本设置"right to left"对齐样式



我正在尝试使用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')

我还没有开始使用 docx(我主要使用 Excel python 模块(,但根据这里的文档,看起来您正在修改错误的样式属性。根据 rtl 属性的此定义,Font 属性只会修改添加的运行(通过 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')

同样,只是离开文档,所以让我知道这是否有效。

最新更新