[Scribus] Scribus script (2nd try)

Craig Ringer craig
Wed Dec 15 08:24:09 CET 2004

Hi Kobus

That script looks pretty handy. In answer to your question about a
better way to do it, I'm really not sure there is - everybody's needs
vary so much with automatic article layout that it's hard to write
something to do it right for everybody. That's the sort of job the
Python interface is for :-)

When it comes to getting the image size, there is a way - it just
requires the use of an external library, which is unfortunate. If you
get the Python Imaging Library from pythonware.com, you'll be able to
not only discover the image size but scale it, colour correct it, etc.
If you changed it you'd need to save the new image to a file though, as
Scribus doesn't support storing image data in the main document.

The script you list seems pretty sensible. I've written some information
that might be helpful for script authors on bug 1317
(http://bugs.scribus.net/view.php?id=1317) . I'd be interested to know
what you think.

A few notes:

Rather than asking users to comment out the font line, you're probably
better off catching the exception. If you're running a very recent CVS

    setFont("Arial Bold")
except NotFoundError,err:
    print "Couldn't set text font: %s" % err

NotFoundError was only recently defined, so if you have an older
snapshot use:

    setFont("Arial Bold")
except ScribusError,err:
    print "Couldn't set text font: %s" % err

That'll work fine on both older and newer versions, but is less
specific. setFont() was changed to throw NotFoundError, but
NotFoundError is a subclass of ScribusError so if you catch ScribusError
you'll also catch NotFoundError.

I'm not entirely sure why you're reading in the paragraphs one at a time
and inserting them. It'd be interesting to know why.

Also, a minor point: In Python, anywhere a function takes a boolean
value it's usually more readable to say True or False than 1 or 0.

The script looks good. I'm afraid I can't (yet) help you with the string
width issue, but I agree it's needed and it's something I do want to get
sorted out.

I'd probably do something a little more structured for the article file
personally, but I'm into over-designing things. A similar program I
wrote a while ago used headers like this:

Title: Boiling good
Author: Ugg Ugg the Chef
Image: 20041112/submitted/ex_snowpeas.tiff
Caption: The crime he committed upon these poor snow peas.

article text here after blank line.
You don't boil snow peas! It's not right!

It's easy to read this in Python. See following example script:

#!/usr/bin/env python
# -*- coding: ascii -*-

filename = "article.txt"

# You could read this from a magic first header
# on the file if you like, but we're just setting
# it for now.
file_encoding = "utf-8"

def main():
    article = open(filename)
    headers = {}

    # Read the headers into the dictionary.

    # We're not using the much nicer
    # for line in article:
    # because we don't want the read-ahead done by
    # the file iterator.

    while True:
        line = article.readline()
        if line.strip() == "":
        header, value = line.split(":",1)
        header = unicode(header.strip(), file_encoding)
        value  = unicode(value.strip(),  file_encoding)
        headers[header] = value

    # The file "article" is now positioned so that the rest
    # of the text read will be the body of the article. We
    # can read it to a string, or in some cases just use
    # it in place. For demonstration's sake we'll read it to
    # a unicode string.

    article_text = unicode(article.read(), file_encoding)

    # Now print out the headers and article. You'd probably format
    # this in Scribus as desired instead. Note that the
    # headers are in a dictionary, which has no order to its
    # items, so they'll be printed out in an undefined order.
    # We could sort the keys first if we cared.

    for header,value in headers.items():
        print "%-15s: %s" % (header,value)

    print article_text

# Only run main() if we're being run as a script, not imported
# as a module.
if __name__ == '__main__':

More information about the scribus mailing list