Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 2.7 Python docx设置文本方向RTL_Python 2.7_Python Docx - Fatal编程技术网

Python 2.7 Python docx设置文本方向RTL

Python 2.7 Python docx设置文本方向RTL,python-2.7,python-docx,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

我正在尝试创建一个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
回溯(最近一次呼叫最后一次):
文件“C:\Users\web2py src\glion\restricted.py”,第227行,受限
环境中的执行代码
文件“C:/Users/web2py src/applications/draft/controllers/question.py”,第96行,在
文件“C:\Users\web2py src\glion\globals.py”,第417行,在
self.\u caller=lambda f:f()
文件“C:/Users/web2py src/applications/draft/controllers/question.py”,第68行,在printExam中
font.rtl=True
AttributeError:“字体”对象属性“rtl”是只读的

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

在跑步或段落中应用自定义样式,您将能够对其进行自定义。对于跑步,您必须创建字符样式(
WD\u style\u TYPE.character


如果你能不厌其烦地正确缩进你的代码那就太好了。
font.rtl
当前设置为什么?当我打印它的值时:True如果你不执行
font.rtl=True
,你会遇到问题吗?如果值已经是
True
,那么这行代码似乎是不必要的。非常感谢@DLDR的回答:)@Mad physicator,这个答案解决了一个问题:),如何将文本对齐设置为正确,我在文档中看到它只能添加到段落级别
是否可以为documnet中的所有语句设置样式而不定义每个语句的样式,好吗,我在文档中没有看到,但您也可以通过创建段落样式并将其设置为段落来解决问题。是的,但mt的问题是将段落设置为右对齐,而RTL不能将它们混合使用
# 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