HomeCourseModule 11 › Inserting charts into Excel

Inserting charts into Excel

Module 11 · Data Visualization7 min readIntermediate

What you'll learn

  • Save a chart as PNG and insert it into a sheet
  • Use =PY() to embed a chart directly
  • Build a sheet with multiple charts in code

Option 1 — Paste a PNG manually

Save the chart, then in Excel: Insert → Pictures → from file. Fastest for one-offs.

Option 2 — =PY() chart

Covered in Module 8. Best for charts that should refresh when the underlying Excel data changes.

Option 3 — Programmatic with openpyxl

import pandas as pd
import matplotlib.pyplot as plt
from openpyxl import load_workbook
from openpyxl.drawing.image import Image

# Generate the chart
df = pd.read_csv("monthly.csv")
fig, ax = plt.subplots(figsize=(7, 3))
df.plot(x="month", y="revenue", ax=ax, marker="o")
plt.tight_layout()
fig.savefig("monthly.png", dpi=150)
plt.close(fig)

# Insert into the workbook
df.to_excel("report.xlsx", index=False, sheet_name="Data")
wb = load_workbook("report.xlsx")
ws = wb.create_sheet("Chart")
img = Image("monthly.png")
ws.add_image(img, "B2")
wb.save("report.xlsx")

Walkthrough: monthly report builder

The plan

One script. Reads a CSV. Writes a 2-sheet Excel: a Data sheet and a Charts sheet with three charts.

The pattern

def make_chart(df, kind, filename, title):
    fig, ax = plt.subplots(figsize=(7, 3))
    df.plot(kind=kind, ax=ax, color="#217346")
    ax.set_title(title)
    plt.tight_layout()
    fig.savefig(filename, dpi=150)
    plt.close(fig)

Then call it three times and insert each via openpyxl.

Key takeaways

  • One-offs: save PNG, paste into Excel.
  • Live: =PY() chart that refreshes.
  • Bulk reports: openpyxl add_image in code.

Multi-chart workbook

Build a script that produces an Excel report with a Data sheet, a Bar Chart sheet, and a Line Chart sheet — all from one CSV input.

📹 Video walkthrough
A video walkthrough of this lesson will be embedded here. Until then, the written walkthrough above mirrors what the video will cover step-for-step.