pandas IndexError:至少一张纸必须是可见的

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46840960/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 04:40:20  来源:igfitidea点击:

IndexError: At least one sheet must be visible

pythonpandas

提问by Andy Hayden

 def multiple_dfs(sheet, row=2):

    writer = pd.ExcelWriter("testing.xlsx", engine='openpyxl')

    f1 = {
        'user': ['Bob', 'Jane', 'Alice'], 
        'income': [40000, 50000, 42000],
    }

    f2 = {
        'amount': ['Chest', 'Bras', 'Braa'], 
        'income': [40000, 50000, 42000]
    }

    frames = [f1, f2]


    for f in frames:
        try:
            wb = load_workbook("testing.xlsx")
            ws = wb.get_sheet_by_name("aaa")

            writer.wb = wb
            writer.sheets = dict((ws.title, ws) for ws in wb.worksheets)

            row = ws.max_row + 2
        except:
            pass 

        df = pd.DataFrame(f)
        df.to_excel(writer, sheet, startrow=row, index=False)
        writer.save()
    # writer.close()

multiple_dfs('aaa')

I got this error, but I can't fix it. I adapt this short representation of what is happening in my code, but it is hard to see where is the real problem. Here is the

我收到此错误,但无法修复。我对代码中发生的事情的简短表示进行了修改,但很难看出真正的问题在哪里。这里是

Traceback (most recent call last):
  File "create_and_update_xlsx_sheets.py", line 144, in <module>
    create_and_update_worksheets()
  File "create_and_update_xlsx_sheets.py", line 140, in create_and_update_worksheets
    writer.save()
  File "/home/jeremie/.virtualenvs/NHL/lib/python3.5/site-packages/pandas/io/excel.py", line 824, in save
    return self.book.save(self.path)
  ...
  File "/home/jeremie/.virtualenvs/NHL/lib/python3.5/site-packages/openpyxl/writer/workbook.py", line 61, in get_active_sheet
    raise IndexError("At least one sheet must be visible")
IndexError: At least one sheet must be visible

How could I fix that issue?

我怎么能解决这个问题?

P.S.Be aware that error is displayed half the time.

PS请注意,错误显示一半的时间。

回答by Ando Jurai

Alternatively, if you don't need to load a workbook, you can merely use xlsxwriter instead of openpyxl; it hasn't this problem. You can also create a workbook with the regular

或者,如果您不需要加载工作簿,则可以仅使用 xlsxwriter 而不是 openpyxl;它没有这个问题。您还可以使用常规创建工作簿

from openpyxl import Workbook
#...
wb= Workbook()
ws=wb.active
with pd.ExcelWriter(output_filepath, engine="openpyxl") as writer:
    writer.book=wb
    writer.sheets = dict((ws.title, ws) for ws in wb.worksheets)
    #useful code
    df.to_excel(writer, sheet, ...)
    writer.save()

now you can work with both pandas.to_excel and the interesting methods of openpyxl that are not translated into it (that's not my hack; found it here but can't find where)

现在你可以使用 pandas.to_excel 和 openpyxl 的有趣方法,这些方法没有被翻译成它(这不是我的黑客;在这里找到了,但找不到在哪里)

回答by Andy Hayden

It seems like what you want to do is just write each DataFrame to the same sheet (appending it below the last), so I think you can write this as:

看起来您想要做的只是将每个 DataFrame 写入同一张表(将其附加在最后一张下方),所以我认为您可以将其写为:

start_row = 1
for df in frames:  # assuming they're already DataFrames
    df.to_excel(writer, sheet, startrow=start_row, index=False)
    start_row += len(df) + 1  # add a row for the column header?
writer.save()  # we only need to save to disk at the very end!