基于python-pptx库中文文档及使用详解

个人使用样例及部分翻译自官方文档,并详细介绍chart的使用

一:基础应用

1.创建pptx文档类并插入一页幻灯片

from pptx import Presentation
prs = Presentation()
slide = prs.slides.add_slide(prs.slide_layouts[1])
# 对ppt的修改
prs.save('python-pptx.pptx')

prs.slide_layouts中一共预存有1-48种,采用第六种为空白幻灯片

例slide_layouts[1]为带标题和正文框的ppt,slide_layouts[6]为空白页ppt

slide 及为一页‘幻灯片类'

修改完后 prs.save('name.pptx') 保存ppt

2.在创建的这页幻灯片文本框中添加文字

body_shape = slide.shapes.placeholders # body_shape为本页ppt中所有shapes
body_shape[0].text = 'this is placeholders[0]' # 在第一个文本框中文字框架内添加文字
body_shape[1].text = 'this is placeholders[1]' # 在第二个文本框中文字框架内添加文字

在ppt中所有的元素均被当成一个shape,slide.shapes表示幻灯片类中的模型类,placeholders中为每个模型,采用slide_layouts[1]中包含两个文本框,所以print len(slide.shapes.placeholders) 话为 2。

title_shape = slide.shapes.title # 取本页ppt的title
title_shape.text = 'this is a title' # 向title文本框写如文字
subtitle = slide.shapes.placeholders[1] # 取出本页第二个文本框
subtitle.text = 'this is a subtitle' # 在第二个文本框中写入文字

由于采用的slide_layouts[1]包含一个标题和一个正文框,所以可以直接取slide.shapes.title 表示标题框写入文字亦可

3.在文本框中添加新段落

from pptx.util import Pt
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 # 新段落的级别

add_paragraph中的文字支持修改font

pptx.util 中为Pt为文字大小设置

4.添加新文本框

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' # 段落文字

5.添加图片

img_path = 'img_path.jpg' # 文件路径
left, top, width, height = Inches(1), Inches(4.5), Inches(2), Inches(2) # 预设位置及大小
pic = slide.shapes.add_picture(img_path, left, top, width, height) # 在指定位置按预设值添加图片

6.添加形状

from pptx.enum.shapes import MSO_SHAPE
left, top, width, height = Inches(1), Inches(3), Inches(1.8), Inches(1) # 预设位置及大小
shape = slide.shapes.add_shape(MSO_SHAPE.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_SHAPE.CHEVRON, left, top, width, height)
  shape.text = 'Step{}'.format(n)

MSO_SHAPE中有office中各类型形状,详见:https://msdn.microsoft.com/en-us/library/office/ff862770(v=office.15).aspx

7.添加表格

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'

8.demo

根据以上代码生成的一页幻灯片如下:

以上这篇基于python-pptx库中文文档及使用详解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持来客网。