Within an python-pptx-generated XYchart, I would like to add labels to each point of the series. Code looks like this:
for p, v in zip(chart.series[0].points, dataSeries):
p.data_label.position = XL_LABEL_POSITION.ABOVE
p.data_label.text_frame.clear()
for paras in p.data_label.text_frame.paragraphs:
paras.font.size = Pt(10)
p.data_label.text_frame.text = str.format('{0:.1f}', v)
The new text is set correctly, but iterating through the paras to change font size has no effect. Any ideas how I could achieve a font size change for the labels?
You need to set the font size after adding the text frame text.
data_label = p.data_label
...
text_frame = data_label.text_frame
# text_frame.clear() # this line is not needed, assigning to .text does this
text_frame.text = str.format('{0:.1f}', v)
for paragraph in text_frame.paragraphs:
paragraph.font.size = Pt(10)
# -- OR --
for run in text_frame.paragraphs[0].runs:
run.font.size = Pt(10)
When you call TextFrame.text
, all existing paragraphs are removed and a single new paragraph is added. Along the way, all character formatting is removed to produce a "clean slate" before adding the specified text.
I don't recall whether PowerPoint respects the font set at the paragraph level (using the a:defRPr
element). If not, you'll need to do it at the run level, as shown after the -- OR -- line.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.