Reputation: 328
I want to have multiple doughnut charts (max 3) using python's pptx module. As of now I'm able to add only one chart, how can I reduce the size of the doughnuts accordingly so that I can adjust 2 or 3 charts in one slide. Also, how can I auto adjust the size of doughnuts depending on number of graphs with different set of values.
python code:
from pptx import Presentation
from pptx.util import Pt, Cm, Inches
from pptx.chart.data import CategoryChartData, ChartData
from pptx.enum.chart import XL_CHART_TYPE
def create_ppt():
pr = Presentation()
slide1_register = pr.slide_layouts[0]
#add initial slide
slide1 = pr.slides.add_slide(slide1_register)
#top placeholder
title1 = slide1.shapes.title
#subtitle
sub_title = slide1.placeholders[1]
# insert text
title1.text = "Data"
title_para = title1.text_frame.paragraphs[0]
title_para.font.name = "Arial (Headings)"
title_para.font.size = Pt(34)
title1.top = Cm(1)
title1.left = Cm(0)
title1.width = Cm(15)
title1.height = Cm(3)
chart_data = ChartData()
chart_data.categories = ['X1', 'X2', 'X3', 'X4']
chart_data.add_series('Data 1', (75, 10, 5, 4))
x, y, cx, cy = Inches(2), Inches(2), Inches(6), Inches(4.5)
chart = slide1.shapes.add_chart(
XL_CHART_TYPE.DOUGHNUT, x, y, cx, cy, chart_data
).chart
chart.has_legend = True
chart.legend.include_in_layout = False
apply_data_labels(chart)
pr.save("test222222222.pptx")
def apply_data_labels(chart):
plot = chart.plots[0]
plot.has_data_labels = True
for series in plot.series:
values = series.values
counter = 0
for point in series.points:
data_label = point.data_label
data_label.has_text_frame = True
data_label.text_frame.text = str(values[counter])
counter = counter + 1
create_ppt()
Upvotes: 0
Views: 596
Reputation: 11
As you insert the chart, you can specify its position and size, as you already do:
x, y, cx, cy = Inches(2), Inches(2), Inches(6), Inches(4.5)
slide1.shapes.add_chart(XL_CHART_TYPE.DOUGHNUT, x, y, cx, cy, chart_data)
In the code above x
and y
are the coordinates of the top left corner of the chart, while cx
and cy
are the width and height, respectively.
You can repeat the add_chart
call as many times as many chart you want to insert with proper position and size values.
See the documentation for shapes.add_chart
function here.
Upvotes: 1