Pyhton Automation via SPSS - Success Story

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

Pyhton Automation via SPSS - Success Story

Mike P-5
 A while back I posted I was trying to automate a large process in SPSS
using the Python and the integration package, I am happy to say that I
have succeeded in doing this.  I am posting this so that hopefully
others can take pieces of the code I have used to help create there own
scripts, so that in the future we can all benefit.

Purpose of the program:
The purpose of this programme was automate the creation of CSV files in
order to run a VBA macro to create custom PowerPoint presentations.  The
automation was needed because there could be up to 20 different CSV
files that needed to be produced.

The Program works by running a filter variable, that produces a
dataset,(dataset_filter) on which a syntax file is run and the output is
sent to an XML file via OMS.  It then runs a python specific program
using XLST to convert the XML into CSV format.  The dataset is then
closed and the next filter is applied. In this instance I have left out
the call for running the VBA macro, but it would be a simple addition at
the end of the code.

My thanks go out to Jon Peck for his time and efforts in helping to make
this programme possible, and who's expert knowledge was invaluable

For the other modules that I am calling please feel free to email me for
a copy, (i.e. the XLST code for converting XML tables into CSV) although
I will be giving Jon a copy, which with any luck will be made available
in http://www.spss.com/devcentral/

Mike



begin program.
import spss,spssaux
import os

file_name = 'z:/projects/surveys/50 - Orange channel profiling/SPSS
main/Channel Specific/Channel specfic breakdowns.sps'
f = open(file_name,"r")
file_string = f.read()
f.close()

#RUN THE FILTER NEEDED AROUND THE FILE.

varList =
['news','Entertainment','Communicate','Lifestyle','Games','Music','Money
','Celebrity','Shopping','Sport','Film','Travel','Cars','Chat','BetPlayW
in']
for i in varList:

        spss_filter = '''DATASET COPY Dataset_filter.
        DATASET ACTIVATE Dataset_Filter.
        FILTER OFF.
        USE ALL.
        SELECT IF(%s= 1).
        EXECUTE .''' % (i)

        spss_oms = '''OMS
         /SELECT TABLES
         /IF COMMANDS = ["CTables"]
         SUBTYPES = ["Custom Table"]
         /DESTINATION FORMAT = OXML
         OUTFILE = "Z:/projects/surveys/50 - Orange channel
profiling/SPSS main/Channel Specific/datatable_%s.xml".''' % (i)

        spss_close = '''DATASET ACTIVATE Dataset2.
        DATASET close Dataset_filter.'''

        spss.Submit(spss_filter)
        spss.Submit(spss_oms)
        spss.Submit(file_string)
        spss.Submit('''OMSEND.''')

        cmd = 'xsltproc.exe "C://Documents and
Settings/Michael.Pearmain/Desktop/xsl_test/gen_csv.xsl"
"Z:/projects/surveys/50 - Orange channel profiling/SPSS main/Channel
Specific/datatable_%s.xml"' % (i)
        f = os.popen(cmd)
        x = f.read()
        f.close ()

        g = open("z:/projects/surveys/50 - Orange channel profiling/SPSS
main/Channel Specific/CSV_%s.csv" % (i),"w")
        g.write(x)
        g.close()

        spss.Submit(spss_close)

        #next iteration

end program.


______________________________________________________________________
This email has been scanned by the MessageLabs Email Security System.
For more information please visit http://www.messagelabs.com/email
______________________________________________________________________