SpssDataCells Class (Python)

The SpssDataCells object provides access to the data cells of a pivot table. In most pivot tables, the data cells contain the results of the statistical analysis. You need to use the SpssDataCells object if you want to highlight significant values in the output (for example, making bold all correlation coefficients that are greater than a specified value) or to retrieve specific statistics from the output (for example, the means and standard deviations of each group or variable).

The SpssDataCells object represents a 2-dimensional array of the data cells you can view in a pivot table. If there are no layer dimensions, all of the cells will be accessible; otherwise, the table must be pivoted in order to fully access the data currently in layer dimensions.

The data cells array has the same number of rows as the row labels array and the same number of columns as the column labels array. That is to say, row indexes for the row labels and column indexes for the column labels respectively correspond to the row and column indexes for the data cells.

Note: If the current table has been set to display blocks of rows--either using SET ROWSBREAK or by checking Display the table as blocks of rows on the Pivot Tables tab of the Options dialog box--then methods of the SpssDataCells class that access specific cells, such as GetTextColorAt, will only have access to the first block of rows. Exceptions to this behavior are the GetValueAt, SetValueAt, and GetUnformattedValueAt methods, which can access all rows of the pivot table, regardless of whether the table is displayed in blocks of rows.

You get an SpssDataCells object from the DataCellArray method of an SpssPivotTable object, as in:

SpssDataCells = SpssPivotTable.DataCellArray()

Example: Modifying Specific Cells

This example assumes that PivotTable is an SpssPivotTable object, and sets the background color to red for all data cells containing a value below 0.01.

DataCells = PivotTable.DataCellArray()
for i in range(DataCells.GetNumRows()):
   for j in range(DataCells.GetNumColumns()):
      try:
         val = float(DataCells.GetValueAt(i,j))
         if  val < 0.01:
            DataCells.SetBackgroundColorAt(i,j,255)
      except:
         pass
  • The value returned from GetValueAt is a unicode string. If the value is a representation of a numeric value, it is converted to a float, otherwise an exception is raised and control passes to the except clause. Since the except clause only contains a pass statement, execution continues.