Abstract class 'Excelwriter' with Abstract methods instantiatedpylint(Abstract-class-instantiated)

Asked

Viewed 133 times

1

I’m trying to save some Dataframes in an excel file but the same is giving error, I saw some tutorials and even leaving identical still gives error.

writer = pd.ExcelWriter('pandas_simple.xlsx', engine='xlsxwriter') as writer: 
    dCliente.to_excel(writer,sheet_name='DIM_CLIENTES', index=False)
    dProjeto.to_excel(writer,sheet_name='DIM_PROJETOS', index=False)
    dTask.to_excel(writer,sheet_name='DIM_TAREFAS', index=False)
    fFATO.to_excel(writer,sheet_name='FATO', index=False)
    dUsers.to_excel(writer,sheet_name='DIM_USUARIOS', index=False)
    xlswriter.save() 
    xlswriter.close()

and that’s the mistake that comes up. Abstract class 'Excelwriter' with Abstract methods instantiatedpylint(Abstract-class-instantiated)

grateful for the answers.

  • because it does not use df.to_excel?

  • has the possibility to save in several tabs ??

  • yes, see documentation: https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.to_excel.html

1 answer

0


As the documentation says, when you want to save more than one sheet in the same file you need to declare the Excelwriter object

with pd.ExcelWriter('teste.xlsx') as writer:  
    df1.to_excel(writer, sheet_name='df1')
    df2.to_excel(writer, sheet_name='df2')

In your case

with pd.ExcelWriter('pandas_simple.xlsx', engine='xlsxwriter') as writer: 
    dCliente.to_excel(writer,sheet_name='DIM_CLIENTES', index=False)
    dProjeto.to_excel(writer,sheet_name='DIM_PROJETOS', index=False)
    dTask.to_excel(writer,sheet_name='DIM_TAREFAS', index=False)
    fFATO.to_excel(writer,sheet_name='FATO', index=False)
    dUsers.to_excel(writer,sheet_name='DIM_USUARIOS', index=False)

You can set another engine this way

with pd.ExcelWriter('pandas_simple.xlsx', engine='openpyxl') as writer: 
    dCliente.to_excel(writer,sheet_name='DIM_CLIENTES', index=False)
    dProjeto.to_excel(writer,sheet_name='DIM_PROJETOS', index=False)
    dTask.to_excel(writer,sheet_name='DIM_TAREFAS', index=False)
    fFATO.to_excel(writer,sheet_name='FATO', index=False)
    dUsers.to_excel(writer,sheet_name='DIM_USUARIOS', index=False)

In your code you assign to the Writer object, thus: writer = pd.ExcelWriter('pandas_simple.xlsx', engine='xlsxwriter') as writer:


Using with, you ensure that the object is opened and closed correctly being unnecessary xlswriter.close(), since it doesn’t even contain the close method. The right name is xlsxwriter, and only by accessing the Workbook can you use . close or . save.

Example taken from the documentation:

import xlsxwriter

workbook  = xlsxwriter.Workbook('filename.xlsx')
worksheet = workbook.add_worksheet()

worksheet.write(0, 0, 'Hello Excel')

workbook.close()

Browser other questions tagged

You are not signed in. Login or sign up in order to post.