pythonpython-pptx

using python pptx how can i display the % in my chart data


I would like to create a chart on a powerpoint slide with pourcent datas , python doesn't let my directly put the % symbol on my data so i had to only put the datas , but i want to show the % symbol on the chart and also on the axes So heres my code :

 if slide:
              
    for shape in slide.shapes:
        
        if shape.shape_type == MSO_SHAPE_TYPE.CHART:
            chart = shape.chart
            if  result and moisDate and nameIntake :
                    chart_data = CategoryChartData()
                    chart_data.categories = nameIntake
                    for moisSingleDate in moisDate:
                        chart_data.add_series( str(moisSingleDate)  , values= ( result[moisSingleDate]) ,number_format= '0%') 
                    chart.replace_data(chart_data)
                    print(result)

but unfortunatly my datas gets multiplied by 100... instead of displaying 24.93% i get 2493% does anyone knows how to do it please ?


Solution

  • You have to divide by 100 in-place. Percentage formatting expects decimal values normalized to 0.0..1.0 as 0..100%.

    for moisSingleDate in moisDate:
        # Divide values by 100 to correctly format as percentages
        percentage_values = [value / 100 for value in result[moisSingleDate]]
        chart_data.add_series(str(moisSingleDate), values=percentage_values, number_format='0%')