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

2

45

I'm trying to save data from a dataframe in excel file by using pandas. I was trying 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?

Tanbark answered 24/4, 2023 at 10:34 Comment(2)
These might help: github.com/jmcnamara/XlsxWriter/issues/190 github.com/jmcnamara/XlsxWriter/issues/191 #22905154Charlottcharlotta
Thanks for you answer, but I didn't find the solution to my problem. The main problem is that the code works on my old pc, while on my new pc doesn't. I though that the problema could be different version of the python libraries, but I still don't manage to find the solution.Tanbark
U
74

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:

enter image description here

Upstart answered 27/4, 2023 at 10:17 Comment(0)
G
6

You can use a context manager via with statement. As soon as you leave its indentation, the XlsxWriter handle will be closed and the file will be created.

import pandas as pd
import xlsxwriter

df = pd.DataFrame({
    'Name': ['John', 'Jane', 'Adam'], 
    'Age': [25, 30, 35], 
    'Gender': ['M', 'F', 'M']
})

with pd.ExcelWriter("output.xlsx", engine="xlsxwriter") as writer:
    df.to_excel(writer, sheet_name="Sheet1", index=False)
    
    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", "name": "=Sheet1!$B$1"})
    worksheet.insert_chart("D2", chart)

result

Giveandtake answered 17/2 at 8:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.