I am trying to build a simple python script that reads data from a .csv file, formats the data to an easy to read layout, then either writes it to a new xlsx file or appends to an existing xlsx file, depending on user input. That all works well and I write to the new file using;
with pd.ExcelWriter(file_path) as writer:
df.to_excel(writer, sheet_name='Master')
Now I want to add a second sheet that contains excel charts from the data and have extended the above code to;
with pd.ExcelWriter(file_path) as writer:
df.to_excel(writer, sheet_name='Master')
book = writer.book
sheet = writer.sheets['Master']
chart_a = book.add_chart({'type': 'line'})
chart_a.add_series({
'categories': ['Master', 1, 0, trend_data_row, 0],
'values': ['Master', 1, 1, trend_data_row, 1],
})
chart_a.set_x_axis({'name': 'time', 'position_axis': 'on_tick'})
chart_a.set_y_axis({'name': 'value'})
chart_a.set_legend({'position': 'Bottom'})
sheet.insert_chart('A11', chart_a)
writer.save()
This adds the chart to the 'Master' sheet as expected, I don't understand how to create the second sheet and insert the chart there instead. I have tried changing sheet = writer.sheets
to a new name [Graphs] but I guess its looking for an existing sheet with that name rather than creating one. Any help is really appreciatted.