Getting Started with Python Scripts
The basic structure of a Python script is:
import SpssClient
SpssClient.StartClient()
<Python language statements>
SpssClient.StopClient()
- The import SpssClient statement imports the Python module containing the IBM® SPSS® Statistics classes and methods available in the Python scripting interface.
- SpssClient.StartClient() provides a connection to the associated IBM SPSS Statistics client, enabling the script to retrieve information from the client and to perform operations on objects managed by the client, such as pivot tables. Whether the script connects to an existing client or starts up a new client depends on how the script was invoked. See the topic Introduction to Python Scripts for more information.
- SpssClient.StopClient() terminates the connection
to the IBM SPSS Statistics client
and should be called at the completion of each Python script.
Note: If you're running a Python script from an external Python process that starts up a new client, call SpssClient.Exit() before SpssClient.StopClient().
Example
This script accesses the designated output document and sets each of the pivot tables as selected.
import SpssClient
SpssClient.StartClient()
OutputDoc = SpssClient.GetDesignatedOutputDoc()
OutputItems = OutputDoc.GetOutputItems()
for index in range(OutputItems.Size()):
OutputItem = OutputItems.GetItemAt(index)
if OutputItem.GetType() == SpssClient.OutputItemType.PIVOT:
OutputItem.SetSelected(True)
SpssClient.StopClient()
Target for Standard output
The Python print statement writes output to Python's standard output. When you run a Python script from Utilities>Run Script, Python's standard output is directed to a log item in the IBM SPSS Statistics Viewer.