" name="sm-site-verification"/>
侧边栏壁纸
博主头像
PySuper 博主等级

千里之行,始于足下

  • 累计撰写 206 篇文章
  • 累计创建 14 个标签
  • 累计收到 2 条评论

目 录CONTENT

文章目录

Python 操作/生成 PPT

PySuper
2020-01-03 / 0 评论 / 0 点赞 / 28 阅读 / 0 字
温馨提示:
本文最后更新于2024-05-28,若内容或图片失效,请留言反馈。 所有牛逼的人都有一段苦逼的岁月。 但是你只要像SB一样去坚持,终将牛逼!!! ✊✊✊

通过base64生成图片

import base64
import re

url = "data:image/png;base64,iVBO...AAAABJRU5ErkJggg=="

str_bas64 = re.match(r'(data:image/png;base64,(.*))', url).group(2)
img = base64.b64decode(str_bas64)
fh = open('pic.jpg', 'wb')
fh.write(img)
fh.close()

环境配置

pip install python-pptx

Python-pptx-Presentations

from pptx import Presentation

# 加载一个ppt文件
prs = Presentation('PPT/zxt.pptx')

# 在第一张幻灯片中获得对第一个形状的引用
sp = prs.slides[0].shapes[0]

# 向第一张幻灯片中添加图片形状
pic = prs.slides[0].shapes.add_picture('image/zxt_save_data.jpg', 100, 100, 100, 100)

# 此演示文稿的NotesMaster实例。如果演示文稿没有便笺母版,
# 则从默认模板创建一个便笺本并将其返回。每次调用都返回相同的单个实例。
note = prs.notes_master

# 本演示文稿中的幻灯片高度,以英制公制(EMU)为单位。
# None如果未定义幻灯片宽度,则返回。读/写。
height = prs.slide_height

# 属于此演示文稿的第一个SlideMaster的SlideLayout实例的序列。
# 一个演示文稿可以有多个幻灯片母版,每个母版都有自己的一组布局。
# 对于演示文稿只有一个幻灯片母版的常见情况,此属性很方便。
layouts = prs.slide_layouts

# 属于此演示文稿的第一个SlideMaster对象。
# 通常,演示文稿只有一个幻灯片母版。
# 在这种常见情况下,此属性提供了更简单的访问。
master = prs.slide_master

# 属于此演示文稿的SlideMaster对象的序列
masters = prs.slide_masters

# 本演示文稿中的幻灯片宽度,以英语公制单位(EMU)为单位。
# 如果未定义幻灯片宽度,则返回None。读/写。
width = prs.slide_width

# slides对象,其中包含此演示文稿中的幻灯片
slides = prs.slides

# 每个Presentation对象都有一个CoreProperties通过其core_properties属性访问的对象,
# 该属性提供对文档的所谓核心属性的读/写访问
core_properties = prs.core_properties

core_properties.author = 'HaI'
core_properties.category = 'python-pptx'
core_properties.comments = 'HaI\'s comments'
core_properties.content_status = "draft"
core_properties.identifier = 'HaI\'s identifier'
core_properties.keywords = 'HaI\'s keywords'
core_properties.language = 'utf-8'
core_properties.last_modified_by = 'HaI\'s last_modified_by'
core_properties.subject = 'HaI\'s subject'
core_properties.title = 'HaI\'s title'
core_properties.version = 'v1.0.0'

# string –主要负责制作资源内容的作者。
author = core_properties.author
# string –此软件包内容的分类。值示例包括:简历,信函,财务预测,提案或技术演示。
category = core_properties.category
# string –资源内容的帐户。
comments = core_properties.comments
# string –文档的完成状态,例如“草稿”
content_status = core_properties.content_status
# datetime –最初创建文档的时间
created = core_properties.created
# string –在给定上下文(例如ISBN)中对资源的明确引用。
identifier = core_properties.identifier
# string –描述性词或短短语可能会用作本文档的搜索词
keywords = core_properties.keywords
# string -文档所用的语言
language = core_properties.language
# string –上次修改文档的人的姓名或其他标识符(例如电子邮件地址)
last_modified_by = core_properties.last_modified_by
# datetime –文档上次打印的时间
last_printed = core_properties.last_printed
# datetime –文档上次修改的时间
modified = core_properties.modified
# int –此修订版的编号,每次保存文档时,PowerPoint®客户端将其递增一次。
# 但是请注意,版本号不会由python-pptx自动增加。
revision = core_properties.revision
# string –资源内容的主题。
subject = core_properties.subject
# string –给资源的名称。
title = core_properties.title
# string –自由格式的字符串
version = core_properties.version

prs.save('pptx/zf-01.pptx')
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 = ['0', '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)
0
  1. 支付宝打赏

    qrcode alipay
  2. 微信打赏

    qrcode weixin

评论区