Retrieving numbering (list item) details from a paragraph

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
_savage
Posts: 187
Joined: Sun Apr 21, 2013 12:55 am

Retrieving numbering (list item) details from a paragraph

Post by _savage »

While my script iterates over the paragraphs of a document:

Code: Select all

    parenum = document.Text.createEnumeration()
    while parenum.hasMoreElements() :
        par = parenum.nextElement()
        if par.supportsService("com.sun.star.text.Paragraph") :
            ...
it detects a bullet or enumeration list through the paragraph's "NumberingStyleName" property: if it's not an empty string then the paragraph is some form of list item. (Loading documents from different sources like Word versions etc, however, seems to result in all sorts of numbering strings.)

My question now is: how can I differentiate nested lists? I mean, looking at a paragraph which properties tell me if this is a numbered or bullet list, style of numbering, and it's nesting depth.

Thanks!
Mac 10.14 using LO 7.2.0.2, Gentoo Linux using LO 7.2.3.2 headless.
User avatar
karolus
Volunteer
Posts: 1158
Joined: Sat Jul 02, 2011 9:47 am

Re: Retrieving numbering (list item) details from a paragrap

Post by karolus »

Hallo

check

Code: Select all

par.NumberingLevel 
and use MRI

Karolus
AOO4, Libreoffice 6.1 on Rasbian OS (on ARM)
Libreoffice 7.4 on Debian 12 (Bookworm) (on RaspberryPI4)
Libreoffice 7.6 flatpak on Debian 12 (Bookworm) (on RaspberryPI4)
_savage
Posts: 187
Joined: Sun Apr 21, 2013 12:55 am

Re: Retrieving numbering (list item) details from a paragrap

Post by _savage »

Thanks :)

I've clicked through MRI for an open document but I found it quite overwhelming to sift through all the information that I'm looking for. Having said that, I could have just checked the UNO Paragraph API.
Mac 10.14 using LO 7.2.0.2, Gentoo Linux using LO 7.2.3.2 headless.
_savage
Posts: 187
Joined: Sun Apr 21, 2013 12:55 am

Re: Retrieving numbering (list item) details from a paragrap

Post by _savage »

The NumberingLevel property of the par does indeed provide a short integer value between 0 (outer-most) and N (increasing number of nesting levels) that I can use.

The next question then is whether a numbering uses bullets of various kinds or an enumeration. So I looked at the paragraph's NumberingRules property which gives a few hints by its own NumberingType and Name properties. However, scanning through a document I found that most of those properties are set to None or some funky integer (Name=45990630598139397491). Continuing my search I then found a NumberingLevel service but neither the paragraph nor the numbering rules provide that service.

EDIT After some playing around I found that the following code works and it returns an integer which represents the NumberingType value. It iterates over the NumberingRules's PropertyValue list to find the one I'm interested in:

Code: Select all

# Helper function get access an object's attribute; return None if the attribute didn't exist for the object.
def get_uno_attr(obj, attr) :
    return getattr(obj, attr) if hasattr(obj, attr) else None

# Helper function to iterate over a PropertyValue list to find a property by name, and return its value; return None if not found.
def get_uno_pval(plist, vname) :
    for pval in plist :
        if vname == get_uno_attr(pval, "Name") :
            return get_uno_attr(pval, "Value")
    return None


# Get the numbering rules for this paragraph, which contain more details.
rules = get_uno_attr(paragraph, "NumberingRules")

# Get the number of PropertyValue items for the paragraph's numbering rule set.
pnum = get_uno_attr(rules, "Count")

# Oddly, any hardwired index between 0..pnum-1 works and returns the same list or PropertyValues.
numtype = get_uno_pval(rules.getByIndex(0), "NumberingType"))
The numtype is an integer representing any of the NumberingType values. Other properties are the ones I found provided by the NumberingLevel service mentioned above. However, I can't explain why the above call to getByIndex() would return the entire list (array) instead of the indexed item.
Mac 10.14 using LO 7.2.0.2, Gentoo Linux using LO 7.2.3.2 headless.
_savage
Posts: 187
Joined: Sun Apr 21, 2013 12:55 am

Re: Retrieving numbering (list item) details from a paragrap

Post by _savage »

_savage wrote:However, I can't explain why the above call to getByIndex() would return the entire list (array) instead of the indexed item.
See also this comment on the “Starbasic Macro for bullets and numbering look” thread.
Mac 10.14 using LO 7.2.0.2, Gentoo Linux using LO 7.2.3.2 headless.
Post Reply