Difference between revisions of "Extracting All Text from a Document"

From Scribus Wiki
Jump to: navigation, search
(Notes)
(Notes)
Line 46: Line 46:
 
</pre>
 
</pre>
 
===Notes===
 
===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 to see if you are running it from Scribus -- you can find examples of such code on the {{Category:Scripts & Plugins}} page.
+
* 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 to see if you are running it from Scribus -- you can find examples of such code on the [[Category:Scripts_&_Plugins|Scripts & Plugins]] page.
 
* The Scripter command
 
* The Scripter command
 
<pre>
 
<pre>

Revision as of 16:41, 27 October 2007

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: frameslist.py
# 2006.03.04  Gregory Pittman
# this version 2007.10.27

import scribus 

if scribus.haveDoc():
    textfile = scribus.valueDialog('Filename','Enter name of file to save to\n".txt" will be appended')
    textfile = textfile + '.txt'

    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)

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 to see if you are running it from Scribus -- you can find examples of such code on the page.
  • 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.

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 subtitute this message for its content. If two separate frames have the same contents this will also happen, thus the message's conditional 'perhaps'.