FLOSS Manuals

 English |  Español |  Français |  Italiano |  Português |  Русский |  Shqip

Python Scripting with Scribus

ExtractText.py

#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""

SYNOPSIS

This script takes the current document and extracts all the text from text frames,
and also gets the pathnames to all images. This is then saved to a file named
by the user.

USAGE

Start the script. A file dialog appears for the name of the file
to save to. The above information is saved to the file.

"""

import scribus


def exportText(textfile):
    page = 1
    pagenum = scribus.pageCount()
    T = []
    content = []
    while (page <= pagenum):
        scribus.gotoPage(page)
        d = scribus.getPageItems()
        strpage = str(page)
        T.append('Page '+ strpage + '\n\n')
        for item in d:
            if (item[1] == 4):
                contents = scribus.getAllText(item[0])
                if (contents in content):
                    contents = 'Duplication, perhaps linked-to frame'
                T.append(item[0]+': '+ contents + '\n\n')
                content.append(contents)
            elif (item[1] == 2):
                imgname = scribus.getImageFile(item[0])
                T.append(item[0]+': ' + imgname + '\n')
        page += 1
        T.append('\n')
    output_file = open(textfile,'w')
    output_file.writelines(T)
    output_file.close()
    endmessage = textfile + ' was created'
    scribus.messageBox("Finished", endmessage, scribus.ICON_NONE, scribus.BUTTON_OK)


if scribus.haveDoc():
    textfile = scribus.fileDialog('Enter name of file to save to', \
                                  filter='Text Files (*.txt);;All Files (*)')
    try:
        if textfile == '':
            raise Exception
        exportText(textfile)
    except Exception, e:
        print e

else:
    scribus.messageBox('Export Error', 'You need a Document open, and a frame selected.', \
                       scribus.ICON_NONE, scribus.BUTTON_OK)

The idea behind this script was that you may have been working on a document, and separate from the document itself you just want to save its content. You might want to run the text by someone for review, maybe even someone that doesn't have or use Scribus. Initially, I only wrote this to extract the text and save that to a file, but later decided to also get the filenames of the images and save that also.

At the top of the script is a function called exportText, so the action begins down lower, at if scribus.haveDoc(). Here is where we ask the user for a filename to save the results, using a fileDialog(), so that we have not only the filename, but a path. After this, the real work begins by accessing exportText.

Just as we have done before, we get a pageCount(), then getPageItems() on the page and go through them. This time we not only are interested in text frames, but also image frames. You might wonder what the "order" of frames is. In this example, it will be the order in which they were created for the page the script is working on. As we go through the text and image frames, we are creating a list, T, which starts with the text 'Page ' and the number of the current page, after which is appended the text from text frames, and the image file names. In regard to the text frames, though, look at this:

                contents = scribus.getAllText(item[0])
                if (contents in content):
                    contents = 'Duplication, perhaps linked-to frame'

Before we append our text frame content to the list T we look to see if it is a duplicate of something already there. I found out early in the development of this script that if you had a series of linked frames, each time to accessed one of the frames in that series, you got all the text from the linkage. So to keep the file from being repetitive, I substituted this message, 'Duplication, perhaps linked-to frame.' The reason I say perhaps is that it is possible someone might have two or more frames with identical content, even though not linked. There might be some more sophisticated way to look for linked frames, but it wasn't clear this would add any great utility to the script.

Finally, when we've finished, we send an appropriate message. This is a script that is included with Scribus, though you won't find it in the menu with Scripter > Scribus Scripts. Depending on your installation, it is located in a directory called samples.

There has been error in communication with Booktype server. Not sure right now where is the problem.

You should refresh this page.