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 效果:

