My extension script won't work. Plese help!

Discuss SVG code, accessible via the XML Editor.
lazygeorge
Posts: 8
Joined: Wed Mar 02, 2011 8:00 am

My extension script won't work. Plese help!

Postby lazygeorge » Thu Apr 07, 2011 7:29 pm

Hi,

I have spent a few weeks now getting to grips with writing extensions and I am getting stuck on this one:
The script is very simple, it places text on the page using data from a csv file. The problem is it gives me an error at the point in the script where I try to set the text style.

Here is the the code that it doesn't like:

Code: Select all

import inkex, simplestyle, simplepath
import csv
import os
import os.path
import sys

class csvtext(inkex.Effect):

 def __init__(self):
   inkex.Effect.__init__(self)
   self.OptionParser.add_option("-f", "--fontsize",
     action="store", type="string",
     dest="fontsize", default="20",
     help="Size of node label numbers")
   self.OptionParser.add_option("-d", "--datafile",
     action="store", type="string",
     dest="datafile", default="C:\data.csv",
     help="The csv file")


 def effect(self):
  csvfile = self.options.datafile
  data = csv.reader(open(csvfile, "rb")) 
  # Read the column names from the first line of the file 
  #fields = data.next() 
  for row in data: 
   x = row[0]
   y = row[1]
   text = row[2]
   new = inkex.etree.SubElement((parent,inkex.addNS('textPath','svg'))
   s = {'font-size': self.options.fontsize, 'fill-opacity': '1.0', 'stroke': 'none',
    'font-weight': 'normal', 'font-style': 'normal', 'fill': '#999'}
   new.set('style', simplestyle.formatStyle(s))
   new.set('x', str(x))
   new.set('y', str(y))
   new.text = str(text)

if __name__ == '__main__':
 e = csvtext()
 e.affect()


When I try to run it I get this:

File "textfromcsv.py", line 49
new.set('style', simplestyle.formatStyle('font-size': self.options.fontsize, 'fill-opacity': '1.0', 'stroke': 'none','font-weight': 'normal', 'font-style': 'normal', 'fill': '#999'))
^
SyntaxError: invalid syntax

So it seems to have problem with the setting text style. I just can't see what's wrong with the syntax. There isn't a problem with the csv file i don't think.
It's probably something simple, can anyone help?

If anyone would like to try the extension, here is the inx file:

Code: Select all

<inkscape-extension>
    <_name>Text From CSV</_name>
   <id>org.ekips.filter.csvtext</id>
   <dependency type="executable" location="extensions">textfromcsv.py</dependency>
   <dependency type="executable" location="extensions">inkex.py</dependency>


   <param name="fontsize" type="string" _gui-text="Font size">20</param>
   <param name="datafile" type="string" _gui-text="Data file:">C:\data.csv</param>
    <param name="extraVarInfo" type="description">The data file must be CSV with comma separated columns</param>
    <param name="sep" type="description">Make sure: Column1=X Column2=Y Column3=Text </param>

    <effect>
      <object-type>all</object-type>
                <effects-menu>
                    <submenu _name="Text"/>
                </effects-menu>
    </effect>
    <script>
        <command reldir="extensions" interpreter="python">textfromcsv.py</command>
    </script>
</inkscape-extension>

lazygeorge
Posts: 8
Joined: Wed Mar 02, 2011 8:00 am

Re: My extension script won't work. Plese help!

Postby lazygeorge » Thu Apr 07, 2011 8:00 pm

Sorry, just me being dopey - There was an extra bracket in the line previous!


Return to “SVG / XML Code”