Python-docx 设置文字方向RTL
Python-docx set the text direction RTL
我正在尝试创建具有 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
您的段落没有定义自定义样式,因此应用了默认 latent style,这种样式是 read-only,因为它在文档中没有真正的定义。
在您的 运行 或您的段落上应用自定义样式,您将能够对其进行自定义。对于 运行,您必须创建字符样式 (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
我正在尝试创建具有 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
您的段落没有定义自定义样式,因此应用了默认 latent style,这种样式是 read-only,因为它在文档中没有真正的定义。
在您的 运行 或您的段落上应用自定义样式,您将能够对其进行自定义。对于 运行,您必须创建字符样式 (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