openpyxl:AttributeError: ‘MergedCell’ object attribute ‘value’ is read-only

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 “”“

  • 1
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 10
    评论
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值