利⽤Python制作PPT的完整步骤
⽬录
前⾔
步骤1:
步骤2:安装PIP3
步骤3:安装python-pptx
4.写代码测试:
总结
前⾔
怎么,你还没学Python吗? 此⼀时彼以时,什么C,JAVA,现在在求职市场都是渣渣,铺天盖地的Python学习⼴告,⼀遍⼜⼀遍地提醒着着我,你已经⽼了:
⽼板说:你很努⼒,但我还是想提拔会Python的⼈。
员⼯说:⾃从学了Python,腰不疼了,腿不酸了,颈椎不痛了,连⼯资都涨了。。。
码农说:我要偷偷学Python,惊呆所有⼈!
。。。。。。
所以,为了不被时代滚滚洪流淘汰,争取抓上时代前进的末班车,我也要学Python了,从最实⽤的-⽂案(吹)PPT(⽜逼)做起。
下⾯开始技术流。
redis是前端还是后端
步骤1:
Python存在不同版本之间的兼容性问题,所以有必要说明⼀下版本,这⾥我在Ubuntu系统上使⽤Python3为例,如果你的系统不是Python3.请按照如下步骤修改:
caozilong@caozilong-Vostro-3268:/usr/bin$ sudo rm python
caozilong@caozilong-Vostro-3268:/usr/bin$ sudo ln -s /usr/bin/python3 /usr/bin/python
caozilong@caozilong-Vostro-3268:/usr/bin$
步骤2:安装PIP3
安装PIP3的⽬的是为了安装 Python PPT插件,作为Python3上的包管理⼯具,PIP3不可或缺。
caozilong@caozilong-Vostro-3268:~/Workspace/python-ppt$ sudo apt install python3-pip
[sudo] caozilong 的密码:
正在读取软件包列表... 完成
正在分析软件包的依赖关系树
正在读取状态信息... 完成
python3-pip 已经是最新版 (9.0.1-2.3~ubuntu1.18.04.5)。
下列软件包是⾃动安装的并且现在不需要了:
libegl1-mesa libfwup1 libllvm9 python-kerberos
使⽤'sudo apt autoremove'来卸载它(它们)。
升级了 0 个软件包,新安装了 0 个软件包,要卸载 0 个软件包,有 10 个软件包未被升级。
java经典教程课本caozilong@caozilong-Vostro-3268:~/Workspace/python-ppt$
步骤3:安装python-pptx
caozilong@caozilong-Vostro-3268:~/Workspace/python-ppt$ sudo pip3 install python-pptx
The directory '/home/caozilong/.cache/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag. The directory '/home/caozilong/.cache/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag. Collecting python-pptx
Downloading /packages/53/ed/547be9730350509253bc7d76631a8ffcd1a62dda4d7482fb25d369696e37/python-pptx-0.6. (9.3MB)
100% |████████████████████████████████| 9.3MB 180kB/s
Requirement already satisfied: Pillow>=3.3.2 in /usr/lib/python3/dist-packages (from python-pptx)
Collecting XlsxWriter>=0.5.7 (from python-pptx)
Downloading /packages/93/51/11cb4545e95f1a845a6ca5475eb425272dc32c2f0e3592d80e7abd491374/XlsxWriter-1.4.4-py2.py3-none-any.whl (149kB)
100% |████████████████████████████████| 153kB 5.9MB/s
Collecting lxml>=3.1.0 (from python-pptx)
Downloading /packages/1f/1d/a4485412268b38043a6c0f873245b5d9315c6615bcf44776759a2605dca5/lxml-4.6.3-cp36-cp36m-manylinux1_x86_64.whl (5.5MB)
100% |████████████████████████████████| 5.5MB 295kB/s
Installing collected packages: XlsxWriter, lxml, python-pptx
Running setup.py install for python-pptx ... done
Successfully installed XlsxWriter-1.4.4 lxml-4.6.3 python-pptx-0.6.19
caozilong@caozilong-Vostro-3268:~/Workspace/python-ppt$
4.写代码测试:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
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]
< = "Hello, World!"
< = "python-pptx 可以轻松制作powerpoint!"
prs.save('czl.pptx')
执⾏:
caozilong@caozilong-Vostro-3268:~/Workspace/python-ppt$ python py1.py
caozilong@caozilong-Vostro-3268:~/Workspace/python-ppt$ ls -l
总⽤量 32
-rw-rw-r-- 1 caozilong caozilong 28292 7⽉  18 15:51 czl.pptx
-rw-rw-r-- 1 caozilong caozilong  362 7⽉  18 15:51 py1.py
caozilong@caozilong-Vostro-3268:~/Workspace/python-ppt$
⽤WPS打开czl.pptx.
from pptx import Presentation
prs = Presentation()
bullet_slide_layout = prs.slide_layouts[1]
slide = prs.slides.add_slide(bullet_slide_layout)
shapes = slide.shapes
title_shape = shapes.title易论坛
body_shape = shapes.placeholders[1]
= 'Adding a Bullet Slide'
tf = _frame
< = 'Find the bullet slide layout'
p = tf.add_paragraph()
< = 'Use _ for first bullet'
p.level = 1
p = tf.add_paragraph()
< = 'Use _TextFrame.add_paragraph() for subsequent bullets' p.level = 2
prs.save('czl.pptx')
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from pptx import Presentation
um.shapes import MSO_SHAPE
from pptx.util import Inches
prs = Presentation()
title_only_slide_layout = prs.slide_layouts[5]
slide = prs.slides.add_slide(title_only_slide_layout)
shapes = slide.shapes
= 'Adding an AutoShape'
html超链接打开本地文件left = Inches(0.93)  # 0.93" centers this overall set of shapes
top = Inches(3.0)
width = Inches(1.75)
height = Inches(1.0)
shape = shapes.add_shape(MSO_SHAPE.PENTAGON, left, top, width, height) = 'Step 1'
left = left + width - Inches(0.4)
width = Inches(2.0)  # chevrons need more width for visual balance
for n in range(2, 6):
shape = shapes.add_shape(MSO_SHAPE.CHEVRON, left, top, width, height)    = 'Step %d' % n
left = left + width - Inches(0.4)
prs.save('test.pptx')
from pptx import Presentation
from pptx.chart.data import CategoryChartData
um.chart import XL_CHART_TYPE
from pptx.util import Inches
# create presentation with 1 slide ------python入门教程 ppt
prs = Presentation()
slide = prs.slides.add_slide(prs.slide_layouts[5])
# define chart data ---------------------
chart_data = CategoryChartData()
chart_data.categories = ['East', 'West', 'Midwest']
chart_data.add_series('Series 1', (19.2, 21.4, 16.7))
# add chart to slide --------------------
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')
from pptx import Presentation
from pptx.util import Inches
from pptx import Presentation
from pptx.chart.data import ChartData
um.chart import XL_CHART_TYPE
from pptx.util import Cm #Inches
um.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
= '报告'
# 定义表格数据 ------
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    # 设置单元格宽度
# 设置标题⾏
# 填充数据源码开源网
# 定义图表数据 ---------------------

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。