All pastes #1705718 Raw Edit

Miscellany

public python v1 · immutable
#1705718 ·published 2009-12-07 17:01 UTC
rendered paste body
#!/usr/bin/env python# -*- coding: utf-8 -*-#ABOUT THIS SCRIPT:#Import CSV data files as tables into Scribusimport systry:	# Please do not use 'from scribus import *' . If you must use a 'from import',	# Do so _after_ the 'import scribus' and only import the names you need, such	# as commonly used constants.	import scribusexcept ImportError,err:	print "This Python script is written for the Scribus scripting interface."	print "It can only be run from within Scribus."	sys.exit(1)########################## YOUR IMPORTS GO HERE  ##########################import csvdef getCSVdata():	"""opens a csv file, reads it in and returns a 2 dimensional list with the data"""	csvfile = scribus.fileDialog("csv2table :: open file", "*.csv")		if csvfile != "":		try:			reader = csv.reader(open(csvfile, 'r'), delimiter=';')			datalist=[]			for row in reader:				rowlist=[]				for col in row:					rowlist.append(col)				datalist.append(rowlist)			return datalist		except Exception,  e:			scribus.messageBox("csv2table", "Could not open file %s"%e)	else:		sys.exitdef create(argv):	"""	    - create a page using the "badge" master page.	    - add the text field with the name	    - add the text with the role in the project	"""	#########################	#  YOUR CODE GOES HERE  #	#########################	userdim=scribus.getUnit() #get unit and change it to mm	scribus.setUnit(scribus.UNIT_POINTS)		data = getCSVdata()		scribus.progressTotal(len(data))	scribus.setRedraw(False)		name_x = [240, 620, 240, 620] 	name_y = [100, 100, 360, 360]	surname_x = [240, 620, 240, 620]	surname_y = [160, 160, 420, 420]	lbg_x = [240, 620, 240, 620] 	lbg_y = [220, 220, 480, 480]	width = 157	height = 27			i = 0	position = 0	for row in data:		#if i > 0 :		#sys.exit(1)		# scribus.messageBox("csv2table", row)		#scribus.messageBox("csv2table", row[0])				# create and fill the text boxes				#name		textbox=scribus.createText(name_x[position], name_y[position], width, height)		scribus.insertText(row[0], 0, textbox)		scribus.setTextColor('Black', textbox);		scribus.setFont('Times New Roman Bold', textbox);		scribus.setFontSize(22, textbox);		scribus.setTextAlignment(scribus.ALIGN_CENTERED, textbox);		#surname		textbox=scribus.createText(surname_x[position], surname_y[position], width, height)		scribus.insertText(row[1], 0, textbox)		scribus.setTextColor('Black', textbox);		scribus.setFont('Times New Roman Bold', textbox);		scribus.setFontSize(22, textbox);		scribus.setTextAlignment(scribus.ALIGN_CENTERED, textbox);		#lbg		textbox=scribus.createText(lbg_x[position], lbg_y[position], width, height)		scribus.insertText(row[2], 0, textbox)		scribus.setTextColor('Black', textbox);		scribus.setFont('Times New Roman Bold', textbox);		scribus.setFontSize(22, textbox);		scribus.setTextAlignment(scribus.ALIGN_CENTERED, textbox);		i = i + 1;		position = position + 1 if position < 3 else 0		# create a new page		if position == 0: 			scribus.newPage(-1, 'badge')		scribus.progressSet(i)			scribus.progressReset()	scribus.setUnit(userdim) # reset unit to previous value	scribus.docChanged(True)	scribus.statusMessage("Done")	scribus.setRedraw(True)def main(argv):	#The main() function disables redrawing, sets a sensible generic	#status bar message, and optionally sets up the progress bar. It then runs	#the main() function. Once everything finishes it cleans up after the create()	#function, making sure everything is sane before the script terminates.	try:		scribus.statusMessage("Importing .csv table...")		scribus.progressReset()		create(argv)	finally:		# Exit neatly even if the script terminated with an exception,		# so we leave the progress bar and status bar blank and make sure		# drawing is enabled.		if scribus.haveDoc():			scribus.setRedraw(True)		scribus.statusMessage("")		scribus.progressReset()# This code detects if the script is being run as a script, or imported as a module.# It only runs main() if being run as a script. This permits you to import your script# and control it manually for debugging.if __name__ == '__main__':	main(sys.argv)