如何设置使用openpyxl模块绘制的图表标题的字体大小

时间:2017-04-05 09:42:01

标签: python-3.x openpyxl

我正在使用openpyxl模块绘制可用分析数据的图表。我能够绘制图形,但我无法找到更改图表标题字体大小的选项。默认情况下,它的字体大小为'18'。在openpyxl中,openpyxl.Styles模块具有“字体”选项。使用它,我们可以更改单元格中可用的数据字体。但是,不是图表标题的字体大小。任何人都可以帮忙...

这是我的代码:

from openpyxl import Workbook
from openpyxl.styles import Style, Font
from openpyxl.chart import (
    AreaChart,
    Reference,
    Series,
)
wb = Workbook()
ws = wb.active
rows = [
    ['Number', 'Batch 1', 'Batch 2'],
    [2, 40, 30],
    [3, 40, 25],
    [4, 50, 30],
    [5, 30, 10],
    [6, 25, 5],
    [7, 50, 10],
]
for row in rows:
    ws.append(row)

chart = AreaChart()
chart.title = "Area Chart"
chart.style = 20
chart.x_axis.title = 'Test'
chart.y_axis.title = 'Percentage'
chart.x_axis.Font = 10 -- /# Trying like this to change the font size. But throwing error #/
chart.y_axis.Font = 10

cats = Reference(ws, min_col=1, min_row=1, max_row=7)
data = Reference(ws, min_col=2, min_row=1, max_col=3, max_row=7)
chart.add_data(data, titles_from_data=True)
chart.set_categories(cats)

ws.add_chart(chart, "A10")
wb.save("area.xlsx")

请在这方面帮助我

1 个答案:

答案 0 :(得分:2)

尝试使用CharacterProperties整数sz(大小)值的以下代码段:

from openpyxl.drawing.text import CharacterProperties
chart.x_axis.title.tx.rich.p[0].r.rPr = CharacterProperties(sz=3500)