0

is it possible to create a chart in python with multiple categories? For example: enter image description here

I copy pasted this in powerpoint, loaded it and tried this:

presentation.slides[0].shapes[0].chart.plots[0].categories.flattened_labels

which gave me all the labels in a tuple format -->

(('Oct-2019', 'Advertiser'),('Oct-2019','25th percentile), etc ...)

if I try printing :

presentation.slides[0].shapes[0].chart.plots[0].categories[i]
for i in 0 to 3

I get the values 'Advertiser','25th percentile' etc, but I can't find a way to access the 'Oct-2019' value.

When creating the ChartData, I saw that I can also add_category, but this adds the label to the categories I'm currently accessing (e.g. Advertiser, 25th percentile, etc), but I would like to add 'Nov 2019' which is in another hierarchy level.

This is a bit of a roundabout way of asking if anyone has created a multi-category chart with python pptx, and how they would do it from the chartdata level/what this would look like.

Thank you!

scanny
  • 26,423
  • 5
  • 54
  • 80
davoro
  • 53
  • 3
  • Please provide code snippet. – Anupam Chaplot Feb 10 '20 at 03:25
  • Check out the documentation on categories here: https://python-pptx.readthedocs.io/en/latest/api/chart-data.html#pptx.chart.data.Category. The gist is that you add a category like Oct-2019, then add _sub-categories_ to that new category for the advertiser, percentile, etc. – scanny Feb 10 '20 at 04:53

1 Answers1

-1
#data

pivot_digital_monthly_imp_laydown = pd.pivot_table(digital_monthly_imp_laydown, values='Original Value', index=['Year','Month'],
                    columns=['Campaign'], aggfunc=np.sum,fill_value=0,sort=False).reset_index().rename_axis(None, axis=1)
    pivot_digital_monthly_imp_laydown.Year=pivot_digital_monthly_imp_laydown.Year.astype(str)

    #inserting Impressions laydown in slide
    prs=input_ppt
    digital_monthly_imp_laydown_chart = prs.slides[3].shapes[6].chart

    digital_monthly_imp_laydown_data =CategoryChartData()
    #multilevel Categories
    cat=list(pivot_digital_monthly_imp_laydown['Year'])
    subcat=list(pivot_digital_monthly_imp_laydown['Month'])
    b={}
    for i,j in zip(cat,subcat):
        key=str(i)
        b.setdefault(key,[])
        b[key].append(j)
    main_cat=list(b.keys())
    for i in range(len(main_cat)):
        ear=digital_monthly_imp_laydown_data.add_category(str(main_cat[i]))
        for sub in b[main_cat[i]]:
            ear.add_sub_category(str(sub))
    #add series data
    for col in pivot_digital_monthly_imp_laydown.columns[2:]:
        new = list(pivot_digital_monthly_imp_laydown[col])
        digital_monthly_imp_laydown_data.add_series(str(col),new)
        new=[]
    digital_monthly_imp_laydown_chart.replace_data(digital_monthly_imp_laydown_data)
    
    #saving
    prs.save("./Output/Sub Brand Output.pptx")
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Dec 20 '21 at 13:08