当前位置: 移动技术网 > IT编程>脚本编程>Python > python生成ppt的方法

python生成ppt的方法

2018年08月19日  | 移动技术网IT编程  | 我要评论

跨行转账需要多长时间,铲形币是哪国的,王一恒

本文主要介绍如何通过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')

本文用到的源码

下载地址

利用场景

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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网