FLOSS Manuals

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

Python Scripting with Scribus

The very beginnings

Just what is a Python script anyway? Python is one of the programming languages which relies on a real-time compiler for its implementation. What this means on a practical level is that you create a plain-text file, which you then "run" by calling up the Python interpreter. Let's look at this very simple Python script, to be run outside of Scribus.

#!/usr/bin/env python
print "Hi! What's up?"

Using a plain-text editor, which in Linux might be vi, or vim, or emacs, or kwrite, gedit, among others. In Windows, I might use Notepad. The important thing is that I don't want an editor that formats the text in any way. So I make this in the editor, then save as "Hi.py". You don't have to use this .py extension, but it will help you find Python scripts if you do, and you might as well save them in a directory named python, so you can find them later. So now what do you do?

Answer: you "run" it. In a command line setting, you type

python Hi.py

and press RETURN, assuming you're in the directory where it is located. What you have done is to call up the Python interpreter, and pointed it to the text file you want it to interpret. You should then see on the next line

Hi! What's up?

All it does is print this message. Exciting, huh? Let's talk about some other elemental things for your script. Look at these modifications.

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Hi.py - gives a simple message
print "Hi! What's up?"

Notice how the first three lines begin with a '#'. Generally speaking, this denotes a comment, in other words, a line that will not be interpreted as Python by the compiler. The exception is the first line, which begins with '#!', called 'she-bang', and what follows on that line is where your computer should look for the compiler to run this script. You actually wouldn't need to specify python in the command line if you can changed the permissions of this file to allow it to be executed. In that case you would just put on the command line


and it would run. The next line is something mainly used in Linux, which explains that the character encoding of the script is UTF-8, mainly important for indicating new lines in the script. The third line is informational, indicating the name of the script file, and what it does, not so necessary in a simple script like this but very useful in a large script you haven't looked at in a while. You can have as many comments as you want in a script, and a line doesn't have to begin with a '#', but the compiler is going to ignore anything to the right of the '#' on that line.

So far we haven't begun to think about Python and Scribus, but let's move on to that.

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

You should refresh this page.