openpyxl chage font size of title & y_axis.title

谁都会走 提交于 2020-01-14 10:09:06

问题


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?


回答1:


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



回答2:


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



回答3:


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


来源:https://stackoverflow.com/questions/39054631/openpyxl-chage-font-size-of-title-y-axis-title

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!