为什么我不能在 Excel 工作簿中添加数字来分隔 sheet?

Why I can't add a figure to separate sheet in Excel Workbook?

我需要将数据和相应的图形保存在单独的 excel 个工作表中。

writer = pd.ExcelWriter('Fiber Forecast.xlsx', engine = 'xlsxwriter')
fiber_forecast_future.to_excel(writer,sheet_name='Future', index=True)
worksheet = writer.sheets['New Sheet']
worksheet.insert_image('C2','India Fiber Price.png')
workbook.save()

但是当我尝试这样做时,添加 'New Sheet' 不需要 work.I 将图像添加到 worksheet.I 出现此错误。

 ---------------------------------------------------------------------------
    KeyError                                  Traceback (most recent call last)
    <ipython-input-11-950dc62ed281> in <module>
        109 writer = pd.ExcelWriter('Fiber Forecast.xlsx', engine = 'xlsxwriter')
        110 fiber_forecast_future.to_excel(writer,sheet_name='Future', index=True)
    --> 111 worksheet = writer.sheets['New Sheet']
        112 #worksheet.insert_image('C2','India Fiber Price.png')
        113 workbook.save()
    
    KeyError: 'New Sheet'

您可以使用

writer.book.add_worksheet("New Sheet") 

相反。这里有关于此方法和工作簿对象的更多信息:https://xlsxwriter.readthedocs.io/workbook.html

但您应该可以使用上面的代码行创建新的 sheet。