Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

openpyxl chage font size of title & y_axis.title

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?

like image 422
Lev Avatar asked Aug 20 '16 13:08

Lev


2 Answers

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)])
like image 186
Leandro Reis Avatar answered Oct 19 '22 22:10

Leandro Reis


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
like image 41
Marco smdm Avatar answered Oct 20 '22 00:10

Marco smdm