我正在尝试使用 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 模块),但是 based on the documentation here看起来您正在修改错误的样式属性。字体属性,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')
同样,只是离开了文档,所以让我知道它是否有效。
