openpyxl chage font size of title & y_axis.title

2019-06-23 16:58发布

I am currently struggling with changing the font of y axis title & the charts title itself.

I have tried to create a font setting & applying it to the titles - with no luck what so ever.

new_chart.y_axis.title = chart_dict['y_title']
ft = Font(name='Calibri',
          size=11,
          bold = False,
          italic = False,
          vertAlign = None,
          underline = 'none',
          strike = False,
          color = 'FF000000')

new_chart.y_axis.title.font = ft

Is there any easy setting for this - like:

chart.y_axis.title.some_size_attrib = 12

or am I in the wrong direction?

3条回答
聊天终结者
2楼-- · 2019-06-23 17:41

I hope it won't get you too late. After a lot of research I was able to find a way to change the font and its size from a chart segment using Openpyxl.

The size of the font is defined at the sz=1500 and this means the usual 15 font size. Using that logic 1200 is 12. The minimum is 100 and the maximum is 400000.

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

font_test = Font(typeface='Calibri')
cp = CharacterProperties(latin=font_test, sz=1500)
chart.x_axis.txPr = RichText(p=[Paragraph(pPr=ParagraphProperties(defRPr=cp), endParaRPr=cp)])
查看更多
ゆ 、 Hurt°
3楼-- · 2019-06-23 17:42

In my case that was not working properly. The one I used in the end was:

from openpyxl.drawing.text import CharacterProperties

cp = CharacterProperties(sz=1100)  # Where size goes from 100 till 40000
mygraph.x_axis.title.tx.rich.p[0].r.rPr = cp
查看更多
Anthone
4楼-- · 2019-06-23 18:02

In my case, neither of these answers worked, so I did this:

from openpyxl.drawing.text import CharacterProperties, Paragraph, ParagraphProperties, RegularTextRun

cp = CharacterProperties(sz=1200)
xtStr = u"X-axis Title"
ytStr = u"Y-axis Title"
myChart.x_axis.title = ""
myChart.y_axis.title = ""
xPara = [Paragraph(pPr=ParagraphProperties(defRPr=cp), r=RegularTextRun(t=s)) for s in xtStr.split("\n")]
yPara = [Paragraph(pPr=ParagraphProperties(defRPr=cp), r=RegularTextRun(t=s)) for s in ytStr.split("\n")]
myChart.x_axis.title.tx.rich.paragraphs = xPara
myChart.y_axis.title.tx.rich.paragraphs = yPara
查看更多
登录 后发表回答