'XlsxWriter' object has no attribute 'save'. Did you mean: '_save'?

ForumBot
Messages : 26117
Inscription : mer. avr. 22, 2026 5:33 pm

'XlsxWriter' object has no attribute 'save'. Did you mean: '_save'?

Message par ForumBot »

I’m trying to save data from a DataFrame to an Excel file using pandas. I tried the following code:

`import pandas as pd

import xlsxwriter

data = {‘Name’: [‘John’, ‘Jane’, ‘Adam’], ‘Age’: [25, 30, 35], ‘Gender’: [‘M’, ‘F’, ‘M’]}

df = pd.DataFrame(data)


writer = pd.ExcelWriter(‘output.xlsx’, engine=‘xlsxwriter’)

df.to_excel(writer, sheet_name=‘Sheet1’)


workbook = writer.book

worksheet = writer.sheets[‘Sheet1’]


Example: Adding a chart


chart = workbook.add_chart({‘type’: ‘line’})

chart.add_series({‘values’: ‘=Sheet1.$B$2:$B$4’})

worksheet.insert_chart(‘D2’, chart)

writer.save()



But I get the following error:
`writer.save()
^^^^^^^^^^^
AttributeError: 'XlsxWriter' object has no attribute 'save'. Did you mean: '_save'?


Does anyone know how to solve it?

ayi
Site Admin
Messages : 13176
Inscription : mer. avr. 22, 2026 5:21 pm

Re: 'XlsxWriter' object has no attribute 'save'. Did you mean: '_save'?

Message par ayi »

The save() method has been deprecated and removed in Pandas. You should use close() instead.


With older versions of Pandas you would have gotten this warning:



FutureWarning: save is not part of the public API, usage can give unexpected results and will be removed in a future version


With more recent versions you just need to use close() like this (I also fixed the syntax for the chart values):

`import pandas as pd

import xlsxwriter


data = {

“Name”: [“John”, “Jane”, “Adam”],

“Age”: [25, 30, 35],

“Gender”: [“M”, “F”, “M”],

}

df = pd.DataFrame(data)


writer = pd.ExcelWriter(“output.xlsx”, engine=“xlsxwriter”)

df.to_excel(writer, sheet_name=“Sheet1”)


workbook = writer.book

worksheet = writer.sheets[“Sheet1”]


Example: Adding a chart


chart = workbook.add_chart({“type”: “line”})

chart.add_series({“values”: “=Sheet1!$C$2:$C$4”})

worksheet.insert_chart(“E2”, chart)


writer.close()



Output:
Répondre

Revenir à « Excel & VBA »