Running Python Scripts from Python Programs

You can run Python scripts from Python programs and you can call Python script methods from within a Python program. This allows you to write Python programs that operate on user interface and output objects.

Example: Calling a Python Script from a Python Program

This example shows a Python program that creates a custom pivot table and calls a Python script to make the column labels of the table bold.

BEGIN PROGRAM.
import spss, MakeColsBold
spss.StartProcedure("Demo")
table = spss.BasePivotTable("Sample Table","OMS subtype")
table.SimplePivotTable(rowlabels = ["1","2"],
                       collabels = ["A","B"],
                       cells = ["1A","1B","2A","2B"])
spss.EndProcedure()
MakeColsBold.Run("Sample Table")
END PROGRAM.

The content of the MakeColsBold module is as follows:

import SpssClient

def Run(tableName):
   SpssClient.StartClient()
   OutputDoc = SpssClient.GetDesignatedOutputDoc()
   OutputItems = OutputDoc.GetOutputItems()
   for index in range(OutputItems.Size()):
      OutputItem = OutputItems.GetItemAt(index)
      if OutputItem.GetType() == SpssClient.OutputItemType.PIVOT \
        and OutputItem.GetDescription() == tableName:
         PivotTable = OutputItem.GetSpecificType()
         ColumnLabels = PivotTable.ColumnLabelArray()
         for i in range(ColumnLabels.GetNumColumns()):
            ColumnLabels.SelectLabelAt(1,i)
         PivotTable.SetTextStyle(SpssClient.SpssTextStyleTypes.SpssTSBold)
   SpssClient.StopClient()

Example: Calling Python Scripting Methods Directly from a Python Program

This example shows a Python program that creates a custom pivot table and makes direct calls to Python scripting methods to make the title of the table italic.

BEGIN PROGRAM.
import spss, SpssClient
spss.StartProcedure("Demo")
table = spss.BasePivotTable("Sample Table","OMS subtype")
table.SimplePivotTable(cells = ["A","B","C","D"])
spss.EndProcedure()

SpssClient.StartClient()
OutputDoc = SpssClient.GetDesignatedOutputDoc()
OutputItems = OutputDoc.GetOutputItems()
OutputItem = OutputItems.GetItemAt(OutputItems.Size()-1)
PivotTable = OutputItem.GetSpecificType()
PivotTable.SelectTitle()
PivotTable.SetTextStyle(SpssClient.SpssTextStyleTypes.SpssTSItalic)
SpssClient.StopClient()
END PROGRAM.