Sax Basic to Python

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Sax Basic to Python

Tim AT Home
 
Hints please?
 
So, Im trying to convert some scripts from Sbasic to python - a small piece at first.
Now - I run some tables output and then run the following python block.
 
 
#*#' Objectives:
#*#'   MERGING STATS IF NECSSARY
#*#'   PAGE/PRINT OPTIONS
#*#'   TABLE NUMBERING AND TABLE OF CONTENTS
#*#'   CLEAN UP
#*
#*#'   MODIFIED DECEMBER 2007 TO REDUCE MANY FILES TO 1
#*#'   MODIFIED MAY 2009 Version 17
#*#'   MODIFIED MAY 2009 FOR PYTHON
#.
 

def OutputRoutine():
 
    import sys
   
    import spss
    #help (spss)
   
   
   
    # CALL & START SPSS CLIENT
    import SpssClient
    SpssClient.StartClient()
   
   
   
    # GET DESIGNATED OUTPUT DOC
    # If multiple output docs exist - get the list of output docs first and use a reference
    # MyOutputDocLists = SpssClient.GetOutputDocuments()
    # FirstOutputDoc = MyOutputDocLists.GetItemAt(0)
   
    try:
        MyOutputDoc = SpssClient.GetDesignatedOutputDoc()
        MyOutputItems = MyOutputDoc.GetOutputItems()
    except:
        pass
   
   
   
   
    # HERE FOR MERGING IF NECESSARY - MUST RUN BEFORE FORMATTING
   
   
   
   
    # SELECTS ALL FOR DELETION EXCEPT RELEVANT PIVOT TABLES AND ADDS PAGE BREAK TO REMAINING PIVOTS
    try:
        for index in range(OutputItems.Size()):
            MyOutputItem = MyOutputItems.GetItemAt(index)
            if MyOutputItem.GetType() == SpssClient.OutputItemType.PIVOT:
                MyPivotTable = MyOutputItem.GetSpecificType()
                sTitle = MyPivotTable.GetTitleText()
                if 'Pearson Chi-Square Test' in sTitle or 'Comparisons of Column' in sTitle:
                    MyOutputItem.SetSelected(True)
                else:
                    MyOutputItem.SetSelected(False)
                    MyOutputItem.SetPageBreak(True)
            else:
                MyOutputItem.SetSelected(True)
        MyOutputDoc.Delete()
    except:
        pass
   
 

    ## PRINT(PAGE) SETUP
 
    try:
        myor = MyOutputDoc.SpssClient.PrintOptions.Orientation
        mytm = MyOutputDoc.SpssClient.PrintOptions.TopMargin
        mylm = MyOutputDoc.SpssClient.PrintOptions.LeftMargin
        myrm = MyOutputDoc.SpssClient.PrintOptions.RightMargin
        mybm = MyOutputDoc.SpssClient.PrintOptions.BottomMargin
        mypn = MyOutputDoc.SpssClient.PrintOptions.StartingPageNumber
        print   myor,mytm,mylm,myrm,mybm,mypn
    except:
        pass
   
    #WANT TO BE ABLE TO PROBE FOR INPUT ABOUT PARAMETERS
 
    try:
        #myorI = 0
        #while myorI <> 1 or myorI <> 2:
        #   myorI = int(raw_input('Portrait=1 : Landscape=2 <<< ENTER CHOICE'))
        #MyOutputDoc.SetPrintOptions(Orientation,myorI)
       
        MyOutputDoc.SetPrintOptions(Orientation,2)
        MyOutputDoc.SetPrintOptions(LeftMargin,9)
        MyOutputDoc.SetPrintOptions(TopMargin,9)
        MyOutputDoc.SetPrintOptions(RightMargin,9)
        MyOutputDoc.SetPrintOptions(BottomMargin,9)
        MyOutputDoc.SetPrintOptions(StartingPageNumber,1)
        #MyOutputDoc.SetPrintOptions(SpaceBetweenItems,)
        #MyOutputDoc.SetPrintOptions(PrintedChartSize,)
    except:
        pass
   
   
   
   
   
   
    # CLOSE ANY&ALL SYNTAX DOCUMENTS
    try:
        MySyntaxDocsList = SpssClient.GetSyntaxDocuments()
        for index in range(MySyntaxDocsList.Size()):
                SpssSyntaxDocument.CloseDocument(index)
    except:
        pass
   
   
    # SAVE DESIGNATED OUTPUT DOCUMENT
    try:
        MyOutputDoc.SetPromptToSave(promptToSave)
        MyOutputDoc.SaveAs(r"c:\temp\flename.spv")
    except:
        pass
   
   
    # EXPORT AS PDF
    try:
        subSet = SpssClient.SpssExportSubset.SpssVisible
        format = SpssClient.SpssExportFormat.SpssForamtPdf
        fileName = r"c:\temp\flename.pdf"
        MyOutputDoc.ExportDocument(subSet,fileName,format)
    except:
        pass
   
   
    # END SPSS CLIENT
    SpssClient.StopClient()
   
 
 
