FLOSS Manuals

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

Python Scripting with Scribus

Creating Text Styles

We certainly encourage the use of Paragraph and Character Styles in Scribus. Aside from easily applying a number of font and paragraph features all at once, you also can subsequently edit your style and have any changes applied document-wide at the same time. It makes sense, then, that we may want to create these styles on the fly as we go through a script. It is possible to import styles from one document to another, but you may need to refresh your memory about where some particular style is located.

So, the short answer is that yes, we can create styles in Scripter. At the start, it's helpful to know that for most purposes there are two components you must create. The first is a Character Style, which denotes the font, its size, and then a large number of features, including its fill color and stroke color.

After that, you then create a Paragraph Style, which includes the name of the Character Style you wish to use, then such things as linespacing, alignment, spacing between paragraphs, indentation, and so on.

This is about as simple as it can be:

    scribus.createCharStyle("titlechar", "DejaVu Sans Condensed Bold", 18)

For our Character Style we have only specified its name, the font, and the font size. For the Paragraph Style we really only wanted to specify its name, the linespacing mode (in this case automatic), and the associated Character Style, but we can't truncate the elements between the linespacing mode and the name of the Character Style.

Recently, someone wanted a script with created styles, and specified that he wanted the kerning or tracking to be -5.0%. Easy enough to say, but...

Let's look at what the online manual says.


Creates a character style. This function takes the following keyword parameters:

  • "name" [required] -> name of the char style to create
  • "font" [optional] -> name of the font to use
  • fontsize [optional] -> font size to set (double)
  • "features" [optional] -> nearer typographic details can be defined by a string that might contain the following phrases comma-separated (without spaces!):
    • inherit
    • bold
    • italic
    • underline
    • underlinewords
    • strike
    • superscript
    • subscript
    • outline
    • shadowed
    • allcaps
    • smallcaps
  • "fillcolor" [optional], "fillshade" [optional] -> specify fill options
  • "strokecolor" [optional], "strokeshade" [optional] -> specify stroke options
  • baselineoffset [optional] -> offset of the baseline
  • shadowxoffset [optional], shadowyoffset [optional] -> offset of the shadow if used
  • outlinewidth [optional] -> width of the outline if used
  • underlineoffset [optional], underlinewidth [optional] -> underline options if used
  • strikethruoffset [optional], strikethruwidth [optional] -> strikethru options if used
  • scaleh [optional], scalev [optional] -> scale of the chars
  • tracking [optional] -> tracking of the text
  • "language" [optional] -> language code

As you can see, tracking is next to last in the options. I thought I could just count down the bullets, maybe skipping the white bullets to see which one was tracking. I thought it was 13th. After days of trial and error, mostly error, I finally looked at this list and considered that, for example, fillcolor and fillshade are actually two options, and so going through the list again, maybe tracking is 19th? This turned out to be correct, but then, how do you set it for -5%? Answer: by playing with numbers. Meanwhile, there was also the trick of deciding what value to put for an option that you really didn't want to set.

The end result of all this consternation was this:

    scribus.createCharStyle("keychar", "DejaVu Sans Condensed Bold", 7.0,'','Black',1.0,'',0,0,0,0,0,0,0,0,0,1,1,-50)

So the 4th and 7th elements of createCharStyle() needed to be null strings. The sixth, fillshade was 1.0 for 100%. The 17th and 18th are 1 for 100% (horizontal and vertical scales), and the others could be zeros. But look at number 19, the kerning/tracking! Somehow -50 translates to -5.0%!

From what I can tell, if I wanted something like underlining, that 4th element null string would be 'underline'.

Applying a Paragraph Style

I had thought that applying a style to a frame would be easy, something like on the main canvas, where you select a frame, apply the style, and you are done! When I tried this in a script, the Paragraph Style was only applied to the last paragraph, or in other words, from the end of the text to the first newline character that was encountered.

The workaround, since I was making a number of styles and applying them, was to create a function like this, to not just select the frame but select all the text in it:

def SelectAllText(textframe):
    texlen = scribus.getTextLength(textframe)

Functions go at the top of your script, right after all the import statements. So then down in the script, after creating the styles I could then just:

    scribus.setStyle('keystyle', key)

Something I haven't tried is to see what would happen if I applied a style to a frame BEFORE it had content – would this work?

An unanticipated bonus

To my happy surprise, there was a side benefit to creating and using styles. If you recall, I mentioned that some of these color files contained thousands of colors. What the script does is to create a list of the color names, and corresponding lists of the values used to create these colors in Scribus. After the colors are created in Scribus, a document is created with small color swatches for each color, 49 per page, with a text frame key underneath showing these color values that made each color. Some of these files made a document over 100 pages, and consequently might take over an hour to finish.

Once I had these styles, instead of applying the individual font features to the labels, I applied a style. The result was that even the largest color files only took a number of minutes to complete. The smaller ones took only a few seconds. So here is a clear additional reason to use styles in scripts. I should add, too, that you don't have to create the styles with Python. You might create your styles on the main canvas, or import them from some document, then in your script simply use them.

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

You should refresh this page.