FLOSS Manuals

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

Python Scripting with Scribus

Python and strings

As you may notice in this book, there are number of useful ways to work with strings in Python. In Scripter, valueDialog() and fileDialog() both return a string. The message you put into a messageBox() must be a string. If what you actually need from the user is an integer, you need to do something like this:

value = scribus.valueDialog('Input Needed', 'How many times to do this task?', '1')
value = int(value)

and in reverse, for a messageBox():

counter = 7
scribus.messageBox('Results', 'The current counter reading is ' + str(counter) + '\nGood Job!', button = scribus.BUTTON_OK)

With this message you can also see how using '\n' (this is a string denoting a newline), or maybe '\t' (for tab) can improve the appearance of the message.

In many cases we may need to manipulate our string input even though it will remain a string. There are a number of built-in string methods in Python, which I will make use of. One of these is split.

values = '20 34 55 61 23 7'
pages = values.split()

The variable name pages denotes a list of the individual pages, though these are still string values, and if we wanted to use them as integers, they would need to be converted.

Sometimes, we either want a string to be in upper case or in lower case, so these are easily accomplished.

response = 'yes'
response.upper()   # response is now 'YES'
Response = 'No'
Response.lower()  # response is now 'no'

A reason for doing this is to simplify some conditional argument, and not need to cover all possible entries.

A recent script that I wrote began by taking lines from a file. Each line consisted of a name of a color, and then separated by tabs were 3 floating point numbers, so a line might look like this:

30-478    39,52    60,42    38,39

The name of the color here is 30-478, but after that we can see that these numbers use a comma as the decimal separator, which Python will have trouble managing as a number.

    content = line.split('\t')
    fields = len(content)
    if (fields == 4):
        colors.append(content[0])
        Lval = content[fields - 3]
        Lval = re.sub(r',' , '.',Lval)
        Lval = float(Lval)
        L.append(Lval)
        aval = content[fields - 2]
        aval = re.sub(r',' , '.',aval)
        aval = float(aval)
        a.append(aval)
        bval = content[fields - 1]
        bval = re.sub(r',' , '.',bval)
        bval = float(bval)
        b.append(bval)

The first line is using the split() method, and notice how we indicate that the separator is a tab with '\t'. Next we take the precaution in these files, where there might be thousands of colors, that the number of bits of data is 4 as it should be, so len(content) returns the number of elements in the list content. A reason why this file used a tab as a separator is that some of the color names had spaces in them, so this would have been a tricky matter to deal with a space separator. For the float values then we start with the third from the last element of fields, and then using the Python re package, we use its sub method to replace (r) a comma with a period. This may be hard to decipher, since inside the parentheses we have an r, then a comma inside single quotes, then a comma separator, then a period inside single quotes. Once we make this switch, we can then convert this string to a float value. I will also add that at the top of this script we needed a line import re for the Python regex package to work.

The characters in a string can also be accessed in part or individually, since you can manage them like a list. The command len(string) may help you select some particular set of characters by finding out its length.

value = 'Some lengthy and arbitrary string'
value[0]   # this is the first character in the string: S
value[-1]  # the last character: g
value[-4]  # the fourth from the last character: r
value[2:]  # all the characters from the third to the end: me lengthy and arbitrary string
value[:4}  # all the characters from the beginning up to the fifth: Some  (includes the space after the word)

While you can't do this directly with numbers, you can, as we did in the script postnet.py convert the number correctnum to a string, so that we could see whether the last digit was a '0'. But we could have also done this:

remainder = correctnum%10

where the '%' is the mathematical operator for modulus, in other words we find the remainder after dividing by 10. If the integer correctnum is divisible by 10, then remainder is 0.

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

You should refresh this page.