[scribus] centering text vertically in a frame

Gregory Pittman gpittman at iglou.com
Thu Jul 5 01:58:49 UTC 2012

Here is a script I wrote which accomplishes this task, albeit with some 
The main one is that you must be using fixed linespacing. This seems to 
be because when you use the getLineSpacing(), Scribus has no idea you 
are using anything else.

The script works by getting the linespacing, the number of lines of 
text, the height of the frame, then using 1/2 the difference between the 
frame height and total space taken by lines, resets the Top distance.

The other curious thing I haven't figured out is that when the script 
runs it dutifully makes the new Top distance, but the text stays put. I 
couldn't figure out any command in Scripter that would fix this. What I 
did find out is that if you save the file, close it, and reload, the 
text has moved to the middle. But it's not just a display issue, since 
if you save to PDF before fixing in some way, the PDF shows the text 
I did find other workarounds, such as setting the Line Color or Fill 
Color to something else, then back to where it was, then the frame is 
redrawn and the text moves like it's should have.
Also, clicking the Edit Shape button in Properties, then closing fixed it.

In the end, the simplest thing to do which works is to just run the 
script again, in which case THEN the text is shifted. (weird)

So, try it out, find other ways it doesn't work (I'm sure if you have 
mixed up fonts in the frame it won't do a very good job then either).


*****script below*****
#!/usr/bin/env python
# -*- coding: utf-8  -*-
# centervert.py

     import scribus
except ImportError:
     print "Unable to import the 'scribus' module. This script will only 
run within"
     print "the Python interpreter embedded in Scribus. Try 
Script->Execute Script."

if not scribus.haveDoc():
     scribus.messageBox('Scribus - Script Error', "No document open", 
scribus.ICON_WARNING, scribus.BUTTON_OK)

if scribus.selectionCount() == 0:
     scribus.messageBox('Scribus - Script Error',
             "There is no object selected.\nPlease select a text frame 
and try again.",
             scribus.ICON_WARNING, scribus.BUTTON_OK)
if scribus.selectionCount() > 1:
     scribus.messageBox('Scribus - Script Error',
             "You have more than one object selected.\nPlease select one 
text frame and try again.",
             scribus.ICON_WARNING, scribus.BUTTON_OK)
textbox = scribus.getSelectedObject()
pageitems = scribus.getPageItems()

for item in pageitems:
     if (item[0] == textbox):
         if (item[1] != 4):
             scribus.messageBox('Scribus - Script Error',
                           "This is not a textframe. Try again.", 
scribus.ICON_WARNING, scribus.BUTTON_OK)

         lines = scribus.getTextLines(textbox)
         distances = scribus.getTextDistances(textbox)
         linespace = scribus.getLineSpacing(textbox)
         dimensions = scribus.getSize(textbox) # (width, height)
         if (scribus.textOverflows(textbox, 1) > 0):
             scribus.messageBox('User Error', "This frame is already 
overflowing", scribus.ICON_WARNING, scribus.BUTTON_OK)
         newtopdist = (dimensions[1] - linespace * lines)/2


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.scribus.net/pipermail/scribus/attachments/20120704/3ce1750c/attachment.html>

More information about the scribus mailing list