Creating an index with mulitple pages for the same topic (solved)

Previous topic - Next topic

philippe.lavoie

In this forum, I posted a question on how to combine pages together to create an index like

Beef        5, 6
Chicken     3, 8

Scribus doesn't do this by default, so I created a script to do it. Maybe it can be useful to someone else too.


"""
Allows you to create an index section. It will sort the entries and combine multiple pages
belonging to the same entry.

This allows you to have something like

Beef        5, 6
Chicken     3, 8


@author: Philippe Lavoie
@version: 1.0 / 20170125
@copyright (c) 2017 Philippe Lavoie under the mit license
           http://www.opensource.org/licenses/mit-license.html
@bugs the sort is a text sort, so 10 might come before 2. I'll fix that later.
"""
import sys


try:
import scribus
except ImportError:
print 'Unable to import the scribus module. This script will only run within'
print 'the Python interpreter embedded in Scribus. Try Script->Execute Script.'
sys.exit(1)


if not  scribus.haveDoc():
scribus.messageBox('Error','You must have a document open',scribus.ICON_WARNING,scribus.BUTTON_OK)
sys.exit(2)


if scribus.selectionCount() == 0:
scribus.messageBox('Scribus - Script Error','There is no object selected. Please select a text frame and try again.',scribus.ICON_WARNING, scribus.BUTTON_OK)
sys.exit(2)


if scribus.selectionCount() > 1:  # get rid of or modify this section if you want more than one object selected
scribus.messageBox('Scribus - Script Error','You have more than one object selected. Please select one text frame and try again.', scribus.ICON_WARNING, scribus.BUTTON_OK)
sys.exit(2)

lines = [s.split('\t') for s in scribus.getText().splitlines()]
lines.sort()
newtext = ""

i = len(lines) - 1
while(i>=0):
entry=lines[i][0]
page = lines[i][1]
while i>0 and (lines[i][0] == lines[i-1][0]):
page = lines[i-1][1] + ", " + page
i = i-1
newtext = entry + "\t" + page + "\n" + newtext
i = i-1

scribus.setText(newtext)