python生成pptx,Presentation库

使用python生成简单的ppt图表

from pptx import Presentation
from pptx.chart.data import ChartData
from pptx.enum.chart import XL_CHART_TYPE
from pptx.util import Inches
 
# 创建幻灯片 ------
prs = Presentation()
slide = prs.slides.add_slide(prs.slide_layouts[5])
 
# 定义图表数据 ---------------------
chart_data = ChartData()
chart_data.categories = ['East', 'West', 'Midwest']
chart_data.add_series('Series 1', (19.2, 21.4, 16.7))
 
# 将图表添加到幻灯片 --------------------
x, y, cx, cy = Inches(2), Inches(2), Inches(6), Inches(4.5)
slide.shapes.add_chart(
  XL_CHART_TYPE.COLUMN_CLUSTERED, x, y, cx, cy, chart_data
)
 
prs.save(r'c:/Users/Administrator/Desktop/cesgu.pptx') #保存位置pptx

你可能感兴趣的:(python学习案例)