Association between ToC index object and its paragraph content

Jens Tröger jens.troeger at
Tue Dec 12 21:44:20 UTC 2017

On Tue, Dec 12, 2017 at 03:53:41PM +0100, Michael Stahl wrote:
> On 12.12.2017 14:22, Jens Tröger wrote:
> > Michael, I the case you describe would I not see the same UNO object address for the same paragraph in the document? But iterating over the ToC’s paragraphs as described previously in this thread, I get for the index view:
> > 
> >     pyuno object ({implementationName=SwXParagraph, … }
> > 
> > and then for the global document view:
> > 
> >     pyuno object ({implementationName=SwXParagraph, … }
> > 
> > for the same first entry paragraph in the ToC. In fact, if I instantiate the ToC’s text range three times, then I get three different objects for the first paragraph:
> > 
> >     pyuno object (
> >     pyuno object (
> >     pyuno object (
> > 
> > Do paragraphs have another unique identifier that associates these different instances as objects representing the same document paragraph?
> do you retain a reference to the paragraphs somewhere?  Writer itself
> doesn't keep the SwXParagraph alive, so if your extension drops the last
> reference to it then it will be deleted and a new one created the next time.

This is getting interesting. If I get the first paragraph of the ToC
index twice then these are two different objects:

    index_pars = index.Anchor                                                                   
    index_cursor = document.Text.createTextCursorByRange(index_pars)                            
    index_parenum1 = index_cursor.createEnumeration()                                           
    while index_parenum1.hasMoreElements():                                                     
        index_par1 = index_parenum1.nextElement()                                               
    index_parenum2 = index_cursor.createEnumeration()                                           
    while index_parenum2.hasMoreElements():                                                     
        index_par2 = index_parenum2.nextElement()                                               
    print(index_par1 == index_par2)                                                             

If I do the same with the global document then the two paragraphs are
the same:

    parenum1 = document.Text.createEnumeration()                                                    
    while parenum1.hasMoreElements():                                                               
        par1 = parenum1.nextElement()                                                               
    parenum2 = document.Text.createEnumeration()                                                    
    while parenum2.hasMoreElements():                                                               
        par2 = parenum2.nextElement()                                                               
    print(par1 == par2)                                                                               

If I collect all ToC index paragraphs in a list first and then iterate
over the entire document then paragraphs that are part of the ToC index
are found:

    TOCPARSET = set()
    index_pars = index.Anchor                                                                   
    index_cursor = document.Text.createTextCursorByRange(index_pars)                            
    index_parenum = index_cursor.createEnumeration()                                            
    while index_parenum.hasMoreElements():                                                      
        index_par = index_parenum.nextElement()                                                 

    parenum = document.Text.createEnumeration()                                                     
    while parenum.hasMoreElements():                                                                
        par = parenum.nextElement()                                                                 
        print(par in TOCPARSET)                                                                     

While I would have expected the first example to print True, the third
example works fine and tells me what I'm looking for.


Jens Tröger

More information about the LibreOffice mailing list