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.
#!/usr/bin/env python # -*- coding: utf-8 -*- # importcsv.py # created December 11, 2012 # Gregory Pittman """ 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. """ 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)
- The first thing to see is the need to import csv at the beginning.
- Next note that about 2/3 of the script is error-checking to see that you have a document open, have a frame selected, only one frame, and that it is a text frame.
- Once this is established, then the current contents of the frame are deleted, and I apply my previously created style csv to the frame.
- fileDialog is then used to search for the CSV file, screening for files that end in .csv, after which the lines are loaded into the list census – I'm using this for my hospital census, thus the name.
- Finally, we process the list line by line. The command insertText is used so that the lines can be appended (see the '-1' parameter for insertText).
- Something I didn't necessarily expect was that I had to apply the style to the frame before inserting the text.
- This could have also been written to apply specific font features, instead of styles. The advantage of styles is for me that I am sometimes using Windows computers and sometimes Linux, and there is a mismatch between available fonts. With the script as it is, I can adjust the styles in the template document in each computer, and run the same script in each.