3

I have very simple xy chart that I made with python-pptx (thanks all from stack overflows who helped me with this). I am struggling with setting font size of XTitle, YTitle, ChartTitle, Series Name and also values on x any axis like (0 20 40 60). The pptx documentation is not very clear to me

enter image description here

from pptx import Presentation
from pptx.util import Inches,Pt

from pptx.enum.chart import XL_CHART_TYPE
from pptx.chart.data import XySeriesData,XyChartData

prs = Presentation()
title_slide_layout = prs.slide_layouts[0]
blank_slide_layout = prs.slide_layouts[6]

slide = prs.slides.add_slide(title_slide_layout)
slide2 = prs.slides.add_slide(blank_slide_layout)

title = slide.shapes.title
subtitle = slide.placeholders[1]

title.text = "Hello, World!"
subtitle.text = "python-pptx was here!"
chart_data = XyChartData()
cd = chart_data.add_series('Series Name',number_format=None)
cxvalues=[0,1,2,3,4,5]
cyvalues=[10,22,33,38,40,43]

for x, y in list(zip(cxvalues, cyvalues)):
    cd.add_data_point(x, y, number_format=None)

x, y, cx, cy = Inches(1), Inches(2), Inches(8), Inches(3)
chart = slide2.shapes.add_chart(XL_CHART_TYPE.XY_SCATTER_LINES_NO_MARKERS, x, y, cx, cy, chart_data).chart
chart.category_axis.axis_title.text_frame.text= "XTitle"
chart.value_axis.axis_title.text_frame.text= "YTitle"

chart.chart_title.text_frame.text='ChartTitle'

prs.save('test_template.pptx')
user2774120
  • 139
  • 3
  • 16

1 Answers1

9

Try using the Font object on the first paragraph of the axis and chart title:

from pptx.util import Pt

chart.chart_title.text_frame.paragraphs[0].font.size = Pt(36)
chart.category_axis.axis_title.text_frame.paragraphs[0].font.size = Pt(24)
chart.value_axis.axis_title.text_frame.paragraphs[0].font.size = Pt(24)

This is pretty much how you would format text in any other text-frame, although I believe in the case of these title objects there is exactly one paragraph.

scanny
  • 26,423
  • 5
  • 54
  • 80
  • Hey thank you so much.. its working for me now.. Can you please also suggest how to change font size of axis values.. like for values 0 2 4 6 on xaxis in my case. I also want to decrease its size – user2774120 Jun 01 '20 at 18:28
  • 1
    ok i figured it out .. chart.category_axis.tick_labels.font.size= Pt(12) worked for me .. Thanks again – user2774120 Jun 01 '20 at 18:35
  • How can I add dotted vertical line say at x=4? – MAC Oct 08 '20 at 08:13