Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set font size of chart,axis values and axis titles in python-pptx for xyscatter chart

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')
like image 338
user2774120 Avatar asked Dec 11 '25 18:12

user2774120


1 Answers

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.

like image 189
scanny Avatar answered Dec 13 '25 07:12

scanny



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!