【问题标题】:Python-docx set the text direction RTLpython-docx设置文本方向RTL
【发布时间】:2017-02-01 16:55:23
【问题描述】:

我正在尝试使用 RTL(从右到左)文本方向创建文档

def printExam():

  #get the exam questions 

  rows = db(db.exam_questions.exam == request.vars.exam).select()

  # create the documnet
  document = Document()
  document.add_heading(u"أختبار", 0)

  #for row in rows:
  row = rows[0] 
  run = document.add_paragraph().add_run(str(row.question.questionText).decode( "utf-8" ))
  font = run.font
  font.rtl = True

我遇到了以下异常:

Traceback (most recent call last):
  File "C:\Users\web2py-src\gluon\restricted.py", line 227, in restricted
    exec ccode in environment
  File "C:/Users/web2py-src/applications/draft/controllers/question.py", line 96, in <module>
  File "C:\Users\web2py-src\gluon\globals.py", line 417, in <lambda>
    self._caller = lambda f: f()
  File "C:/Users/web2py-src/applications/draft/controllers/question.py", line 68, in printExam
    font.rtl = True
AttributeError: 'Font' object attribute 'rtl' is read-only

【问题讨论】:

  • 如果您能不厌其烦地正确缩进代码就好了。
  • font.rtl 当前设置为什么?
  • 当我打印它的值时:真
  • 如果您不执行font.rtl = True,您会遇到问题吗?
  • 如果值已经是True,则似乎不需要该行。

标签: python-2.7 python-docx


【解决方案1】:

您的段落没有定义自定义样式,因此应用了默认的latent style,并且这种样式是只读的,因为它在文档中没有真正的定义。

在您的跑步或段落中应用自定义样式,您将能够对其进行自定义。要运行,您必须创建字符样式 (WD_STYLE_TYPE.CHARACTER)。

# create the document and the custom style
document = Document()
mystyle = document.styles.add_style('mystyle', WD_STYLE_TYPE.CHARACTER)
...
...
# apply the custom on the run
run.style = mystyle
font = run.font
font.rtl = True

【讨论】:

  • 非常感谢@DLDR 的回答 :)
  • @Mad Physicist,这个答案解决了这个问题:)
  • ,如何将 Text alignmnet 设置为正确,我在文档中看到它只能在段落级别添加
    是否可以为文档中的所有语句设置样式每个都没有定义样式,
  • 好的,我在文档中没有看到,但您也可以通过创建段落样式并将其设置为您的段落来解决您的问题。
  • 是的,但是mt的问题是设置段落右对齐和RTL,不能混合它们
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-08-16
  • 2018-07-03
  • 1970-01-01
  • 2016-02-11
  • 2015-10-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多