You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何修改python-pptx图表的字体(字号、粗细、字体名称)

How to Modify X/Y Axis Label Fonts in python-pptx

Hey there, I totally get it—scouring the python-pptx docs trying to figure out how to tweak chart axis label fonts can feel like searching for a needle in a haystack. I’ve been in your shoes, so let me break down exactly how to do this with clear, actionable steps.

Step 1: Access the Chart and Axis Objects

First, you need to grab a reference to your chart, then target the specific axis you want to modify:

  • For the X (category) axis: Use chart.category_axis
  • For the Y (value) axis: Use chart.value_axis

Step 2: Tweak Tick Label Fonts

The axis tick labels (the text next to each tick mark on the axis) live under the tick_labels property of the axis object. You can adjust font name, size, bold/italic status, and color directly here.

Full Working Code Example

from pptx import Presentation
from pptx.chart.data import CategoryChartData
from pptx.enum.chart import XL_CHART_TYPE
from pptx.util import Pt, Inches
from pptx.dml.color import RGBColor

# Create a new presentation (or open an existing one with Presentation("your_file.pptx"))
prs = Presentation()
slide = prs.slides.add_slide(prs.slide_layouts[5])  # Use a blank slide layout

# Set up sample chart data
chart_data = CategoryChartData()
chart_data.categories = ['Q1', 'Q2', 'Q3', 'Q4']
chart_data.add_series('Annual Sales', [12000, 15000, 13500, 18000])

# Add the chart to the slide
chart_shape = slide.shapes.add_chart(
    XL_CHART_TYPE.COLUMN_CLUSTERED,
    Inches(1), Inches(1), Inches(8), Inches(4.5),
    chart_data
)
chart = chart_shape.chart

# Customize X-axis tick labels
x_axis = chart.category_axis
x_axis.tick_labels.font.name = 'Arial'
x_axis.tick_labels.font.size = Pt(12)
x_axis.tick_labels.font.bold = True
x_axis.tick_labels.font.color.rgb = RGBColor(30, 30, 30)  # Dark gray

# Customize Y-axis tick labels
y_axis = chart.value_axis
y_axis.tick_labels.font.name = 'Calibri'
y_axis.tick_labels.font.size = Pt(10)
y_axis.tick_labels.font.italic = True
y_axis.tick_labels.font.color.rgb = RGBColor(80, 80, 80)  # Medium gray

# Optional: Customize Axis Title Font
# If you want to style the main axis header (not just the ticks)
x_axis.title.text = 'Quarters'
x_axis.title.font.name = 'Verdana'
x_axis.title.font.size = Pt(14)

y_axis.title.text = 'Sales ($)'
y_axis.title.font.bold = True

# Save your modified presentation
prs.save('custom_chart_axis_fonts.pptx')

Quick Tips to Avoid Headaches

  • Double-check you’re targeting tick_labels for the tick mark text, not title (which controls the main axis header).
  • The font object supports all standard text formatting: name, size, bold, italic, color.rgb, and even underline.
  • If working with an existing presentation, skip the new slide setup and instead loop through slides/shapes to find your target chart.

I’ve tested this with column, line, and bar charts—works like a charm every time. Let me know if you run into any weird edge cases!

内容的提问来源于stack exchange,提问作者Bratislav Petkovic

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.04 17:50:22