[Solved] How to check ParaAdjust value with pyUNO?

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
_LX_
Posts: 2
Joined: Fri Mar 13, 2015 11:50 pm

[Solved] How to check ParaAdjust value with pyUNO?

Post by _LX_ »

How could I write conditional statements to check the paragraph adjustment while scanning a document?

Let me explain:

Code: Select all

from com.sun.star.style.ParagraphAdjust import RIGHT
...
cursor.ParaAdjust = RIGHT
print(cursor.ParaAdjust == RIGHT)
---> False
:?
checking the types:

Code: Select all

type(cursor.ParaAdjust)
---> <class 'int'>
type(RIGHT)
---> <class 'uno.Enum'>
Checking the uno.py file I get that the uno.Enum class has two properties, typeName and value:

Code: Select all

type(RIGHT.value)
---> <class 'string'>
print(RIGHT.value)
---> 'RIGHT'
If I set the ParaAdjust property with the 5 values from com.sun.star.style.ParagraphAdjust, and then print the actual value of ParaAdjust I get:

Code: Select all

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

Where are these values defined?
How could I get these values using the pyUNO API?
Last edited by _LX_ on Sat Mar 14, 2015 10:34 pm, edited 1 time in total.
hanya
Volunteer
Posts: 885
Joined: Fri Nov 23, 2007 9:27 am
Location: Japan

Re: How to check ParaAdjust value with pyUNO?

Post by hanya »

Hi, welcome to the forum.

The enum like ParagraphAdjust is defined in relative IDL file and converted into API reference.
Its data can be accessed through type description manager. In C, enum member has only interger value but the type description provides access to members by their names.
In general, to check the relative page of the enum value is enough: http://www.openoffice.org/api/docs/comm ... djust.html

If you want to get name to value map of enum values, try the following code written in Basic:

Code: Select all

Sub GetEnumValues
  tdm = GetDefaultContext().getByName("/singletons/com.sun.star.reflection.theTypeDescriptionManager")
  v = tdm.getByHierarchicalName("com.sun.star.style.ParagraphAdjust")
  names = v.getEnumNames()
  values = v.getEnumValues()
  s = ""
  for i = 0 to ubound(names) step 1
    s = s & names(i) & ": " & cstr(values(i) & chr(10)
  next
  msgbox s
End Sub
Please, edit this thread's initial post and add "[Solved]" to the subject line if your problem has been solved.
Apache OpenOffice 4-dev on Xubuntu 14.04
_LX_
Posts: 2
Joined: Fri Mar 13, 2015 11:50 pm

Re: How to check ParaAdjust value with pyUNO?

Post by _LX_ »

Hi hanya,
thank you for the welcome and thanks for your answer. :D

Let me share the python code of a function that returns a dictionary with the mapping of the enum values:

Code: Select all

def get_paragraph_adjust_values():
    tdm = uno.getComponentContext().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())}
(note: with python 2.6 the dictionary can be created using the dict() function instead of the comprehension form)

Just for curiosity, I've noted that using the previous function 'STRETCH' is mapped to '4' (as expected), but if I set the value of ParaAdjust with

Code: Select all

from com.sun.star.style.ParagraphAdjust import STRETCH
cursor.ParaAdjust = STRETCH
or with

Code: Select all

cursor.ParaAdjust = 4
ParaAdjust is initialized to '0', why?
Post Reply