栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

ppt制作工具python-pptx 快速入门

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

ppt制作工具python-pptx 快速入门

hello
#!/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]

title.text = "Hello, World!"subtitle.text = "python-pptx 可以轻松制作powerpoint!"prs.save('test.pptx')

图片.png

Bullet
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]

title_shape.text = 'Adding a Bullet Slide'tf = body_shape.text_frame
tf.text = 'Find the bullet slide layout'p = tf.add_paragraph()
p.text = 'Use _Textframe.text for first bullet'p.level = 1p = tf.add_paragraph()
p.text = 'Use _Textframe.add_paragraph() for subsequent bullets'p.level = 2prs.save('test.pptx')

图片.png

并非所有shape都可以包含文本,但至少有段落。 _baseShape.has_text_frame可用于确定shape是否可以包含文本,所有shape都是_baseShape的子类 。当_baseShape.has_text_frame为True 时,_baseShape.text_frame.paragraphs[0]返回第一段。 可以使用text_frame.paragraphs[0].text设置文本。 _baseShape.text和_Textframe.text可以达到同样的效果。

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]

title_shape.text = 'Adding a Bullet Slide'tf = body_shape.text_frame
tf.text = 'Find the bullet slide layout'p = tf.add_paragraph()
p.text = 'Use _Textframe.text for first bullet'p.level = 1p = tf.add_paragraph()
p.text = 'Use _Textframe.add_paragraph() for subsequent bullets'p.level = 2prs.save('test.pptx')
参考资料
  • python测试开发项目实战-目录

  • python工具书籍下载-持续更新

  • python 3.7极速入门教程 - 目录

  • 讨论qq群630011153 144081101

  • 原文地址

  • 本文涉及的python测试开发库 谢谢点赞!

  • [本文相关海量书籍下载](https://github.com/china-testing/python-api-tesing/blob/master/books.md

字体
from pptx import Presentationfrom pptx.util import Inches, Pt

prs = Presentation()
blank_slide_layout = prs.slide_layouts[6]
slide = prs.slides.add_slide(blank_slide_layout)

left = top = width = height = Inches(1)
txBox = slide.shapes.add_textbox(left, top, width, height)
tf = txBox.text_frame

tf.text = "This is text inside a textbox"p = tf.add_paragraph()
p.text = "This is a second paragraph that's bold"p.font.bold = Truep = tf.add_paragraph()
p.text = "This is a third paragraph that's big"p.font.size = Pt(40)

prs.save('test.pptx')

图片.png

图片
#!/usr/bin/env python3# -*- coding: utf-8 -*-from pptx import Presentationfrom pptx.util import Inches

img_path = 'test.jpg'prs = Presentation()
blank_slide_layout = prs.slide_layouts[6]
slide = prs.slides.add_slide(blank_slide_layout)

left = top = Inches(1)
pic = slide.shapes.add_picture(img_path, left, top)

left = Inches(5)
height = Inches(5.5)
pic = slide.shapes.add_picture(img_path, left, top, height=height)

prs.save('test.pptx')

图片.png

Autoshape
#!/usr/bin/env python3# -*- coding: utf-8 -*-from pptx import Presentationfrom pptx.enum.shapes import MSO_SHAPEfrom 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

shapes.title.text = 'Adding an AutoShape'left = Inches(0.93)  # 0.93" centers this overall set of shapestop = Inches(3.0)
width = Inches(1.75)
height = Inches(1.0)

shape = shapes.add_shape(MSO_SHAPE.PENTAGON, left, top, width, height)
shape.text = 'Step 1'left = left + width - Inches(0.4)
width = Inches(2.0)  # chevrons need more width for visual balancefor n in range(2, 6):
    shape = shapes.add_shape(MSO_SHAPE.CHEVRON, left, top, width, height)
    shape.text = 'Step %d' % n
    left = left + width - Inches(0.4)

prs.save('test.pptx')

图片.png

表格
#!/usr/bin/env python3# -*- coding: utf-8 -*-from pptx import Presentationfrom 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

shapes.title.text = 'Adding a Table'rows = cols = 2left = top = Inches(2.0)
width = Inches(6.0)
height = Inches(0.8)

table = shapes.add_table(rows, cols, left, top, width, height).table# set column widthstable.columns[0].width = Inches(2.0)
table.columns[1].width = Inches(4.0)# write column headingstable.cell(0, 0).text = 'Foo'table.cell(0, 1).text = 'Bar'# write body cellstable.cell(1, 0).text = 'Baz'table.cell(1, 1).text = 'Qux'prs.save('test.pptx')

图片.png

提取文本
from pptx import Presentation

prs = Presentation(path_to_presentation)# text_runs will be populated with a list of strings,# one for each text run in presentationtext_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)



作者:python作业AI毕业设计
链接:https://www.jianshu.com/p/23734d34d788


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/220049.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号