from pptx import Presentation
from pptx.util import Pt, Inches prs = Presentation() slide = prs.slides.add_slide(prs.slide_layouts[1])# 添加空白页PPT body_shape = slide.shapes.placeholders
body_shape[0].text = 'this is placeholders[0]' # 在第一个文本框中文字框架内添加文字
body_shape[1].text = 'this is placeholders[1]' # 在第二个文本框中文字框架内添加文字
print(len(slide.shapes.placeholders)) new_paragraph = body_shape[1].text_frame.add_paragraph() # 在第二个shape中的文本框架中添加新段落
new_paragraph.text = 'add_paragraph' # 新段落中文字
new_paragraph.font.bold = True # 文字加粗
new_paragraph.font.italic = True # 文字斜体
new_paragraph.font.size = Pt(15) # 文字大小
new_paragraph.font.underline = True # 文字下划线
new_paragraph.level = 1 # 新段落的级别 left = top = width = height = Inches(5) # 预设位置及大小
textbox = slide.shapes.add_textbox(left, top, width, height) # left,top为相对位置,width,height为文本框大小
textbox.text = 'this is a new textbox' # 文本框中文字
new_para = textbox.text_frame.add_paragraph() # 在新文本框中添加段落
new_para.text = 'this is second para in textbox' # 段落文字 img_path = 'dog.png' # 文件路径
left, top, width, height = Inches(1), Inches(4.5), Inches(2), Inches(2) # 预设位置及大小
pic = slide.shapes.add_picture(img_path, left, top, width, height) # 在指定位置按预设值添加图片 from pptx.enum.shapes import MSO_AUTO_SHAPE_TYPE
left, top, width, height = Inches(1), Inches(3), Inches(1.8), Inches(1) # 预设位置及大小
shape = slide.shapes.add_shape(MSO_AUTO_SHAPE_TYPE.PENTAGON, left, top, width, height) # 在指定位置按预设值添加类型为PENTAGON的形状
shape.text = 'Step 1'
for n in range(2, 6):
left = left + width - Inches(0.3)
shape = slide.shapes.add_shape(MSO_AUTO_SHAPE_TYPE.CHEVRON, left, top, width, height)
shape.text = 'Step{}'.format(n) rows, cols, left, top, width, height = 2, 2, Inches(3.5), Inches(4.5), Inches(6), Inches(0.8)
table = slide.shapes.add_table(rows, cols, left, top, width, height).table # 添加表格,并取表格类
table.columns[0].width = Inches(2.0) # 第一纵列宽度
table.columns[1].width = Inches(4.0) # 第二纵列宽度
table.cell(0, 0).text = 'text00' # 指定位置写入文本
table.cell(0, 1).text = 'text01'
table.cell(1, 0).text = 'text10'
table.cell(1, 1).text = 'text11' from pptx.chart.data import ChartData
from pptx.enum.chart import XL_CHART_TYPE
from pptx.enum.chart import XL_TICK_MARK
from pptx.dml.color import RGBColor
from pptx.enum.chart import XL_DATA_LABEL_POSITION
from pptx.enum.chart import XL_LEGEND_POSITION slide = prs.slides.add_slide(prs.slide_layouts[6]) # 在幻灯片中加入一页6号风格(空白)幻灯片 # chart1 左上方图
x, y, cx, cy = Inches(0.5), Inches(0.5), Inches(4), Inches(3) # 按英尺标准指定x,y值 chart_data = ChartData() # 图表data类 chart_data.categories = [u'A班级得分率', u'B班级得分率'] # 图表加入两栏
chart_data.add_series(u'得分率对比', (80.5, 60.5)) # 在两栏分别填入数据 graphic_frame = slide.shapes.add_chart(
XL_CHART_TYPE.COLUMN_CLUSTERED, x, y, cx, cy, chart_data
) # add_chart(图表类型,xy表示图表位置,cx cy表示图表宽高,并且插入chart_data中规定好的数据) chart = graphic_frame.chart # 从生成的图表中取出图表类
chart.chart_style = 21 # 图表整体颜色风格 chart.has_title = True # 图表是否含有标题,默认为False
chart.chart_title.text_frame.clear() # 清除原标题
new_paragraph = chart.chart_title.text_frame.add_paragraph() # 添加一行新标题
new_paragraph.text = '得分率对比' # 新标题
new_paragraph.font.size = Pt(15) # 新标题字体大小 category_axis = chart.category_axis # category_axis 为chart的category控制类
category_axis.has_major_gridlines = True # 是否显示纵轴线
category_axis.tick_labels.font.italic = True # tick_labels为图表下标签,置为斜体
category_axis.tick_labels.font.size = Pt(15) # 下标签字体大小
category_axis.tick_labels.font.color.rgb = RGBColor(255, 0, 0) # 标签字体颜色 value_axis = chart.value_axis # value_axis 为chart的value控制类
value_axis.maximum_scale = 100.0 # 纵坐标最大值
value_axis.minimum_scale = 0.0 # 纵坐标最小值
value_axis.minor_tick_mark = XL_TICK_MARK.CROSS
value_axis.has_minor_gridlines = True tick_labels = value_axis.tick_labels # tick_labels 为chart的纵轴标签控制类
tick_labels.number_format = '0%' # 标签显示样式
tick_labels.font.bold = True # 字体加粗
tick_labels.font.size = Pt(14) # 字体大小
tick_labels.font.color.rgb = RGBColor(0, 255, 0) # 标签颜色 plot = chart.plots[0] # 取图表中第一个plot
plot.has_data_labels = True # 是否显示数据标签
data_labels = plot.data_labels # 数据标签控制类
data_labels.font.size = Pt(13) # 字体大小
data_labels.font.color.rgb = RGBColor(0, 0, 255) # 字体颜色
data_labels.position = XL_DATA_LABEL_POSITION.INSIDE_END # 字体位置 # chart 2 左下方图
x, y, cx, cy = Inches(0.5), Inches(3.5), Inches(4), Inches(3) # 按英尺标准指定x,y值
chart_data = ChartData()
chart_data.categories = ['A', 'B', 'C', 'D']
chart_data.add_series(u'A班级选项占比', (80, 10, 9, 10))
chart = slide.shapes.add_chart(
XL_CHART_TYPE.PIE, x, y, cx, cy, chart_data
).chart # PIE为饼状图 chart.has_legend = True # 是否含有下方的说明
chart.legend.position = XL_LEGEND_POSITION.BOTTOM
chart.legend.horz_offset = 0 # 说明位移量 [-1, 1] 默认为0 chart.plots[0].has_data_labels = True # 饼中是否写入数值
data_labels = chart.plots[0].data_labels
data_labels.number_format = '0%' # 数值显示格式
data_labels.position = XL_DATA_LABEL_POSITION.INSIDE_END # 数值布局方式 chart.has_title = True
chart.chart_title.text_frame.clear() # 清除原标题
new_paragraph = chart.chart_title.text_frame.add_paragraph() # 添加一行新标题
new_paragraph.text = 'A班级选项占比' # 新标题
new_paragraph.font.size = Pt(13) # 新标题字体大小 # chart 3 右下方图
x, y, cx, cy = Inches(5.5), Inches(4), Inches(4), Inches(3) # 按英尺标准指定x,y值
chart_data = ChartData()
chart_data.categories = ['A', 'B', 'C', 'D']
chart_data.add_series(u'B班级选项占比', (0.1, 0.2, 0.3, 0.4))
chart = slide.shapes.add_chart(
XL_CHART_TYPE.PIE, x, y, cx, cy, chart_data
).chart chart.has_legend = True
chart.legend.position = XL_LEGEND_POSITION.BOTTOM chart.plots[0].has_data_labels = True
data_labels = chart.plots[0].data_labels
data_labels.number_format = '0%'
data_labels.position = XL_DATA_LABEL_POSITION.INSIDE_END chart.has_title = True
chart.chart_title.text_frame.clear() # 清除原标题
new_paragraph = chart.chart_title.text_frame.add_paragraph() # 添加一行新标题
new_paragraph.text = 'B班级选项占比' # 新标题
new_paragraph.font.size = Pt(13) # 新标题字体大小 # chart 4 右上方图
x, y, cx, cy = Inches(5.5), Inches(0.5), Inches(4), Inches(3)
chart_data = ChartData()
chart_data.categories = ['', '1-3', '4-6', '7-9']
chart_data.add_series('', (50, 18, 30, 34))
chart = slide.shapes.add_chart(
XL_CHART_TYPE.PIE, x, y, cx, cy, chart_data
).chart chart.has_legend = True
chart.legend.position = XL_LEGEND_POSITION.BOTTOM
chart.legend.font.size = Pt(13) chart.plots[0].has_data_labels = True
data_labels = chart.plots[0].data_labels
data_labels.number_format = '0%'
data_labels.position = XL_DATA_LABEL_POSITION.INSIDE_END chart.has_title = True
chart.chart_title.text_frame.clear()
new_title = chart.chart_title.text_frame.add_paragraph()
new_title.text = '得分占比'
new_title.font.size = Pt(13) prs.save('python-pptx.pptx') 从演示文稿中的幻灯片中提取所有文本
from pptx import Presentation prs = Presentation('教育行业通案模板 1.pptx') # text_runs will be populated with a list of strings,
# one for each text run in presentation
text_runs = [] for slide in prs.slides:
for shape in slide.shapes:
if not shape.has_text_frame:
continue
for paragraph in shape.text_frame.paragraphs:
for run in paragraph.runs:
text_runs.append(run.text)
print(text_runs)

