Add Outline Levels 1 and 2 to Indexes at Levels 1/2 using Py

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
monosij.dr
Posts: 5
Joined: Sat Nov 05, 2016 6:19 am

Add Outline Levels 1 and 2 to Indexes at Levels 1/2 using Py

Post by monosij.dr »

Hello All -

I am using LibreOffice 5.2 on Ubuntu and trying to see how to best solve the following issue using Python3.

I would like add Outline Levels 1/ 2 to four (4) Indexes I have. This is so the entry items in the 4 Indexes, when inserted at Level 3, show up under the Outline Levels 1/2 added at Levels 1/2 in those Indexes. Thus it would be useful to know where in the document the Index entries appear.

I have been able to do this manually to get the desired effect.

...

However, it would be better done programmatically through a macro and I am trying to see if I could best do it from Python3 as that is what I use mainly.

The following pseudocode is what I am generally contemplating:

- Check the 4 Indexes and if any item in those Indexes appear at Levels 1/2 then move them to Level 3, if they are not in Outline Levels 1/2.
- For each Outline Level 1/2 add to the 4 Indexes at Levels 1/2 as an Index item.
- My outline levels all end with semi-colon so I can do further checks before moving them or discarding them.

I am planning on using the XDocumentIndexes. I am assuming there is an interface to the Outline Index or some other interface to access the Outlines also.

If I could get please get some pointers on how to best use the API to access the structures inside the document I would much appreciate.

If there is a link in the forum or such you could connect me to, will be great. I have searched to no avail yet.

Also I assume the API is universal regardless of LibreOffice or OpenOffice?

Thank you for your help and suggestions.

Mono
Last edited by monosij.dr on Tue Nov 08, 2016 1:39 am, edited 1 time in total.
LibreOffice 5.3, Ubuntu 16.04
User avatar
Villeroy
Volunteer
Posts: 31269
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Add Outline Levels 1 and 2 to Indexes at Levels 1/2 usin

Post by Villeroy »

With MRI you should find your path through the jungle.
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
monosij.dr
Posts: 5
Joined: Sat Nov 05, 2016 6:19 am

Re: Add Outline Levels 1 and 2 to Indexes at Levels 1/2 usin

Post by monosij.dr »

Hi Villeroy - thank you fr your message but does this work in LibreOffice? Also will I need to always use it or just initially to generate necessary code? I am hoping i can access it directly through a code interface.

I will wait to see if I hear from others on accessing directly from Python through Uno-bridge.

Much thanks.

Mono
LibreOffice 5.3, Ubuntu 16.04
User avatar
Villeroy
Volunteer
Posts: 31269
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Add Outline Levels 1 and 2 to Indexes at Levels 1/2 usin

Post by Villeroy »

The latest version 1.3.3 from the OpenOffice site works fine with LibreOffice: http://extensions.openoffice.org/en/pro ... ction-tool
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
FJCC
Moderator
Posts: 9248
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: Add Outline Levels 1 and 2 to Indexes at Levels 1/2 usin

Post by FJCC »

Villeroy is absolutely right that using MRI will help you understand the methods and properties relevant to indexes. I used it to take a look at a document with only a table of contents. MRI recorded this Python code to get to the index, where oInitialTarget is the document model.

Code: Select all

from com.sun.star.beans import UnknownPropertyException
from com.sun.star.container import NoSuchElementException
from com.sun.star.lang import WrappedTargetException
from com.sun.star.uno import RuntimeException

def snippet(ctx, oInitialTarget):
    """  """
    try:
        oDocumentIndexes = oInitialTarget.getDocumentIndexes()
        obj1 = oDocumentIndexes.getByName("Table of Contents1")
        
    except NoSuchElementException as e:
        # getByName
        print(e)
    except WrappedTargetException as e:
        # getByName, getPropertyValue
        print(e)
    except UnknownPropertyException as e:
        # getPropertyValue
        print(e)
    except RuntimeException as e:
        # getByName
        print(e)
I don't see any obvious way to move individual entries within the index. There are three properties that seem to define how to construct the index. The columns are the Property name, Value Type, Value, and Handle.

Code: Select all

CreateFromChapter               boolean                     False                                              1008  
CreateFromLevelParagraphStyles  boolean                     False                                              1052  
CreateFromMarks                 boolean                     True                                               1005  
CreateFromOutline               boolean                     True                                               1006  
,
I suppose you could set CreateFromMarks to TRUE and insert index marks at every necessary location and then manipulate the indexing level of those marks, but that seems difficult impractical.
OpenOffice 4.1 on Windows 10 and Linux Mint
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
monosij.dr
Posts: 5
Joined: Sat Nov 05, 2016 6:19 am

Re: Add Outline Levels 1 and 2 to Indexes at Levels 1/2 usin

Post by monosij.dr »

Thank you FJCC and Villeroy. Before I try installing MRI I will try and see if I can get this code to work to get through my Indexes. I hope it should work. Much thanks for your code to see how I can 'look through' my documents. I will try to keep the post here open for a bit more to see if I get any more responses.
LibreOffice 5.3, Ubuntu 16.04
FJCC
Moderator
Posts: 9248
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: Add Outline Levels 1 and 2 to Indexes at Levels 1/2 usin

Post by FJCC »

Looking at this more, I think what you want to do is modify the OutlineLevel of the text and not manipulate the Index, other than updating it. This code, in Basic, changes the OutlineLevel of all of the Heading 2 text to 3 and updates the table of contents.

Code: Select all

oDoc = ThisComponent
SDesc = oDoc.createSearchDescriptor()
SDesc.SearchStyles = TRUE
SDesc.SearchString = "Heading 2"
oFound = oDoc.findAll(SDesc)
for i = 0 to oFound.Count - 1
	TextRng = oFound.getByIndex(i)
	TextRng.OutlineLevel = 3
next i

oDocIndexes = oDoc.DocumentIndexes
Tbl = oDocIndexes.getByName("Table of Contents1")
Tbl.update()
Normally, I would just change the text style to Heading 3, rather than changing only the OutlineLevel. You should consider that, though I understand you may have reasons for using another approach.
OpenOffice 4.1 on Windows 10 and Linux Mint
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
monosij.dr
Posts: 5
Joined: Sat Nov 05, 2016 6:19 am

Re: Add Outline Levels 1 and 2 to Indexes at Levels 1/2 usin

Post by monosij.dr »

Hi FJCC -

Thank you again but the issue I have is getting the Outline Levels 1/2 into the Indexes. So that each item in each Index appears in the relative position of the Outline Levels. I will enter each item in each Index at Outline Level 3 when I enter it. If there was a way to define each Index with Outline Levels just as a TOC is defined but with the option of adding 1 or 2 or however many Levels would have been ideal.
LibreOffice 5.3, Ubuntu 16.04
Post Reply