首页 > 解决方案 > 在 Openpyxl 中,如何用工作表中的 Cell 对象替换单元格?

问题描述

所以我有一个函数,它接受一个Cell对象并吐出一个已处理的cell

from openpyxl.cell.cell import Cell
cell = Cell(ws)

def process_cell(cell):
    # Add style to cell
    return cell

但是我不能这样做:

cell = process_cell(cell)
ws['A1'] = cell

错误是:

raise ValueError("Invalid column index {0}".format(idx))
ValueError: Invalid column index None

标签: pythonopenpyxl

解决方案


问题是这样的: cell = Cell(ws)

这将创建一个绑定到特定工作表但没有您必须提供的任何坐标的单元格。如果您处于只写模式,则必须使用 aWriteOnlyCell并将其传递给工作表的append()方法。


推荐阅读