Import CSV Data

I recently wanted to incorporate Scribus into a daily task in my work. I had been doing this using Postgresql, in which case I would output the information I wanted as text in columns, then use a2ps to convert that to PostScript.

I was using a laptop for that, but then I began using a tablet, much easier to carry around. Unfortunately, I couldn't find a suitable database app for the purpose, but then it occurred to me that Scribus can import CSV files, and automatically line up the information in columns. This would only require a simple text editor, and for that purpose I am currently using a vim app.

Since this is a repetitive operation, I wanted to automate the document creation as much as possible, so I wrote a script that would rewrite my header with the current date. Unfortunately, there are no Scripter commands for importing a CSV file, so I was manually importing, then resetting the style, since CSV import creates its own style. I mentioned this on the mail list, after which it was pointed out that Python has capabilities to handle CSV files, both writing and reading. This article shows the script that came out of that.

importcsv.py
""" USAGE You must have a document open, and a text frame selected. Simply run the script, which asks for a CSV file with a file dialog. Note that any text in the frame will be deleted before the text from the CSV file is added. The script also assumes you have created a style named 'csv', which it will apply to the frame. """
 * 1) !/usr/bin/env python
 * 2) -*- coding: utf-8  -*-
 * 3) importcsv.py
 * 4) created December 11, 2012
 * 5) Gregory Pittman

import csv try: 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." sys.exit(1)

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

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) sys.exit(2) 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) sys.exit(2)

textbox = scribus.getSelectedObject ftype = scribus.getObjectType(textbox)

if (ftype != "TextFrame"): scribus.messageBox('Scribus - Script Error', "This is not a textframe. Try again.", scribus.ICON_WARNING, scribus.BUTTON_OK) sys.exit(2)

scribus.deleteText(textbox) scribus.setStyle('csv',textbox)

csvfiledata = scribus.fileDialog('CSV file', 'CSV files(*.csv)') with open(csvfiledata, 'rb') as csvfile: census = csv.reader(csvfile) for row in census: scribus.insertText('\t'.join(row) + '\n', -1, textbox)