python 操作PPT练习的更多相关文章

  1. python操作word、ppt的详解

    python使用win32com的心得   python可以使用一个第三方库叫做win32com达到操作com的目的, 我是安装了ActivePython的第三方库,从官网下载了安装包,该第三方库几乎 ...

  2. python操作word

    python教程(百度经验) Python 操作Word(Excel.PPT等通用)   import win32comfrom win32com.client import Dispatch, co ...

  3. Python(九) Python 操作 MySQL 之 pysql 与 SQLAchemy

    本文针对 Python 操作 MySQL 主要使用的两种方式讲解: 原生模块 pymsql ORM框架 SQLAchemy 本章内容: pymsql 执行 sql 增\删\改\查 语句 pymsql ...

  4. Python 【第六章】:Python操作 RabbitMQ、Redis、Memcache、SQLAlchemy

    Memcached Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载.它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提高动态.数据库驱动网站的速度 ...

  5. 练习:python 操作Mysql 实现登录验证 用户权限管理

    python 操作Mysql 实现登录验证 用户权限管理

  6. Python操作MySQL

    本篇对于Python操作MySQL主要使用两种方式: 原生模块 pymsql ORM框架 SQLAchemy pymsql pymsql是Python中操作MySQL的模块,其使用方法和MySQLdb ...

  7. Python操作Mysql之基本操作

    pymysql python操作mysql依赖pymysql这个模块 下载安装 pip3 install pymysql 操作mysql python操作mysql的时候,是通过”游标”来进行操作的. ...

  8. Python操作RabbitMQ

    RabbitMQ介绍 RabbitMQ是一个由erlang开发的AMQP(Advanced Message Queue )的开源实现的产品,RabbitMQ是一个消息代理,从“生产者”接收消息并传递消 ...

  9. python操作日期和时间的方法

    不管何时何地,只要我们编程时遇到了跟时间有关的问题,都要想到 datetime 和 time 标准库模块,今天我们就用它内部的方法,详解python操作日期和时间的方法.1.将字符串的时间转换为时间戳 ...

