Search code examples
python-3.xpython-docx

Python-docx cells returning tuple instead of list


Working in Python 3 with python-docx 0.8.6

I am trying to create a simple table by building it up one row at a time. This generates an error:

TypeError: 'tuple' object does not support item assignment

    numCols = 2
    numRows = 0 #dynamically create table
    activeTable = document.add_table(rows=numRows, cols=numCols)
    for actorIndex in reversed(actorsRanked):
        cells = activeTable.add_row().cells
        cells[0] = '{:20}'.format(actorIndex.id.get())
        cells[1] = '{:4.1}'.format(actorIndex.pains[painIndex].avg)

Not only is this technique is based on the python-docx example provided on the quickstart page here, it's working elsewhere in my program.


Solution

  • Row.cells is a tuple. A tuple is immutable; you cannot assign to an element of it like you can a list.

    In any case, I think what you want here is cells[0].text = ..., which is what is shown in the documentation you link to.