python-pptx 实践 6.1:添加五种基本图形(柱形图、折线图、饼图、条形图、散点图)
0、import
import numpy as np
from pptx import Presentation
from pptx.util import Inches, Pt
from pptx.chart.data import ChartData, XyChartData
from pptx.dml.color import RGBColor
from pptx.enum.chart import XL_CHART_TYPE, XL_LEGEND_POSITION, XL_TICK_MARK, XL_LABEL_POSITION
# 创建幻灯片-------------------------------------------------
prs = Presentation() # 初始化 ppt 文档
title_only_slide_layout = prs.slide_layouts[5] # 选择空白幻灯片版式
slide = prs.slides.add_slide(title_only_slide_layout) # 添加一页空白幻灯片
shapes = slide.shapes
shapes.title.text = '简单柱形图'
# 定义图表数据-------------------------------------------------
x = ['Q1', 'Q2', 'Q3', 'Q4']
y = [8888, 8899, 7788, 9988]
chart_data = ChartData()
chart_data.categories = x
chart_data.add_series(name='销量', values=y)
# 添加图表-------------------------------------------------
left, top, width, height = Inches(0.5), Inches(1.5), Inches(9), Inches(6)
graphic_frame = shapes.add_chart(chart_type=XL_CHART_TYPE.COLUMN_CLUSTERED, # 簇状柱形图
x=left, y=top, # 图表区的位置
cx=width, cy=height, # 图表的宽和高
chart_data=chart_data)
# 保存 ppt 文档
prs.save('test.pptx')
ppt 效果:
# 创建幻灯片-------------------------------------------------
prs = Presentation() # 初始化 ppt 文档
title_only_slide_layout = prs.slide_layouts[5] # 选择空白幻灯片版式
slide = prs.slides.add_slide(title_only_slide_layout) # 添加一页空白幻灯片
shapes = slide.shapes
shapes.title.text = '折线图'
# 定义图表数据-------------------------------------------------
x = ['Q1', 'Q2', 'Q3', 'Q4']
y = [8888, 8899, 7788, 9988]
chart_data = ChartData()
chart_data.categories = x
chart_data.add_series(name='销量', values=y)
# 添加图表-------------------------------------------------
left, top, width, height = Inches(0.5), Inches(1.5), Inches(9), Inches(6)
graphic_frame = shapes.add_chart(chart_type=XL_CHART_TYPE.LINE, # 图表类型
x=left, y=top, # 图表区的位置
cx=width, cy=height, # 图表的宽和高
chart_data=chart_data)
# 保存 ppt 文档
prs.save('test.pptx')
ppt 效果:
# 创建幻灯片-------------------------------------------------
prs = Presentation() # 初始化 ppt 文档
title_only_slide_layout = prs.slide_layouts[5] # 选择空白幻灯片版式
slide = prs.slides.add_slide(title_only_slide_layout) # 添加一页空白幻灯片
shapes = slide.shapes
shapes.title.text = '饼图'
# 定义图表数据-------------------------------------------------
x = ['Q1', 'Q2', 'Q3', 'Q4']
y = [8888, 8899, 7788, 9988]
chart_data = ChartData()
chart_data.categories = x
chart_data.add_series(name='销量', values=y)
# 添加图表-------------------------------------------------
left, top, width, height = Inches(0.5), Inches(1.5), Inches(9), Inches(6)
graphic_frame = shapes.add_chart(chart_type=XL_CHART_TYPE.PIE, # 图表类型
x=left, y=top, # 图表区的位置
cx=width, cy=height, # 图表的宽和高
chart_data=chart_data)
chart = graphic_frame.chart
plot = chart.plots[0]
# 设置数据标签
plot.has_data_labels = True # 显示数据标签
data_labels = plot.data_labels # 获取数据标签控制类
data_labels.show_category_name = True # 是否显示类别名称
data_labels.show_value = False # 是否显示值
data_labels.show_percentage = True # 是否显示百分比
data_labels.number_format = '0.0%' # 标签的数字格式
data_labels.position = XL_LABEL_POSITION.INSIDE_END # 标签位置
data_labels.font.name = 'Arial'
data_labels.font.size = Pt(14)
# 设置图表标题
chart.has_title = True # 显示标题
para = chart.chart_title.text_frame.add_paragraph()
para.text = '销量占比' # 标题内容
para.font.size = Pt(16) # 字体大小
# 保存 ppt 文档
prs.save('test.pptx')
ppt 效果:
# 创建幻灯片-------------------------------------------------
prs = Presentation() # 初始化 ppt 文档
title_only_slide_layout = prs.slide_layouts[5] # 选择空白幻灯片版式
slide = prs.slides.add_slide(title_only_slide_layout) # 添加一页空白幻灯片
shapes = slide.shapes
shapes.title.text = '条形图'
# 定义图表数据-------------------------------------------------
x = ['Q1', 'Q2', 'Q3', 'Q4']
y = [8888, 8899, 7788, 9988]
chart_data = ChartData()
chart_data.categories = x
chart_data.add_series(name='销量', values=y)
# 添加图表-------------------------------------------------
left, top, width, height = Inches(0.5), Inches(1.5), Inches(9), Inches(6)
graphic_frame = shapes.add_chart(chart_type=XL_CHART_TYPE.BAR_CLUSTERED, # 图表类型
x=left, y=top, # 图表区的位置
cx=width, cy=height, # 图表的宽和高
chart_data=chart_data)
chart = graphic_frame.chart
plot = chart.plots[0]
# 设置数据标签
plot.has_data_labels = True # 显示数据标签
data_labels = plot.data_labels # 获取数据标签控制类
data_labels.number_format = '#,#' # 标签的数字格式
data_labels.position = XL_LABEL_POSITION.OUTSIDE_END # 标签位置
data_labels.font.name = 'Arial'
data_labels.font.size = Pt(14)
# 保存 ppt 文档
prs.save('test.pptx')
ppt 效果:
# 创建幻灯片-------------------------------------------------
prs = Presentation() # 初始化 ppt 文档
title_only_slide_layout = prs.slide_layouts[5] # 选择空白幻灯片版式
slide = prs.slides.add_slide(title_only_slide_layout) # 添加一页空白幻灯片
shapes = slide.shapes
shapes.title.text = '散点图'
# 定义图表数据-------------------------------------------------
np.random.seed(42)
x = np.random.randn(500)
np.random.seed(2)
y = np.random.randn(500)
chart_data = XyChartData()
# chart_data.categories = x
series = chart_data.add_series(name='高斯分布样本')
for px, py in zip(x, y):
if py > 0:
series.add_data_point(px, py)
# 添加图表-------------------------------------------------
left, top, width, height = Inches(0.5), Inches(1.5), Inches(9), Inches(6)
graphic_frame = shapes.add_chart(chart_type=XL_CHART_TYPE.XY_SCATTER, # 图表类型
x=left, y=top, # 图表区的位置
cx=width, cy=height, # 图表的宽和高
chart_data=chart_data)
# 保存 ppt 文档
prs.save('test.pptx')
ppt 效果:
非学无以广才,非志无以成学。