使用Python openpyxl,如何更改图表图例中的字体大小?

时间:2019-06-11 21:23:07

标签: python python-3.x openpyxl

使用Python openpyxl如何更改图表图例中的字体大小。

#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)

并且我的图表图例文本仍为10

谢谢!

1 个答案:

答案 0 :(得分:0)

.font没有Legend()属性。

Legend()的{​​{3}}状态:

  

txPr(别名:textProperties)

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

您需要像这样修改图例:

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

(受documentation 启发)