openpyxl:AttributeError: ‘MergedCell’ object attribute ‘value’ is read-only
使用openpyxl向excel写入数据时,报了一个错误:openpyxl:AttributeError: ‘MergedCell’ object attribute ‘value’ is read-only
某度翻译过来 是:AttributeError:“MergedCell”对象属性“value”是只读的
个人理解是这是你的代码程序根据你给的值坐标读取到了已经合并的单元格,由于单元格合并后,excel默认合并单元格应该不能被赋新值,导致了写入数值异常
解决办法:
格式化该单元格的属性,即取消的read_only属性。
修改源代码workshet.py文件的大约620做添加如下代码:
if cr.coord not in self.merged_cells:
raise ValueError("Cell range {0} is not merged".format(cr.coord))
self.merged_cells.remove(cr)
# Deletes the MergedCellRange.
# del self._merged_cell_range[cr.bounds]
# autho : watson
# aim : deal with the bug about umerger
# describe : Add the following five lines of code to format the attribute.
min_col, min_row, max_col, max_row = cr.bounds
for row in range(min_row, max_row + 1):
for col in range(min_col, max_col + 1):
if col == min_col and row == min_row:
continue
del self._cells[(row, col)]
def append(self, iterable):
"""Appends a group of values at the bottom of the current sheet “”“