简体   繁体   English

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

[英]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. 使用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)

and my chart legend text is still size 10 我的图表图例文字仍然是10

thanks!! 谢谢!!

There is no .font attribute for Legend() . Legend()没有.font属性。

The documentation for Legend() states: Legend()文档说明:

txPr (alias: textProperties) txPr(别名: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 ) (受openpyxl格式化图表数据标签的启发

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM