最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【已解决】openpyxl生成excel文件打开提示:发现xlsx中的部分内容有问题 是否让我们尽量尝试恢复

Excel crifan 19046浏览 0评论

用openpyxl去创建新excel文件,并从某原有excel中拷贝数据到新excel中。

其中包括保留原先的合并后的单元格,比如:

然后对应代码是:

def keepPrevMergedCells(wsIn, wsInCurCellRange, wsInCurRowNum, wsOut, wsOutColNum):

    """

        keep original excel’s merged cells

    """

    logging.info("keepPrevMergedCells: wsIn=%s, wsInCurCellRange=%s, wsInCurRowNum=%s, wsOut=%s, wsOutColNum=%s",

                 wsIn, wsInCurCellRange, wsInCurRowNum, wsOut, wsOutColNum)

    cellRangeValue = crifanLib.getCellRangeValue(wsIn, wsInCurCellRange)

    rangeMinRow = wsInCurCellRange.min_row

    rangeMinCol = wsInCurCellRange.min_col

    rangeMaxRow = wsInCurCellRange.max_row

    rangeMaxCol = wsInCurCellRange.max_col

    logging.info("curCellRange: min_row=%s, min_col=%s, max_row=%s, max_row=%s",

                 rangeMinRow, rangeMinCol, rangeMaxRow, rangeMaxCol)

    rowNumDiff = wsInCurRowNum – rangeMinRow

    logging.info("rowNumDiff=%s", rowNumDiff)

    curStartCol = wsOutColNum

    curEndCol = wsOutColNum + (rangeMaxCol – rangeMinCol)

    logging.info("curStartCol=%s, curEndCol=%s", curStartCol, curEndCol)

    if rowNumDiff > 1:

        wsOut.unmerge_cells(start_row=rangeMinRow,

                                     start_column=curStartCol,

                                     end_row=wsInCurRowNum – 1,

                                     end_column=curEndCol)

    wsOut.merge_cells(start_row=rangeMinRow,

                       start_column=curStartCol,

                       end_row=wsInCurRowNum,

                       end_column=curEndCol)

然后调试期间发现,去打开保存后的excel文件后报错:

发现 xlsx 中的部分内容有问题。是否让我们尽量尝试恢复? 如果信任此工作簿的源,请单击“是”。

点击 是,然后再点击 删除:

是可以打开的,且貌似数据也是对的:

但是要找到原因。

然后也去试了试:

点击查看,则会打开xml文件:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>

<recoveryLog xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main"><logFileName>修复结果到 xxx2100.xml</logFileName><summary>在文件“/Users/xxx210.xlsx”中检测到错误</summary><removedRecords summary="以下是已删除记录的列表:"><removedRecord>已删除的记录: /xl/worksheets/sheet1.xml 部分的 合并单元格</removedRecord></removedRecords></recoveryLog>

通过去查看xml文件所在目录:

并不能找到,其他我希望看到的,额外的文件或信息。

对于解决问题没帮助。

然后就去调试代码,然后一点点缩小出问题的范围,

最终调试发现,是

720行没问题

但是750行出问题

然后去原先文件看看是否有什么异常的情况:

最后找到了是原先的excel中的702 – 727 行 R列中:

是合并的单元格,但是内容却是空的:

经过调试,忽略掉这种:

合并了的单元格但是内容是空(value 是None或空字符串)

然后就可以正常打开excel文件,没有错误提示了。

具体代码是:

def keepPrevMergedCells(wsIn, wsInCurCellRange, wsInCurRowNum, wsOut, wsOutColNum):

    """

        keep original excel’s merged cells

        if merged cell range itself value is None, should NOT keep merged

        otherwise will cause generated excel file corrupted

    """

    logging.info("keepPrevMergedCells: wsIn=%s, wsInCurCellRange=%s, wsInCurRowNum=%s, wsOut=%s, wsOutColNum=%s",

                 wsIn, wsInCurCellRange, wsInCurRowNum, wsOut, wsOutColNum)

    cellRangeValue = crifanLib.getCellRangeValue(wsIn, wsInCurCellRange)

    if not cellRangeValue:

        logging.warning("Not keep previous None or empty value of merged cells")

        return

    # then wsOut.unmerge_cells and wsOut.merge_cells is ok

即可。

另外:

通过搜:

openpyxl 发现xlsx中的部分内容有问题 是否让我们尽量尝试恢复

找到:

excel部分内容有问题xml的修复 – CSDN博客

“excel文件实际是一个压缩文件包”

-》如果以后遇到类似问题,则可以去尝试:

复制一份出来,改后缀为zip,然后去zip文件里找找出错的地方,或许也是一种不错的思路。

转载请注明:在路上 » 【已解决】openpyxl生成excel文件打开提示:发现xlsx中的部分内容有问题 是否让我们尽量尝试恢复

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
90 queries in 0.209 seconds, using 22.08MB memory