2

I am trying to create a docunent with RTL (Right-To-Left) text direction

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

I got the following exception:

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
Udi
  • 29,222
  • 9
  • 96
  • 129
User
  • 573
  • 3
  • 15
  • 28

1 Answers1

3

No custom style has been defined on your paragraph so a default latent style is applied and this kind of style is read-only because it has no real definition in the document.

Apply a custom style on your run or your paragraph and you will be able to customize it. For a run, you have to create character 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
Maxwell77
  • 918
  • 6
  • 16
  • Thanks a lot @DLDR for ur answer :) – User Feb 07 '17 at 13:05
  • @ Mad Physicist, this answer fixed the issue :) – User Feb 07 '17 at 13:06
  • ,How can set the Text alignmnet to be right, i see in the documentation that it can be addded on the paragraph level only
    Is there away to set the style for all statements in the documnet without define style per each one,
    – User Feb 07 '17 at 13:32
  • Ok, I haven't seen that in the documentation but you can also solve your problem by creating a paragraph style and set it to your paragraph. – Maxwell77 Feb 07 '17 at 13:37
  • Yes, but mt problem is to set the paragraph right aligned and RTL , couldn't mix them – User Feb 07 '17 at 13:51
  • For now, the solution I think which could work would be to create a base document in Ms Word and modifying the default normal style in it as needed. Then using Python-docx, I would use it as base document and inject all the new text in it. – Maxwell77 Feb 07 '17 at 14:46
  • NameError: name 'WD_STYLE_TYPE' is not defined – keramat Sep 24 '19 at 12:59