Create tables out of csv data


 * Version distributed with scribus (june 2014)
 * Improved version (june 2014)

Distributed version (as of june 2014)
""" ABOUT THIS SCRIPT:
 * 1) !/usr/bin/env python
 * 2) -*- coding: utf-8 -*-

Import CSV data files as tables into Scribus

1st create any frame with the desired table size on your page

make shure it is selected

execute this script you will be prompted for a csv filename

the data from the csv file will be imported and a table of textboxes will be drawn on the page.



LICENSE:

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

Author: Sebastian Stetter

please report bugs to: scribusscript@sebastianstetter.de """

from __future__ import division import sys

try: # 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 scribus except 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)

import csv
 * 1) YOUR IMPORTS GO HERE  #
 * 1) YOUR IMPORTS GO HERE  #

def getAriaInformation: if scribus.selectionCount == 1: AI = dict areaname = scribus.getSelectedObject areaposition= scribus.getPosition(areaname) AI["vpos"] = areaposition[1] AI["hpos"] = areaposition[0] areadimensions = scribus.getSize(areaname) AI["vsize"]=areadimensions[1] AI["hsize"]=areadimensions[0] scribus.deleteObject(areaname) return AI   else: scribus.messageBox("csv2table", "please select ONE Object to mark the drawing area for the table") sys.exit
 * 1) get information about the area where the bale should be drawed

def 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(file(csvfile)) 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.exit
 * 1) get the cvs data

def getDataInformation(list): """takes a 2 dimensional list object and returns the numbers of rows and cols""" datainfo = dict datainfo["rowcount"]=len(list) datainfo["colcount"]= len(list[0]) return datainfo

def cellsize(areainfo, datainfo): """"takes the area and data info and calculates the prper cell size"""   csize=dict    csize["v"]= areainfo["vsize"] / datainfo["rowcount"]    csize["h"]= areainfo["hsize"] / datainfo["colcount"]    return csize

def main(argv): """This is a documentation string. Write a description of what your code   does here. You should generally put documentation strings ("docstrings")    on all your Python functions.""" #########################   #  YOUR CODE GOES HERE  # #########################   userdim=scribus.getUnit #get unit and change it to mm    scribus.setUnit(scribus.UNIT_MILLIMETERS) ai = getAriaInformation data = getCSVdata di= getDataInformation(data) cs = cellsize(ai,di) vcellsize = cs["v"] hcellsize = cs["h"] hposition=ai["hpos"] vposition=ai["vpos"] objectlist=[] # here we keep a record of all the created textboxes so we can group them later i=0 scribus.progressTotal(len(data)) scribus.setRedraw(False) for row in data: for cell in row: textbox=scribus.createText(hposition, vposition, hcellsize, vcellsize) #create a textbox objectlist.append(textbox) scribus.insertText(cell,0, textbox)#insert the text into the textbox hposition=hposition+hcellsize #move the position for the next cell vposition=vposition+vcellsize #set vertical position for next row hposition=ai["hpos"] #reset vertical position for next row i=i+1 scribus.progressSet(i) #scribus.groupObjects(objectlist) #pack thre whole stuff togehter ##############This causes scribus to render veeeery slowly!!! scribus.progressReset scribus.setUnit(userdim) # reset unit to previous value scribus.docChanged(True) scribus.statusMessage("Done") scribus.setRedraw(True)

def main_wrapper(argv): """The main_wrapper 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 main    function, making sure everything is sane before the script terminates.""" try: scribus.statusMessage("Importing .csv table...") scribus.progressReset main(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

if __name__ == '__main__': main_wrapper(sys.argv)
 * 1) This code detects if the script is being run as a script, or imported as a module.
 * 2) It only runs main if being run as a script. This permits you to import your script
 * 3) and control it manually for debugging.

Improved version
Improved by Jean-Paul Gendner (f5bu.fr)

Changelog : hposition=hposition+cellwidthleft #move the position for the next cell
 * I corrected a bug. When first column width was different from others, cells where not juxtaposed properly. Error is in the following line, where I replaced 'cellwidthleft' by 'cellsize' :

However, different values may be entered. Of course, in this case the table size may be different from the selected frame.
 * I allow specifying a width for each column (only two in original version) and a height for each row (only one in original version).
 * The default values for the widths and heights are calculated to get a table with the size of the selected frame. If different values are entered, the default values are updated to finally respect the overall size.
 * Ability to affect directly a style to all cells. Style must exist when launching the script.
 * Possibility to define a top distance for the texts (distance between top of textbox and text).

So much more possibilities are covered: defining width of each column and height of each row knowing the overall size of the planed table or knowing individual cells values.

