pythonpython-pptx

python-pptx dataLabels to show percentage (%) symbol for data points properties


while generating column stacked 100% chart. if suppose series values are (1, 8, 1). I would expect to see segments with data labels 10%, 80% and 10%.

However, with the code below I see 100%, 800% and 100%.

below is code used. plot = chart.plots[0] plot.has_data_labels = True

data_labels = chart.plots[0].data_labels
data_labels.font.size = Pt(6)
data_labels.number_format = '0%'
data_labels.position = XL_LABEL_POSITION.CENTER

when data_labels.show_percentage=True is set instead of data_labels.number_format = '0%' it just shows number but not percentage symbol.

column stacked 100 chart

I want to show data labels with percentage like below

data labels with % symbol

when data_labels.number_format = '0%' is used 40 is shown as 4000% 3->300% , 60->6000%

below is data used

data used for chart

when data_labels.number_format = '0%' is used


Solution

  • Try adding data_labels.show_percentage = True and change the number-format to "0\%" (note the backslash before the percent-sign.

    data_labels = chart.plots[0].data_labels
    data_labels.show_percentage = True
    data_labels.number_format = "0\%"