openpyxl can not add a chart after using remove_sheet command to delete one sheet

Issue #530 resolved
Jimmy Xie created an issue

I use command remove_sheet to delete one sheet in a workbook. After the command executed, the charts in other sheets disappear. And I can not add chart to other sheets. Following code shows after sheet1 is deleted, the chart in sheet2 disappears.

from openpyxl import Workbook
from openpyxl.chart import Reference, Series, BarChart, BarChart3D, LineChart, LineChart3D, PieChart, PieChart3D

wb = Workbook()
def populate(sheet_name = 'sheet1'):
    ws = wb.create_sheet()
    ws.title = sheet_name
    data = [
        ['', 'loop1', 'loop2', 'Others', 'Unknown'],
        ['Good_Command', 2, 40, 30, 1],
        ['Bad_Command', 3, 40, 25, 4],
        ['Uncertain', 4, 50, 30, 5],
    for i in data:

    chart1 = BarChart()
    chart1.type = "bar" = 11
    chart1.title = "Horizontal Bar Chart"
    chart1.y_axis.title = 'Test number'
    chart1.x_axis.title = 'Sample length (mm)'

    data = Reference(ws, min_col=2, min_row=1, max_row=7, max_col=3)
    cats = Reference(ws, min_col=1, min_row=2, max_row=7)
    chart1.add_data(data, titles_from_data=True)
    chart1.shape = 4
    ws.add_chart(chart1, "H2")



Comments (4)

  1. CharlieC

    Can't see a quick fix for this (and it will happen in other situations) so at the moment the advice has to be: don't do this.

  2. Log in to comment