原文链接:http://www.juzicode.com/python-error-xlwt-attempt-to-overwrite-cell-sheetname-rowx-colx
错误提示:
xlwt写入单元格提示Attempt to overwrite cell: sheetname=’juzicode’ rowx=5 colx=5
#juzicode.com/VX公众号:juzicode
import xlwt
wb = xlwt.Workbook()
ws = wb.add_sheet('juzicode')
ws.write(5,5,'juzicode.com')
ws.write(5,5,'微信公众号:桔子code')
==========运行结果:
File D:\Python\Python310\lib\site-packages\xlwt\Row.py:154, in Row.insert_cell(self, col_index, cell_obj)
151 if not self.__parent._cell_overwrite_ok:
152 msg = "Attempt to overwrite cell: sheetname=%r rowx=%d colx=%d" \
153 % (self.__parent.name, self.__idx, col_index)
--> 154 raise Exception(msg)
155 prev_cell_obj = self.__cells[col_index]
156 sst_idx = getattr(prev_cell_obj, 'sst_idx', None)
Exception: Attempt to overwrite cell: sheetname='juzicode' rowx=5 colx=5
错误原因:
1、ws.write(5,5,’xyz’) 对单元格(5,5) 先后2次写入导致报错。
解决方法:
1、在用wb.add_sheet(‘juzicode’)创建sheet页对象时,声明cell单元可以被覆盖:
#juzicode.com/VX公众号:juzicode
import xlwt
wb = xlwt.Workbook()
#ws = wb.add_sheet('juzicode')
ws = wb.add_sheet('juzicode',cell_overwrite_ok=True)
ws.write(5,5,'juzicode.com')
ws.write(5,5,'微信公众号:桔子code')
扩展内容:
如果本文还没有完全解决你的疑惑,你也可以在微信公众号“桔子code”后台给我留言,欢迎一起探讨交流。