Step 2: Defining Categories (Python)
There are two ways to define categories for each dimension: explicitly, using the SetCategories method, or implicitly when setting values. The explicit method is shown here. The implicit method is shown in Step 3: Setting Cell Values (Python).
Example
from spss import CellText
table = spss.BasePivotTable("Table Title",
"OMS table subtype")
coldim=table.Append(spss.Dimension.Place.column,"coldim")
rowdim1=table.Append(spss.Dimension.Place.row,"rowdim-1")
rowdim2=table.Append(spss.Dimension.Place.row,"rowdim-2")
cat1=CellText.String("A1")
cat2=CellText.String("B1")
cat3=CellText.String("A2")
cat4=CellText.String("B2")
cat5=CellText.String("C")
cat6=CellText.String("D")
cat7=CellText.String("E")
table.SetCategories(rowdim1,[cat1,cat2])
table.SetCategories(rowdim2,[cat3,cat4])
table.SetCategories(coldim,[cat5,cat6,cat7])
- The statement
from spss import CellText
allows you to omit thespss
prefix when specifyingCellText
objects (discussed below), once you have imported thespss
module. - You set categories after you add dimensions, so the
SetCategories
method calls follow theAppend
orInsert
method calls. - The first argument to
SetCategories
is an object reference to the dimension for which the categories are being defined. This underscores the need to save references to the dimensions you create withAppend
orInsert
, as discussed in the previous topic. - The second argument to
SetCategories
is a single category or a sequence of unique category values, each expressed as a CellText object (one ofCellText.Number
,CellText.String
,CellText.VarName
, orCellText.VarValue
). When you specify a category as a variable name or variable value, pivot table display options such as display variable labels or display value labels are honored. In the present example, we use string objects whose single argument is the string specifying the category. - It is a good practice to assign variables to the
CellText
objects representing the category names, since each category will often need to be referenced more than once when setting cell values.

Note: Generation of the resulting table requires more code than is shown here.