python生成ppt的方法

yipeiwu_com5年前Python基础

本文主要介绍如何通过python生成ppt文件,以及借助ppt模板来生成ppt

环境

python 3
python-pptx

安装

pip3 install python-pptx

将文字输出到ppt

效果图

代码

from pptx import Presentation

# 创建幻灯片 ------
prs = Presentation()
title_slide_layout = prs.slide_layouts[0]
slide = prs.slides.add_slide(title_slide_layout)

title = slide.shapes.title
subtitle = slide.placeholders[1]

# 设置标题和副标题
title.text = "Hello, World!"
subtitle.text = "pip install python-pptx"

prs.save("test.pptx")

图表输出到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('chart-01.pptx')

使用ppt模板来生成ppt

  • 准备ppt模板(网络下载或自定义幻灯片母版)
  • 加载ppt模板,并使用指定幻灯片样式
  • 添加数据并生成新ppt

效果图

代码

from pptx import Presentation
from pptx.util import Inches
from pptx import Presentation
from pptx.chart.data import ChartData
from pptx.enum.chart import XL_CHART_TYPE
from pptx.util import Cm #Inches
from pptx.enum.chart import XL_LEGEND_POSITION

if __name__ == '__main__':
  # 创建幻灯片 ------
  prs = Presentation('template.pptx')
  title_only_slide_layout = prs.slide_layouts[5]
  slide = prs.slides.add_slide(title_only_slide_layout)
  shapes = slide.shapes

  shapes.title.text = '报告'

  # 定义表格数据 ------
  name_objects = ["object1", "object2", "object3"]
  name_AIs = ["AI1", "AI2", "AI3"]
  val_AI1 = (19.2, 21.4, 16.7)
  val_AI2 = (22.3, 28.6, 15.2)
  val_AI3 = (20.4, 26.3, 14.2)
  val_AIs = [val_AI1, val_AI2, val_AI3]

  # 表格样式 --------------------
  rows = 4
  cols = 4
  top  = Cm(12.5)
  left  = Cm(3.5) #Inches(2.0)
  width = Cm(24) # Inches(6.0)
  height = Cm(6) # Inches(0.8)

  # 添加表格到幻灯片 --------------------
  table = shapes.add_table(rows, cols, left, top, width, height).table

  # 设置单元格宽度
  table.columns[0].width = Cm(6)# Inches(2.0)
  table.columns[1].width = Cm(6)
  table.columns[2].width = Cm(6)
  table.columns[3].width = Cm(6)

  # 设置标题行
  table.cell(0, 1).text = name_objects[0]
  table.cell(0, 2).text = name_objects[1]
  table.cell(0, 3).text = name_objects[2]

  # 填充数据
  table.cell(1, 0).text = name_AIs[0]
  table.cell(1, 1).text = str(val_AI1[0])
  table.cell(1, 2).text = str(val_AI1[1])
  table.cell(1, 3).text = str(val_AI1[2])

  table.cell(2, 0).text = name_AIs[1]
  table.cell(2, 1).text = str(val_AI2[0])
  table.cell(2, 2).text = str(val_AI2[1])
  table.cell(2, 3).text = str(val_AI2[2])

  table.cell(3, 0).text = name_AIs[2]
  table.cell(3, 1).text = str(val_AI3[0])
  table.cell(3, 2).text = str(val_AI3[1])
  table.cell(3, 3).text = str(val_AI3[2])

  # 定义图表数据 ---------------------
  chart_data = ChartData()
  chart_data.categories = name_objects
  chart_data.add_series(name_AIs[0], val_AI1)
  chart_data.add_series(name_AIs[1], val_AI2)
  chart_data.add_series(name_AIs[2], val_AI3)

  # 添加图表到幻灯片 --------------------
  x, y, cx, cy = Cm(3.5), Cm(4.2), Cm(24), Cm(8)

  graphic_frame = slide.shapes.add_chart(
    XL_CHART_TYPE.COLUMN_CLUSTERED, x, y, cx, cy, chart_data
    )

  chart = graphic_frame.chart

  chart.has_legend = True
  chart.legend.position = XL_LEGEND_POSITION.TOP
  chart.legend.include_in_layout = False

  value_axis = chart.value_axis
  value_axis.maximum_scale = 100.0

  value_axis.has_title = True
  value_axis.axis_title.has_text_frame = True
  value_axis.axis_title.text_frame.text = "False positive"
  value_axis.axis_title.text_frame.auto_size

  prs.save('test_template.pptx')

本文用到的源码

下载地址

利用场景

  • 周期性数据汇总及报表生成
  • 业务或项目等数据可视化

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

Python设置matplotlib.plot的坐标轴刻度间隔以及刻度范围

Python设置matplotlib.plot的坐标轴刻度间隔以及刻度范围

一、用默认设置绘制折线图 import matplotlib.pyplot as plt x_values=list(range(11)) #x轴的数字是0到10这11个整数 y...

Python数据结构与算法之二叉树结构定义与遍历方法详解

本文实例讲述了Python数据结构与算法之二叉树结构定义与遍历方法。分享给大家供大家参考,具体如下: 先序遍历,中序遍历,后序遍历 ,区别在于三条核心语句的位置 层序遍历  采...

python三引号输出方法

python三引号输出方法

和C语言一样,引号属于特殊功能字符,不能够像普通字符那样直接通过print打印,需要进行一些处理,比如说反斜杠转义等。这里介绍几种打印三引号的方法,希望对需要的朋友有用。 1、第一中方法...

Python中特殊函数集锦

Python中特殊函数集锦

以下内容主要针过滤函数filter , 映射和归并函数map/reduce , 装饰器@ 以及 匿名函数lamda,具体内容如下: 1. 过滤函数filte...

Python嵌套式数据结构实例浅析

本文实例讲述了Python嵌套式数据结构。分享给大家供大家参考,具体如下: 嵌套式数据结构指的是:字典存储在列表中, 或者列表作为值存储在字典中。甚至还可以在字典中嵌套字典。 1 字典列...