Import CSV Data: Difference between revisions
(Created page with "{{Scripting Index}} 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...") |
No edit summary |
||
Line 2: | Line 2: | ||
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 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 | 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 come out of that. | |||
<pre> | |||
#!/usr/bin/env python | |||
# -*- coding: utf-8 -*- | |||
# importcsv.py | |||
# created December 11, 2012 | |||
# 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) | |||
csvfiledata = scribus.fileDialog('CSV file', 'CSV files(*.csv)') | |||
with open(csvfiledata, 'rb') as csvfile: | |||
census = csv.reader(csvfile) | |||
for row in census: | |||
scribus.setStyle('csv',textbox) | |||
scribus.insertText('\t'.join(row) + '\n', -1, textbox) | |||
</pre> |
Revision as of 22:57, 11 December 2012
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 come out of that.
#!/usr/bin/env python # -*- coding: utf-8 -*- # importcsv.py # created December 11, 2012 # 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) csvfiledata = scribus.fileDialog('CSV file', 'CSV files(*.csv)') with open(csvfiledata, 'rb') as csvfile: census = csv.reader(csvfile) for row in census: scribus.setStyle('csv',textbox) scribus.insertText('\t'.join(row) + '\n', -1, textbox)