UNO: iterate through headings?

In python I have an opened document, and I need to change styles of
some «Heading 1»'s. (actually I need to do the thing like here
http://ask.libreoffice.org/en/question/53521/how-to-exclude-some-chapters-from-automatic-numbering-of-headings/
, but in script)

I couldn't figure out, what is a function or property of an opened
with «loadComponentFromURL» document to iterate over these? I guess
that would be something like «getAllParagraphs», or
«getParagraphsByStyle»… Either way, I couldn't find anything relevant.

In python I have an opened document, and I need to change styles of
some «Heading 1»'s. (actually I need to do the thing like here
http://ask.libreoffice.org/en/question/53521/how-to-exclude-some-chapters-from-automatic-numbering-of-headings/
, but in script)

I couldn't figure out, what is a function or property of an opened
with «loadComponentFromURL» document to iterate over these? I guess
that would be something like «getAllParagraphs», or
«getParagraphsByStyle»… Either way, I couldn't find anything relevant.

In the GUI you simply modify the paragraph style in order to change the
appearance of all the paragraphs where the stye applies.
Without MRI you should not even try to write any macros.

[Tutorial] Introduction into object inspection with MRI

https://forum.openoffice.org/en/forum/viewtopic.php?f=74&t=49294

It leads you to the StyleFamilies of your documents and the built-in
ParagraphStyles which are accessible by their English names. On the fly
it records macros in 4 langauges.

Thank you for suggestion. Unfortunately I didn't managed to understand
how to work with MRI. Could you show an example of MRI usage with this
particular case?

I don't know if that relevant, but I am not writing macros, I am using
the API from a separate script with LO being run as a server.

Okay, to point what is confuses me with MRI — I tried to copy
properties list when cursor is in the table of contents and somewhere
in text. The *diff* utility then shows no difference.

But supposing that there is indeed no difference, I still couldn't see
how that could easily give me an idea for how to iterate through
headings. I do understand that the only thing I need is just change
style of particular paragraphs, and here I'm stuck.

I found there's a function «getFlatParagraphIterator()», which result
in turn have functions like «getFirstPara()». That function returns a
type «XFlatParagraph», and I couldn't find in methods and properties
anything related to styles. Do anybody know anything about it?

In the end the «getFlatParagraphIterator()» turned to be unrelated to
what I want to. I found an answer, and since α) nobody seems to know
out there for how to do it, and β) it is impossible to guess how to do
it without someone's example, so I'll leave the code here.

def removeNumberingSomeHeading1s(text):
    """Removes numbering from the first Heading1 (intro), then skips
three heading1s (chapters), and
removes from the rest Heading1s and Heading2s"""
    enumeration = text.createEnumeration()
    heading1s = 0
    while enumeration.hasMoreElements():
        par = enumeration.nextElement()
        if ( par.supportsService("com.sun.star.text.Paragraph") and
            par.ParaStyleName != None):
            if par.ParaStyleName == HEADING1:
                heading1s = heading1s + 1
            if ((heading1s > 4 or heading1s == 1) and
                par.ParaStyleName == HEADING1 or
                par.ParaStyleName == HEADING2):
                    par.setPropertyValue("NumberingStyleName", "NONE")

It takes Document.Text property as arg. The magic «paragraph iterator»
function in the end turned to be the «Text.createEnumeration()». I
guess it enumerates not only paragraphs, so one need to check that it
actually is — in above the code I'm doing this with the «if (
par.supportsService("com.sun.star.text.Paragraph")»

I know next to nothing about Writer because I rarely use any word
processors and my mind can not sync Writer's API with what I see on the
screen. Let's try anyway:

The enumeration of the document text seems to reflect the paragraphs inded.

Sub enumDocText
e=thiscomponent.text.createenumeration
while e.hasmoreelements
  print e.nextelement.string
wend
End Sub

Looping through UNO objects is highly inefficient. Let the application
do the loop:

Find/Replace

[More Options]
[X] Search for para stlyes
Search: Heading 1
[Find All]

which selects all matching paragraphs and
ThisComponent.CurrentController.Selection.Count reports the count of
selected paragraphs.

This would be equivalent to method X=ThisComponent.findAll(oDescriptor)
with the right descriptor settings.
The descriptor is an array of c.s.s.beans.PropertyValue.
X.Count would return the element count of the found objects.

Thank you, that is interesting!