How to iterate figures using openpyxl into specific cells in multiple excel sheets for unique data of each sheet?

Viewed 36

This is for a personal project, I am trying to take the data from two Excel sheets. Each one contains approximately 200.000 records. I would like to split them into multiple sheets so that each one corresponds to a specific week. I used groupby function and exported the outcome into one excel file with 52 sheets (52 weeks in a whole year). I have been able to do that.

Now, I would like to write a code that can plot figures/graphs from matplotlib & seaborn libraries for each sheet data.

Excelwriter = pd.ExcelWriter("data.xlsx",engine="xlsxwriter")

for i, df33 in enumerate (list):
    df33.to_excel(Excelwriter, sheet_name="Week" + str(i+1),index=False)

Excelwriter.save()

I used this to generate the excel workbook with 52 sheets of required data stored within list (named LIST)

wb = Workbook('data.xlsx')

Matplotlibbb = []

for i, df33 in enumerate (list):

    ws = wb.active

    df_grouped = df33.groupby(by="SubClass", as_index=False).sum()
    ax = df_grouped.plot(kind="bar", x="SubClass", y="Qty 20-21", color="#50C878", grid=False, title ="Week" + str(i+1))
    ay = df_grouped.plot(kind="bar", x="SubClass", y="Qty 21-22", color="#FF8C00", grid=False, title ="Week" + str(i+1))

    for j in df33:
      ws.add_image(ax.savefig('Sales Vs Qty'),'015')

    #fig = ax.get_figure()

I have written a loop to generate the required barplot for sales vs qty for two years, the graph generates in the Google Colab window but I am unable to export it within each sheet of the workbook. I need a fix to my for-loop and I am not sure how to proceed.

I will not be able to attach the data or code as it is sensitive information, but any help received will be of great help.

0 Answers
Related