Python-docx设置文本方向RTL

问题描述 投票:1回答:1

我正在尝试使用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
python-2.7 python-docx
1个回答
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
© www.soinside.com 2019 - 2024. All rights reserved.