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 CellTextallows you to omit thespssprefix when specifyingCellTextobjects (discussed below), once you have imported thespssmodule. - You set categories after you add dimensions, so the
SetCategoriesmethod calls follow theAppendorInsertmethod calls. - The first argument to
SetCategoriesis 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 withAppendorInsert, as discussed in the previous topic. - The second argument to
SetCategoriesis 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
CellTextobjects 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.