Openpyxl chage font size title & y_axis.title

I am currently struggling with changing the font of the y axis name and the chart title itself.

I tried to create a font setting and apply it to the names - with no luck that it was.

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 a simple setup for this - for example:

chart.y_axis.title.some_size_attrib = 12

or am I in the wrong direction?

+4
source share
3 answers

I hope you will not be too late. After much research, I was able to find a way to change the font and its size from the chart segment using Openpyxl.

The font size is defined in sz = 1500, and this means the usual font size is 15. Using this 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)])
+3
source

, :

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
+1

. :

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
0
source

Source: https://habr.com/ru/post/1651881/


All Articles