XlsxWriter 'save' AttributeError
Problem Statement
A common error occurs when using Pandas with XlsxWriter to export DataFrames to Excel and add customizations like charts. Attempting to save the file with writer.save()
triggers:
AttributeError: 'XlsxWriter' object has no attribute 'save'. Did you mean: '_save'?
This happens because pandas deprecated and removed the .save()
method for ExcelWriter
objects. Modern pandas versions (≈1.5.0+) no longer support save()
, resulting in this AttributeError when using older code.
Correct Solutions
Optimal Approach: Use Context Manager (Recommended)
Use Python's with
statement to automatically handle resource cleanup. The file saves correctly when exiting the block, eliminating manual closing.
import pandas as pd
df = pd.DataFrame({
'Name': ['John', 'Jane', 'Adam'],
'Age': [25, 30, 35],
'Gender': ['M', 'F', 'M']
})
with pd.ExcelWriter("output.xlsx", engine="xlsxwriter") as writer:
# Export DataFrame (disable index to avoid extra column)
df.to_excel(writer, sheet_name="Sheet1", index=False)
# Access workbook/worksheet objects
workbook = writer.book
worksheet = writer.sheets["Sheet1"]
# Add chart referencing the 'Age' column (Column B)
chart = workbook.add_chart({"type": "line"})
chart.add_series({
"values": "=Sheet1!$B$2:$B$4", # Age values (B2-B4)
"name": "=Sheet1!$B$1", # Series name from header (B1)
})
worksheet.insert_chart("D2", chart)
Key fixes:
- Replaces
writer.save()
with context manager - Uses
index=False
to align data columns correctly - Corrects chart references (
Sheet1!$B$X
instead ofSheet1.$B$X
) - Includes series name for clearer chart labeling
Alternative: Explicitly Close Writer
If not using a context manager, call .close()
on the writer object:
writer = pd.ExcelWriter("output.xlsx", engine="xlsxwriter")
df.to_excel(writer, sheet_name="Sheet1", index=False)
workbook = writer.book
worksheet = writer.sheets["Sheet1"]
chart = workbook.add_chart({"type": "line"})
chart.add_series({
"values": "=Sheet1!$B$2:$B$4",
"name": "=Sheet1!$B$1"
})
worksheet.insert_chart("D2", chart)
writer.close() # Use close() instead of save()
Why This Error Occurs
Deprecation Timeline
Older pandas versions (≤1.4.x) raised aFutureWarning
when using.save()
, but it still worked. Recent versions (≥1.5.0) fully removed the method to enforce safer resource handling.API Changes
The.save()
method was never part of pandas' public API. Its removal aligns with Python best practices for file I/O (using context managers orclose()
).
Best Practices for Excel Export
Always use
index=False
Avoid unexpected misalignment of columns and chart data.Validate cell references
Excel formulas in charts require!
notation (not.
):=Sheet1!$B$2:$B$4
→ Correct=Sheet1.$B$2:$B$4
→ Incorrect (causes errors)Prefer context managers
Guarantees files close properly, even if errors occur mid-execution.
::: success
Key Takeaway: Replace writer.save()
with
writer.close()
for explicit handling, or- Context manager (
with pd.ExcelWriter(...)
) for robust, automated file management.
:::