""" ABOUT THIS SCRIPT (modified by J.-P. Gendner June, 15 2014):
 * 1) !/usr/bin/env python
 * 2) -*- coding: utf-8 -*-

Import CSV data files as tables into Scribus

1. You may create a style to be applied to all cells of the table

2. Create a frame at the position (upper left corner) and of the size (height and width) you want for your table

3. Make sure it is selected

4. Execute this script:

You will first be prompted for the csv data filename. -> The number of columns and row will be displayed. You will then be prompted for the width of each column in mm. ->  The default value given is calculated to respect the width of the table. Then you will be prompted for the height of each row. -> The default value is calculated to respect the height of the table. Then you may enter the style name you want to be applied to each cell. Finally, you may enter the Top distance for all text.

5. The data from the csv file will be imported and a table of textboxes will be drawn on the page.

LIMITATIONS:

In Scribus versions 1.3.5svn, when the script ends, you cannot adjust text, colors, and line features for a group, whereas in 1.3.3.x, all of these can be done without ungrouping.

HINTS:

Postgresql: You can easily create a CSV file with a Postgresql database. From Postgresql, toggle unaligned output with the '\a' switch, then activate a comma as a separator with '\f ,' (without apostrophes). Send output to a file with '\o myfile.csv', then query your database.

Sqlite3: You can use "sqlite3 -csv" in the command line or ".mode csv" in sqlite's interactive shell.



LICENSE:

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.

Author: Sebastian Stetter

Modifications: Gregory Pittman

please report bugs to: scribusscript-xpvPi5bcW5WP0LLXB2dsMhvVK+yQ3ZXh@public.gmane.org """

from __future__ import division import sys

try: # 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 scribus except 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)

import csv
 * 1) YOUR IMPORTS GO HERE  #
 * 1) YOUR IMPORTS GO HERE  #

def getPosition: if scribus.selectionCount == 1: areaname = scribus.getSelectedObject position= scribus.getPosition(areaname) vpos = position[1] hpos = position[0] size = scribus.getSize(areaname) width = size[0] height = size[1] scribus.deleteObject(areaname) return vpos, hpos, width, height else: scribus.messageBox("csv2table", "please select ONE Object to mark the drawing area for the table") sys.exit
 * 1) get information about the area where the bale should be drawed

def 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(file(csvfile)) 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.exit
 * 1) get the cvs data

def getDataInformation(list): """takes a 2 dimensional list object and returns the numbers of rows and cols""" datainfo = dict datainfo["rowcount"]=len(list) datainfo["colcount"]= len(list[0]) return datainfo

def cellsize(areainfo, datainfo): """"takes the area and data info and calculates the prper cell size"""   csize=dict    csize["v"]= areainfo["vsize"] / datainfo["rowcount"]    csize["h"]= areainfo["hsize"] / datainfo["colcount"]    return csize def main(argv):    """This is a documentation string. Write a description of what your code does here. You should generally put documentation strings ("docstrings") on all your Python functions."""   #########################    #  YOUR CODE GOES HERE  #    #########################    userdim=scribus.getUnit #get unit and change it to mm    scribus.setUnit(scribus.UNIT_MILLIMETERS)    cellwidthleft = 0    cellwidthright = 0    pos = getPosition    hposition=pos[1]    vposition=pos[0]    width = pos[2]    height = pos[3]    data = getCSVdata    di= getDataInformation(data)    ncol = len(data[0])    nrow = len(data)    scribus.messageBox("Table", "   " + str(ncol) + " columns,    " + str(nrow) + " rows   ")	#jpg    ColWidthList = []    TableWidth = 0    RowHeightList = []    TableHeight=0    i=0    for row in data:      if i==0:	c=0        for cell in row:          ColWidthT = scribus.valueDialog('Column Width','How wide (mm) do you wish column ' +str(c+1) + ' to be?',str((width-TableWidth)/(ncol-c)))	  ColWidth = float(ColWidthT)          ColWidthList.append(ColWidth) TableWidth = TableWidth + ColWidth c=c+1 RowHeightT = scribus.valueDialog('Row Height','How tall (mm) do you wish row ' + str(i+1) +' to be?',str((height-TableHeight)/(nrow-i))) RowHeight = float(RowHeightT) RowHeightList.append(RowHeight) TableHeight= TableHeight + RowHeight i=i+1 CellsStyle = scribus.valueDialog('Cells Style','Style name or blank?',"") TextDist = float(scribus.valueDialog('Text distance','Top distance for text (mm)?',"0")) objectlist=[] # here we keep a record of all the created textboxes so we can group them later i=0 scribus.progressTotal(len(data)) scribus.setRedraw(False) for row in data: c=0 for cell in row: cell = cell.strip cellsize=ColWidthList[c] cellHeight=RowHeightList[i] textbox=scribus.createText(hposition, vposition, cellsize, cellHeight) #create a textbox objectlist.append(textbox) if len(CellsStyle) > 0: scribus.setStyle(CellsStyle,textbox) if TextDist > 0: scribus.setTextDistances(0,0,TextDist,0,textbox) scribus.insertText(cell,0, textbox)#insert the text into the textbox hposition=hposition+cellsize #move the position for the next cell c=c+1 vposition=vposition + cellHeight	#set vertical position for next row hposition=pos[1] #reset vertical position for next row i=i+1 scribus.progressSet(i) scribus.deselectAll scribus.groupObjects(objectlist) scribus.progressReset scribus.setUnit(userdim) # reset unit to previous value scribus.docChanged(True) scribus.statusMessage("Done") scribus.setRedraw(True)

def main_wrapper(argv): """The main_wrapper 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 main    function, making sure everything is sane before the script terminates.""" try: scribus.statusMessage("Importing .csv table...") scribus.progressReset main(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 > 0: scribus.setRedraw(True) scribus.statusMessage("") scribus.progressReset

module. your script if __name__ == '__main__': main_wrapper(sys.argv)
 * 1) This code detects if the script is being run as a script, or imported as a
 * 1) It only runs main if being run as a script. This permits you to import
 * 1) and control it manually for debugging.