pandas exlewriter.book 不读取我的 excel 文件甚至破坏现有文件

pandas exlewriter.book does not read my excel file and even break the existed file

我想在一个 excel 文件中堆叠一系列数据帧,我写了下面的代码。

if os.path.isfile(result) is False:
    with pd.ExcelWriter(result, engine='openpyxl') as writer:
        raw_data.to_excel(writer,sheet_name=sheet1, index=False)      
else:
    with pd.ExcelWriter(result, engine='openpyxl') as writer:
        writer.book=openpyxl.load_workbook(result)
        raw_data.to_excel(writer, sheet_name=sheet2, index=False)

excel 文件的初始创建工作正常,但在创建文件后,错误发生在步骤 writer.book=openpyxl.load_workbook(result),如下所示。

zipfile.BadZipFile: File is not a zip file

During handling of the above exception, another exception occurred:

IndexError: At least one sheet must be visible

而且,在这个错误之后,现有文件的大小发生了变化,文件没有打开。

之前:

之后:

有解决这个问题的办法吗?


我之前的代码有一些歧义,下面的代码可能会更清楚

import pandas as pd
import os
import openpyxl as oxl

resultpath=r'D:/CODE/result.xlsx'

for i in range(5):
    sheet = 'Sheet'+ str(i)
    result = pd.DataFrame({'a': range(10), 'b': range(10)})
    if os.path.isfile(resultpath) is False:
        with pd.ExcelWriter(resultpath, engine='openpyxl') as writer:
            result.to_excel(writer,sheet_name=sheet, index=False)
             
    else:
        with pd.ExcelWriter(resultpath, engine='openpyxl') as writer:
            writer.book =oxl.load_workbook(resultpath)
            result.to_excel(writer, sheet_name=sheet, index=False)

每个包的版本如下。

Pandas:1.4.2 openpyxl : 3.0.9

欢迎来到 Stack Overflow。 sheet_name 应该用引号引起来。此外,写入后保存文件 writer.save() 始终是一个好习惯。更新后的代码可以正常工作...

if os.path.isfile(result) is False:
    with pd.ExcelWriter(result, engine='openpyxl') as writer:
        raw_data.to_excel(writer,sheet_name='Sheet1', index=False)
        writer.save()
else:
    with pd.ExcelWriter(result, engine='openpyxl') as writer:
        writer.book=openpyxl.load_workbook(result)
        raw_data.to_excel(writer, sheet_name='Sheet2', index=False)
        writer.save()

我找到了解决这个问题的方法

使用ExcelWriter的'mode' 属性解决了这个问题,也让代码更容易阅读。

修改后的代码如下,运行正常

import pandas as pd
import os

result = pd.DataFrame({'a': range(10), 'b': range(10)})

resultpath=r'D:/CODE/test.xlsx'

for i in range(5):
    sheet = 'Sheet'+ str(i)
    if not os.path.exists(resultpath):   
        with pd.ExcelWriter(resultpath, mode='w', engine='openpyxl') as writer:
            result.to_excel(writer, index=False, sheet_name=sheet)
    else:   
        with pd.ExcelWriter(resultpath, mode='a', engine='openpyxl') as writer:
            result.to_excel(writer, index=False, sheet_name=sheet)