如何使用openpyxl设置刻度标签的字体?

问题描述 投票:0回答:1

我使用 openpyxl 创建了一个 Excel 图表。现在我尝试配置 x 轴刻度标签的字体。如何正确做?

a)这是我当前的代码:

from openpyxl import Workbook
from openpyxl.chart import BarChart, Reference
from openpyxl.chart.text import RichText

from openpyxl.drawing.text import Font, CharacterProperties


def main():
    wb = Workbook()
    chart = _create_openpyxl_chart(wb)

    _format_chart(chart)

    wb.save("example.xlsx")


def _format_chart(chart):
    font_type = 'Arial'
    font_size = 12
    font_style = 'bold'

    font = Font(typeface=font_type)
    character_properties = CharacterProperties(
        sz=font_size * 100,
        b=(font_style == 'bold'),
        latin=font,
    )

    axis = chart.x_axis

    if axis.textProperties is None:
        axis.textProperties = RichText()

    paragraph = axis.textProperties.paragraphs[0]
    text_run = paragraph.text[0]
    text_run.properties = character_properties


def _create_openpyxl_chart(wb):
    sheet = wb.active
    _create_example_data(sheet)
    chart = _create_stacked_bar_chart()
    _connect_chart_to_example_data(chart, sheet)
    sheet.add_chart(chart)

    return chart


def _connect_chart_to_example_data(chart, sheet):
    values = Reference(
        sheet,
        min_col=1,
        max_col=4,
        min_row=2,
        max_row=4,
    )
    chart.add_data(
        values,
        titles_from_data=True,
        from_rows=True,
    )
    categories = Reference(
        sheet,
        min_col=2,
        max_col=4,
        min_row=1,
        max_row=1,
    )
    chart.set_categories(categories)


def _create_stacked_bar_chart():
    chart = BarChart()
    chart.x_axis.title = "Year"
    chart.type = "col"
    chart.grouping = "stacked"
    chart.overlap = 100
    return chart


def _create_example_data(sheet):
    data = [
        ["Category", 2017, 2018, 2019],
        ["Apples", 10, 7, 12],
        ["Oranges", 5, 3, 4],
        ["Bananas", 8, 6, 9],
    ]
   
    for row in data:
        sheet.append(row)


if __name__ == '__main__':
    main()

b) 这是我从 Excel 文件中提取的示例 xml 部分:

...
</c:title>

<c:numFmt formatCode="General" sourceLinked="1"/>
<c:majorTickMark val="none"/>
<c:minorTickMark val="none"/>
<c:tickLblPos val="nextTo"/>
<c:txPr>
 <a:bodyPr/>
 <a:lstStyle/>
 <a:p>
  <a:pPr>
   <a:defRPr>
    <a:latin typeface="Arial" panose="020B0500000000000000" pitchFamily="34" charset="0"/>
   </a:defRPr>
  </a:pPr>
  <a:endParaRPr lang="en-US"/>
 </a:p>
</c:txPr>
python excel charts openpyxl styling
1个回答
0
投票

我已经很接近了。解决办法如下:

def _format_chart(chart):
    font_type = 'Arial'
    font_size = 12
    font_style = 'bold'

    font = Font(typeface=font_type)
    character_properties = CharacterProperties(
        sz=font_size * 100,
        b=(font_style == 'bold'),
        latin=font,
    )
    axis = chart.x_axis
        
    if axis.textProperties is None:
        axis.textProperties = RichText()
        
    paragraph = axis.textProperties.paragraphs[0]
        
    if paragraph.properties is None:
        paragraph.properties = ParagraphProperties()
        
    properties = paragraph.properties
        
    properties.defRPr = character_properties  # for defRPr no alias "defaultTextRunProperties" exists
© www.soinside.com 2019 - 2024. All rights reserved.