Extracting All Text from a Document

From Scribus Wiki
Jump to: navigation, search
This article is part of the Scripts series.

This script will allow you to extract all text from all frames from within Scribus. In addition, it will list all pathnames for images in image frames.

When you start the script it will ask for a name for the text file to create. As soon as you enter this, it scans your Document and creates the file.

The original version relied on text frame names starting with 'Text' such as is the default, but this revision now tests for frame type. Frames other than text and image frames will be ignored.

Here is the script:

#!/usr/bin/env python
# File: extract_text.py - Extracts the text from a document, saving to a text file
# also lists image files with pathnames
# 2006.03.04 Gregory Pittman
# 2008.02.28 Petr Vanek - fileDialog replaces valueDialog
# this version 2008.02.28
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.

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,icon=0,button1=1)


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.', \
                       icon=0, button1=1)

Notes

  • If you do not have a document open when you run this script, it will fail, and nothing at all happens -- if you feel that it needs a feedback message, put one in. Similarly, this script does not test if you are running it from Scribus -- you can find examples of such code in the Scripts category.
  • The Scripter command
        d = scribus.getPageItems()

results in a tuple, a 3-item list about the frame, so that when we later test the frame: item[0] = frame's contents (or source of image), item[1] = frame type, and item[2] (not used here) = order on the page.

  • Sorry about the potential confusion about the list content and the variable contents. They are related, with contents serving as a temporary holding place for the text in the frame, where it is compared to the list content[]. If there is no match, then it's added to the list content, and also to the list T that comprises what will become the text file. If there is a match then the message about duplication is substituted.
  • Presumably, there is a limit to how much text can be held in a variable name, if nothing else a Python limit. This has not been tested.


Here is an example of the output from this script:


Page 1

header: Scribus
An Introductory Manual

body: A very brief intro
Scribus is an open source program for the production of professional layout targeted toward press-ready output, as well as other purposes PDFs may be used for, such as presentations and online document distribution like this manual. Begun as a personal project of Franz Schmid, it has attracted an increasing number of developers and users. The main sources of information will be found in the online manual included with the Scribus distribution, at docs.scribus.net, and at wiki.scribus.net. This manual is meant to collect together selected information from these sources in a format many have requested. Because Scribus is rapidly changing and improving, all of the sources of information, and especially this one may have errors or incomplete instructions, and in fact this is to be expected for development/unstable versions of Scribus. In general this manual will focus on the current stable version, now in the 1.3.3.x series, but additional notes may be included for 1.3.4+ when procedures and requesters substantially change.
Orientation
Some detailed explanations of DTP (Desktop Publishing) and its differences from wordprocessing can be found on the Scribus sites. Here we will merely point out that DTP is the design and layout of various elements on the workspace. Your workspace, the Document, is a 2-dimensional area on which you place the content with high precision. In the screenshot below, the blank white area whose border is lined in red and has some light blue margin guides is the workspace.
On this sample Document, two items or frames have been placed. The one with a picture in it is called an image frame, and to its left is a text frame. To the right of our Document is the Properties window, which you will find to be one of the most important if not essential tools for using Scribus. Note that the image frame has been selected, since it is outlined in a dotted red line. For whichever item is selected, the Properties window displays and allows the adjustment of its size, position, and many other properties.
The display gives the appearance that the image frame is resting on top of the text frame -- thus a workspace has its elements on various Levels in relationship to each other. Any new element added is laid on top of others, but with the Properties window, one can move frames and other items up or down Levels. Also note that our text flows around our image -- this too is selected in the Properties window. Furthermore, while not depicted in this screenshot, a Document may have one or more Layers, each Layer being a collection of items on different Levels. This should begin to give you a sense of the complexity of Scribus and DTP. If we have Levels, why do we need Layers? A more detailed explanation and demonstration is found in the tutorial in the Wiki, but for this manual let us sum it up by saying that the Layers allow you to work on selected elements without disturbing others.

Image3: /home/downloads/Scribus/MainScreen.png

Page 2

Text4: Duplication, perhaps linked-to frame


We see a listing of page number, then frames in order, with the name of the frame followed by a colon, then its contents. In this example, Text4 is a linked-to frame, in which case its contents would have been identical to the frame body:, so we test for this and substitute this message for its content. If two separate frames have the same contents this will also happen, thus the message's conditional 'perhaps'.

Script Variant

For those with an aversion to coming to grips with Python and modifying a script, here is a version that will only look for the image frames, and output that to a file:

#!/usr/bin/env python
# File: extract_imageframes.py - Extracts image frame pathnames from a document, saving to a text file
# 2010.06.15 minor mod to eliminate checks for text frames, only image frames
# 2006.03.04 Gregory Pittman
# 2008.02.28 Petr Vanek - fileDialog replaces valueDialog
# this version 2008.02.28
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.

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] == 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,icon=0,button1=1)


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.', \
                       icon=0, button1=1)

Notes

  • If you do not have a document open when you run this script, it will fail, and nothing at all happens -- if you feel that it needs a feedback message, put one in. Similarly, this script does not test if you are running it from Scribus -- you can find examples of such code on the Scripts page.
  • The Scripter command
        d = scribus.getPageItems()