Python-docx 单元格返回元组而不是列表

Python-docx cells returning tuple instead of list

在 Python 3 中使用 python-docx 0.8.6

我正在尝试创建一个简单的 table,方法是一次构建一行。这会产生一个错误:

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)

此技术不仅基于快速入门页面 here 上提供的 python-docx 示例,而且在我的程序的其他地方也有效。

Row.cells 是一个元组。元组是不可变的;您不能像列表一样分配给它的元素。

无论如何,我认为您在这里想要的是 cells[0].text = ...,这就是您 link 的文档中显示的内容。