【问题标题】:Not able to set "right to left" alignment style for text in python-docx无法为 python-docx 中的文本设置“从右到左”对齐方式
【发布时间】:2018-09-26 10:18:19
【问题描述】:

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

【问题讨论】:

  • 你看过this question吗?
  • 是的,它使用该解决方案。但我必须在添加每个段落后添加“paragraph.alignment = 0”。所以我试图找到一个更好的解决方案来减少代码行。

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


【解决方案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')

再一次,只是离开文档,所以让我知道这是否有效。

【讨论】:

    猜你喜欢
    • 2015-10-07
    • 2015-05-07
    • 2018-01-05
    • 2018-11-05
    • 1970-01-01
    • 2011-11-25
    • 2021-01-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多