无法为pythondocx中的文本设置“从右到左”对齐方式

2024-10-04 01:35:55 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图用pythondocx将一些文本写入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')

Tags: 文件文本addstyletype样式rtldocx
1条回答
网友
1楼 · 发布于 2024-10-04 01:35:55

我还没来得及玩docx(我主要使用Excel python模块),但是based on the documentation here看起来你修改了style的错误属性。字体属性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

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

^{pr2}$

再说一遍,把文档删掉,让我知道这是否有效。在

相关问题 更多 >