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.
- This feature is only available when running a Python program from
the IBM® SPSS® Statistics client--within
a
BEGIN PROGRAM-END PROGRAM
block in command syntax or within an extension command. It is not available when running a Python program from an external Python process. - When running Python scripting code from a Python program in distributed mode, you may need to configure your firewall to allow access from the remote server to which you are connected.
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.
- Python programs use the interface exposed by the Python
spss
module, so the first line of the program contains animport
statement for that module. The Python script is assumed to be contained in a Python module namedMakeColsBold
, so theimport
statement also includes that module. - The code from
spss.StartProcedure
tospss.EndProcedure
creates a pivot table titled "Sample Table". -
MakeColsBold.Run("Sample Table")
calls theRun
function in theMakeColsBold
module and passes the value "Sample Table" as the argument. TheRun
function implements the Python script to make the column labels of the specified table bold.
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()
- The
import SpssClient
statement is needed to access the classes and methods available in the Python scripting interface. - The module contains a single function named
Run
, which implements the script. It takes a single argument that specifies the name of the table to modify. There is nothing special about the name Run and the module is not limited to a single function. You can create a module that contains many functions, each of which implements a different script. - The
Run
function callsSpssClient.StartClient()
to provide a connection to the associated IBM SPSS Statistics client andSpssClient.StopClient()
to terminate the connection at the completion of the script.
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.
- The
import spss, SpssClient
statement provides access to the classes and methods available for Python programs (spss
) as well as those for Python scripts (SpssClient
). - The code from
spss.StartProcedure
tospss.EndProcedure
is the Python program code that creates the pivot table. - The code from
SpssClient.StartClient()
toSpssClient.StopClient()
is the Python script code that makes the title italic.