Python-docx设置文本方向RTL

时间:2017-02-01 16:55:24

标签: python-2.7 python-docx

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

1 个答案:

答案 0 :(得分:3)

您的段落上没有定义自定义样式,因此应用了默认的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