无法为 python-docx 中的文本设置 "right to left" 对齐方式
Not able to set "right to left" alignment style for text in 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')
我还没有开始玩 docx(我主要使用 Excel python 模块),但是 based on the documentation here it's looking like you're modifying the wrong property of style. The Font property, per this definition of the rtl property, would only modify an 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')
再说一次,只是离开了文档,如果可行的话请告诉我。
我正在尝试使用 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 it's looking like you're modifying the wrong property of style. The Font property, per this definition of the rtl property, would only modify an 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')
再说一次,只是离开了文档,如果可行的话请告诉我。