How to modify SPSS output files with Python?
Question: I'm making custom tables in SPSS, but when the cell values (percentages) are
rounded to 1 decimal, they sometimes add up to 99,9 or 100,1 in stead of
100,0. My boss asked my to have everything neatly add up to 100. This means
slightly changing some values in the output tables.

I wrote some code to retrieve cell values from tables, which works fine, but I
cannot find any method or class that allows me to change cells in already
generated output. I've tried things like :

Table[(rij,6)] = CellText.Number(11)

and

SpssDataCells[(rij,6)] = CellText.Number(11)

but it keeps giving me "AttributeError: 'SpssClient.SpssTextItem' object has
no attribute 'DataCellArray'"

How do I succesfully change cell values of output tables in SPSS?

My code so far:

    
    
    import SpssClient, spss
    
    # Python verbinden met SPSS.
    SpssClient.StartClient()
    OutputDoc = SpssClient.GetDesignatedOutputDoc()
    OutputItemList = OutputDoc.GetOutputItems()
    
    # Laatste tabel pakken.
    lastTab = OutputItemList.Size()-2
    OutputItem = OutputItemList.GetItemAt(lastTab)
    Table = OutputItem.GetSpecificType()
    SpssDataCells = Table.DataCellArray()
    
    # For loop. Voor iedere rij testen of de afgeronde waarden optellen tot 100.
    # Specifieke getallen pakken.
    rij=0
    try:
    while (rij<20):
       b14 = float(SpssDataCells.GetUnformattedValueAt(rij,0))
       z14 = float(SpssDataCells.GetUnformattedValueAt(rij,1))
       zz14 = float(SpssDataCells.GetUnformattedValueAt(rij,2))
       b15 = float(SpssDataCells.GetUnformattedValueAt(rij,4))
       z15 = float(SpssDataCells.GetUnformattedValueAt(rij,5))
       zz15 = float(SpssDataCells.GetUnformattedValueAt(rij,6))
       print [b14,z14,zz14,b15,z15,zz15]
       rij=rij+1
    except:
      print 'Einde tabel'
    


Answer: The
[`SetValueAt`](http://www-01.ibm.com/support/knowledgecenter/SSLVMB_21.0.0/com.ibm.spss.statistics.python.help/python_scripting_spsslabels_setvalueat.htm?lang=en)
method is what you require to change the value of a cell in a table.

_PS. I think your boss should focus on more important things than to spend
billable time on having percentages add up neatly to 100% (due to rounding).
Also ensure you are using as many decimal point precision as possible in your
calculations so to minimize this "discrepancy"._

## Update:

Just to give an example of what you can do with manipulation like this (beyond
fixing round errors):

[![enter image description
here](http://i.stack.imgur.com/jobMp.png)](http://i.stack.imgur.com/jobMp.png)

The table above shows the Share of Voice (SoV) of a Respiratory drug brand
(R3) and it's rank among all brands (first two columns of data) and SoV & Rank
also within it's same class of brands only (third and forth column). This is
compared against previous month (July 15) and if the rank has increased then
it is highlighted in green and an upward facing arrow is added and if declined
in rank then highlighted in red and downward red facing arrow added. Just adds
a little, color and visualization to what otherwise can be dull tables.

