尝试将熊猫数据框保存到现有Excel工作表时出现AttributeError

时间:2019-02-25 16:00:34

标签: python excel python-3.x pandas attributeerror

我正在尝试将熊猫数据框写入新选项卡上的现有excel工作表,但这会给我以下错误:

  

AttributeError:“ NoneType”对象没有属性“ read”。

我已经确定这是因为pandas to_excel返回了一个NoneType对象,这不允许我使用writer.save()保存文件。有人知道解决方法吗?

path = 'summary.xlsx'
book = load_workbook(path)
writer = pd.ExcelWriter(path, engine='openpyxl')
writer.book = book
writer.sheets = dict((ws.title, ws) for ws in book.worksheets)

df.to_excel(writer, sheet_name="results")
writer.save()

1 个答案:

答案 0 :(得分:0)

我有完全一样的问题。 我设法通过从工作簿的每个工作表中删除legacy_drawing中的值来解决此问题。

path = 'summary.xlsx'
book = load_workbook(path)
writer = pd.ExcelWriter(path, engine='openpyxl')
writer.book = book
writer.sheets = dict((ws.title, ws) for ws in book.worksheets)

for s in list(writer.sheets.keys()):
   writer.sheets[s].legacy_drawing = None

df.to_excel(writer, sheet_name="results")
writer.save()