0

I have written a program to create a report in excel using python. The below script inserts a bar chart into the spreadsheet which seems to be working perfectly...

barchart = workbook.add_chart({'type': 'bar','legend':False})
barchart.set_legend({'none':True})
series_end_range = len(strategy.keys())+12
barchart.add_series({'values':'=Attribution!$B$12:$B$'+str(series_end_range),'categories': '=Attribution!$A$12:$A$'+str(series_end_range),'data_labels': {'value': True,'category': True,'leader_lines': True},})`
worksheet.insert_chart('D12',barchart,{'x_scale':.4,'y_scale':1.2})

However, when I try to write another dataframe below this chart, using the below code, the chart disappears.

from openpyxl import load_workbook
book = load_workbook(f'{FUNDNAME}.xlsx')
writer = pd.ExcelWriter(f'{FUNDNAME}.xlsx', engine='openpyxl') 
writer.book = book
writer.sheets = dict((ws.title, ws) for ws in book.worksheets)
df_returns.to_excel(writer,sheet_name='Attribution',startrow=series_end_range+4, startcol=0)

Any idea what part of the second code is causing the chart in the first code to disappear? it's well below where the chart placement was would be so it doesn't seem to be overriding because it's in the same space

Thanks!

Rob Friedman
  • 95
  • 2
  • 8
  • See this answer: https://stackoverflow.com/a/46915816/5496634 – Sam Jul 31 '18 at 19:05
  • I don't think this solves my problem. I'm not trying to post multiple dataframes, but it seems that dataframes and charts on the same sheet are mutually exclusive. – Rob Friedman Jul 31 '18 at 21:04

0 Answers0