随机推荐

  1. 软工实践-Alpha 冲刺 (8/10)

    队名:起床一起肝活队 组长博客:博客链接 作业博客:班级博客本次作业的链接 组员情况 组员1(队长):白晨曦 过去两天完成了哪些任务 描述: 已经解决登录注册等基本功能的界面. 完成非功能的主界面制作 ...

  2. HDU 5875 Function 优先队列+离线

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5875 Function Time Limit: 7000/3500 MS (Java/Others) ...

  3. <<世界是数字的>>读书笔记

    <世界是数字的>这本书是大学职业规划老师介绍个我读的,从着本中我学到了很多. 第一章,计算机里有什么.这个问题可以从两方面来看:逻辑上或者说功能上的组成,即每一部分是什么.做什么.怎样做. ...

  4. java沙盒入门(2)

    Java在Internet上的应用已经日渐普遍,使用在网页上的Java程序称之为applet,利用Applet的嵌入能够使原本静态的HTML富有变化,并且能够做到"声"." ...

  5. jQuery的滚动监听

    jQuery的滚动监听 1.当前滚动的地方的窗口顶端到整个页面顶端的距离: var winPos = $(window).scrollTop(); 2.获取指定元素的页面位置: $(val).offs ...

  6. Spring Boot 学习笔记 - 01

    看了[纯洁的微笑]的博客后,我决定开始学好 Spring 体系了,真的是解决了饥渴的我.

  7. Android内存泄漏第一课【转】--------(使用单例模式造成的内存泄漏)

    使用单例模式造成的内存泄漏 Android的单例模式在我们项目开发中经常会用到,不过使用的不恰当的话也会造成内存泄漏.因为单例的静态特性使得单例的生命周期和应用的生命周期一样长, 这就说明了如果一个对 ...

  8. 第188天:extend拷贝创建对象的原理

    一.拷贝创建对象的原理 //拷贝创建对象核心代码 function extend(target,source) { //遍历对象 for(var i in source){ target[i] = s ...

  9. XTU 1233 Coins(DP)

    题意: n个硬币摆成一排,问有连续m个正面朝上的硬币的序列种数. 很明显的DP题.定义状态dp[i][1]表示前i个硬币满足条件的序列种数.dp[i][0]表示前i个硬币不满足条件的序列种数. 那么显 ...

  10. BZOJ4953 Wf2017Posterize(动态规划)

    设f[i][j]为前i种强度选了j种且其中第i种选时前i个的最小误差.转移枚举上个选啥前缀和优化即可. #include<iostream> #include<cstdio> ...