Mattman85208
Mattman85208

Reputation: 2090

With Python openpyxl how do you change the font size in a chart legend?

With Python openpyxl how do you change the font size in a chart legend.

#Create Chart
chart1 = BarChart()
chart1.type = "col"
chart1.style = 10
chart1.title = "Rolling 4 weeeeks"
chart1.legend.position = 'b'
chart1.legend.font = FONT(name = 'Calibri', size = 9)

and my chart legend text is still size 10

thanks!!

Upvotes: 2

Views: 2201

Answers (1)

Joe
Joe

Reputation: 7121

There is no .font attribute for Legend().

The documentation for Legend() states:

txPr (alias: textProperties)

Values must be of type <class ‘openpyxl.chart.text.RichText’>

You need to modify the legend like this:

from openpyxl.chart.text import RichText
from openpyxl.drawing.text import Paragraph, ParagraphProperties, CharacterProperties, Font


font = Font(typeface='Verdana')
size = 2000 # 20 point size
cp = CharacterProperties(latin=font, sz=size, b=True) # Try bold text
pp = ParagraphProperties(defRPr=cp)
rtp = RichText(p=[Paragraph(pPr=pp, endParaRPr=cp)])

ch1.legend.textProperties = rtp

(Inspired by Formatting chart data labels in openpyxl)

Upvotes: 3

Related Questions