It seems to run fine - I can see 'running etc...' in the SPSS windows and after short while see processor ready - I check the log and see
 
 
FROM LOG:
 
BEGIN PROGRAM PYTHON
 
import sys
sys.path.insert(0, r"C:\Work\Other\SPSS\Scripts")
import TWH0906OutputOptions
TWH0906OutputOptions.OutputRoutine()
 
END PROGRAM.               
 
 
BUT BUT - it doesnt do anything apparently - no formatting, no closing of syntax doc(s), no export of files
 
 
 

Thanks!

Tim

 

****************************

Notice: This e-mail and any attachments may contain confidential and privileged information.  If you are not the intended recipient, please notify the sender immediately by return e-mail, do not use the information, delete this e-mail and destroy any copies.  Any dissemination or use of this information by a person other than the intended recipient is unauthorized and may be illegal.  Email transmissions cannot be guaranteed to be secure or error free. The sender therefore does not accept any liability for errors or omissions in the contents of this message that arise as a result of email transmissions.

 
Reply | Threaded
Open this post in threaded view
|

Re: Sax Basic to Python

Peck, Jon

For starters, you are suppressing any error messages by using all those try blocks with pass as the except action.  Change them all to say

raise

instead of

pass

(or just remove them, but I assume you plan to do something with the caught errors eventually)

and you may see a helpful error message.

 

HTH,

Jon Peck

 


From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of Tim Hennigar
Sent: Friday, June 05, 2009 12:43 PM
To: [hidden email]
Subject: [SPSSX-L] Sax Basic to Python

 

 

Hints please?

 

So, Im trying to convert some scripts from Sbasic to python - a small piece at first.

Now - I run some tables output and then run the following python block.

 

 

#*#' Objectives:
#*#'   MERGING STATS IF NECSSARY
#*#'   PAGE/PRINT OPTIONS
#*#'   TABLE NUMBERING AND TABLE OF CONTENTS
#*#'   CLEAN UP
#*
#*#'   MODIFIED DECEMBER 2007 TO REDUCE MANY FILES TO 1
#*#'   MODIFIED MAY 2009 Version 17
#*#'   MODIFIED MAY 2009 FOR PYTHON
#.

 


