I don't have time to actually test this in Scribus itself, but here's a
cleaned up version which (should) fix things.
In case the crash is coming from non-unicode text being input, I added a
correction to avoid that. I've also cleaned up the while loop a bit to
make it more readable and run faster. Also, we are advised to always
tell Scribus that our code is utf-8 so that starts the script.
I added a file open check so the script quits when the input text file
cannot be found and some other little cleanup stuff. Also made the
horizontal position of the text box into a variable rather than hard-coded.
See if this works any better. <g>
Pat
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# ---------------------------------------------------#
# script to import text file into scribus
#----------------------------------------------------#
import sys
try:
import scribus
except ImportError:
print "This script only works from within Scribus"
sys.exit(1)
#----------------------------------------------------#
# declareer variabelen #
#----------------------------------------------------#
VerticalPosition = 100 # y of (x,y) position for text box
HorizontalPosition = 60 # x of (x,y) position for text box
Width = 325.99 # text box width
Height = 50 # initial text box height
VerticalMarge = 10 #afstand tussen de tekstboxen
TextFile='' # tekst file dat geimporteerd moet worden
Page = 0 # bladzijde, we beginnen bij 0
Add = ''
OverflowChar = 0 #overflowing characters, we will adjust textbox
height until this is 0
#----------------------------------------------------#
# actie #
#----------------------------------------------------#
if scribus.newDoc(scribus.PAPER_A5, (51.02,42.52,42.52,53.86),
scribus.PORTRAIT, 1, scribus.UNIT_POINTS, scribus.FACINGPAGES,
scribus.FIRSTPAGERIGHT):
# laad stylen
try:
TextFile = open('/home/pat/workspace/scribusTut/test.txt', 'r')
except:
print "Cannot open text file\n"
sys.exit(1)
# determine how big a text box can be allowed to grow maxBoxHeight)
pageWidth, pageHeight = scribus.getPageSize()
topMargin, leftMargin, rightMargin, bottomMargin = scribus.getPageMargins()
maxBoxHeight = pageHeight - topMargin - bottomMargin
for line in TextFile:
#[c] The following commented out code could be used if text files cannot be
trusted to not contain
#[c] trailing white space or if users might break lines arbitrarily, adding
paragraph breaks we don't want.
#[c]
#[c] line = line.rstrip() # strip trailing white space
plus \n (also \r, I suppose)
#[c] if len(line) > 0:
#[c] Add = Add + line + ' ' # add one trailing space so continued
lines won't run together
#[c] else:
#[c] Add = Add.rstrip()+'\n' # take away final trailing space &
add \n to end this paragraph
if len(line) > 1:
Add = Add + unicode(line,'iso-8859-2') # make sure text is
unicode for Scribus
else:
#make textbox
TextBox = scribus.createText(HorizontalPosition, VerticalPosition,
Width, Height)
VerticalPosition=VerticalPosition+VerticalMarge+Height
#vul de textbox
scribus.setText(Add, TextBox)
#check if alles well
OverflowChar = scribus.textOverflows(TextBox)
while OverflowChar > 0:
Height=Height+100
if Height > maxBoxHeight:
scribus.sizeObject(Width,maxBoxHeight,TextBox)
break # ugly, but we have to stop somehow
else:
scribus.sizeObject(Width,Height,TextBox)
OverflowChar = scribus.textOverflows(TextBox)
Add = '' # reset in case this gets put into a multi-file
while loop later on
TextFile.close() # clean up after ourselves