2015-03-13 42 views
3

com.sun.star.style.ParagraphProperties服務支持屬性 ParaAdjust,支持5個值從com.sun.star.style.ParagraphAdjustParagraphPropertiesParagraphAdjust)。如何使用Libre/Open Office中的pyUNO庫檢查段落調整?

要設置的值,這兩種方法中的一種,可以使用:

cursor.ParaAdjust = com.sun.star.style.ParagraphAdjust.RIGHT 
cursor.setPropertyValue('ParaAdjust', com.sun.star.style.ParagraphAdjust.RIGHT) 

要檢查值的第一個嘗試是:

if cursor.ParaAdjust == com.sun.star.style.ParagraphAdjust.RIGHT: 
    ... 

,但沒有奏效。

檢查:

type(cursor.ParaAdjust) 
----> <class 'int'> 
type(com.sun.star.style.ParagraphAdjust.RIGHT) 
----> <class 'uno.Enum'> 

正確的,我認爲這些都是常數(請參閱下面的註釋),我的錯。

現在,uno.Enum類有兩個屬性typeNamevalue,所以我嘗試:

if cursor.ParaAdjust == com.sun.star.style.ParagraphAdjust.RIGHT.value: 
    ... 

,但沒有工作,太!

檢查:

type(com.sun.star.style.ParagraphAdjust.RIGHT.value) 
----> <class 'string'> 
print(com.sun.star.style.ParagraphAdjust.RIGHT.value) 
----> 'RIGHT' 

設置ParaAdjust屬性,然後打印出它的實際價值,我得到:

LEFT = 0 
RIGHT = 1 
BLOCK = 2 
CENTER = 3 
STRETCH = 0 
(note that STRETCH is considered as LEFT, 
a bug or something not implemented?) 

所以:

  • 在哪裏這些值定義?
  • 如何使用UNO API獲取這些值?
  • 我在官方文檔中遺漏了什麼嗎?

注意

uno.getConstantByName('com.sun.star.style.ParagraphAdjust.RIGHT') 

從版本4.1不起作用:

在LibreOffice的4.0(也許在老版本太),你可以用得到這個值(正確,不是一個常數)。

回答

1

感謝「漢雅」從OpenOffice的論壇(link),這裏的一些Python代碼映射的ParagraphAdjust值:

def get_paragraph_adjust_values(): 
    ctx = uno.getComponentContext() 
    tdm = ctx.getByName(
      "/singletons/com.sun.star.reflection.theTypeDescriptionManager") 
    v = tdm.getByHierarchicalName("com.sun.star.style.ParagraphAdjust") 
    return {name : value 
      for name, value 
      in zip(v.getEnumNames(), v.getEnumValues())} 

在Python 2.6中,不支持對理解語法字典,dict()函數可以用來代替。