def OutputRoutine():

 

    import sys
   
    import spss
    #help (spss)
   
   
   
    # CALL & START SPSS CLIENT
    import SpssClient
    SpssClient.StartClient()
   
   
   
    # GET DESIGNATED OUTPUT DOC
    # If multiple output docs exist - get the list of output docs first and use a reference
    # MyOutputDocLists = SpssClient.GetOutputDocuments()
    # FirstOutputDoc = MyOutputDocLists.GetItemAt(0)
   
    try:
        MyOutputDoc = SpssClient.GetDesignatedOutputDoc()
        MyOutputItems = MyOutputDoc.GetOutputItems()
    except:
        pass
   
   
   
   
    # HERE FOR MERGING IF NECESSARY - MUST RUN BEFORE FORMATTING
   
   
   
   
    # SELECTS ALL FOR DELETION EXCEPT RELEVANT PIVOT TABLES AND ADDS PAGE BREAK TO REMAINING PIVOTS
    try:
        for index in range(OutputItems.Size()):
            MyOutputItem = MyOutputItems.GetItemAt(index)
            if MyOutputItem.GetType() == SpssClient.OutputItemType.PIVOT:
                MyPivotTable = MyOutputItem.GetSpecificType()
                sTitle = MyPivotTable.GetTitleText()
                if 'Pearson Chi-Square Test' in sTitle or 'Comparisons of Column' in sTitle:
                    MyOutputItem.SetSelected(True)
                else:
                    MyOutputItem.SetSelected(False)
                    MyOutputItem.SetPageBreak(True)
            else:
                MyOutputItem.SetSelected(True)
        MyOutputDoc.Delete()
    except:
        pass
   

 


    ## PRINT(PAGE) SETUP

 

    try:
        myor = MyOutputDoc.SpssClient.PrintOptions.Orientation
        mytm = MyOutputDoc.SpssClient.PrintOptions.TopMargin
        mylm = MyOutputDoc.SpssClient.PrintOptions.LeftMargin
        myrm = MyOutputDoc.SpssClient.PrintOptions.RightMargin
        mybm = MyOutputDoc.SpssClient.PrintOptions.BottomMargin
        mypn = MyOutputDoc.SpssClient.PrintOptions.StartingPageNumber
        print   myor,mytm,mylm,myrm,mybm,mypn
    except:
        pass
   
    #WANT TO BE ABLE TO PROBE FOR INPUT ABOUT PARAMETERS

 

    try:
        #myorI = 0
        #while myorI <> 1 or myorI <> 2:
        #   myorI = int(raw_input('Portrait=1 : Landscape=2 <<< ENTER CHOICE'))
        #MyOutputDoc.SetPrintOptions(Orientation,myorI)
       
        MyOutputDoc.SetPrintOptions(Orientation,2)
        MyOutputDoc.SetPrintOptions(LeftMargin,9)
        MyOutputDoc.SetPrintOptions(TopMargin,9)
        MyOutputDoc.SetPrintOptions(RightMargin,9)
        MyOutputDoc.SetPrintOptions(BottomMargin,9)
        MyOutputDoc.SetPrintOptions(StartingPageNumber,1)
        #MyOutputDoc.SetPrintOptions(SpaceBetweenItems,)
        #MyOutputDoc.SetPrintOptions(PrintedChartSize,)
    except:
        pass
   
   
   
   
   
   
    # CLOSE ANY&ALL SYNTAX DOCUMENTS
    try:
        MySyntaxDocsList = SpssClient.GetSyntaxDocuments()
        for index in range(MySyntaxDocsList.Size()):
                SpssSyntaxDocument.CloseDocument(index)
    except:
        pass
   
   
    # SAVE DESIGNATED OUTPUT DOCUMENT
    try:
        MyOutputDoc.SetPromptToSave(promptToSave)
        MyOutputDoc.SaveAs(r"c:\temp\flename.spv")
    except:
        pass
   
   
    # EXPORT AS PDF
    try:
        subSet = SpssClient.SpssExportSubset.SpssVisible
        format = SpssClient.SpssExportFormat.SpssForamtPdf
        fileName = r"c:\temp\flename.pdf"
        MyOutputDoc.ExportDocument(subSet,fileName,format)
    except:
        pass
   
   
    # END SPSS CLIENT
    SpssClient.StopClient()
   

 

 

It seems to run fine - I can see 'running etc...' in the SPSS windows and after short while see processor ready - I check the log and see

 

 

FROM LOG:

 

BEGIN PROGRAM PYTHON
 
import sys
sys.path.insert(0, r"C:\Work\Other\SPSS\Scripts")
import TWH0906OutputOptions
TWH0906OutputOptions.OutputRoutine()
 
END PROGRAM.               

 

 

BUT BUT - it doesnt do anything apparently - no formatting, no closing of syntax doc(s), no export of files

 

 

 

Thanks!

Tim

 

 

****************************

Notice: This e-mail and any attachments may contain confidential and privileged information.  If you are not the intended recipient, please notify the sender immediately by return e-mail, do not use the information, delete this e-mail and destroy any copies.  Any dissemination or use of this information by a person other than the intended recipient is unauthorized and may be illegal.  Email transmissions cannot be guaranteed to be secure or error free. The sender therefore does not accept any liability for errors or omissions in the contents of this message that arise as a result of email transmissions.