脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - 利用Python自动生成PPT的示例详解

利用Python自动生成PPT的示例详解

2022-07-14 18:41阿涛的一天 Python

在日常工作中,PPT制作是常见的工作。这篇文章主要为大家详细介绍了如何利用Python自动生成PPT,文中的示例代码讲解详细,感兴趣的可以了解一下

在日常工作中,PPT制作是常见的工作,如果制作创意类PPT,则无法通过自动化的形式生成,因为创意本身具有随机性,而自动化解决的是重复性工作,两者有所冲突。

python-pptx是python处理PPT的一个库,注重的是读和写,无法导出,没有渲染功能。

利用Python自动生成PPT的示例详解

废话不多说,第一步,安装python-pptx库:

pip3 install -i https://pypi.doubanio.com/simple/ python-pptx

ppt里面处理的主要对象一般为文本框,表格,图片。

每一页的ppt为一个slide

from pptx import Presentation, util
from pptx.util import Pt,Cm
from pptx.shapes.picture import Picture
#实例化一个ppt对象
ppt = Presentation("./test.pptx")
slide = ppt.slides[0] #第几页

然后遍历查看这一页ppt中都包含哪些对象:

def rander_template(slide):
    for shape in slide.shapes:
        if shape.has_text_frame == True:
            print("==========================文本框=============================")
            print("段落长度:",len(shape.text_frame.paragraphs))
            for paragraph in shape.text_frame.paragraphs:
                # 拼接文字
                print("段落包含字段:",len(paragraph.runs))
                print("".join(run.text for run in paragraph.runs))
                for i in range(len(paragraph.runs)):
                    print("run"+str(i)+":"+paragraph.runs[i].text)
            print(shape.text_frame.paragraphs[0].runs[0].text)
            shape.text_frame.paragraphs[0].runs[0].text = "规则是自由的第一要义"
        elif shape.has_table == True:
            print("==========================表格==============================")
            one_table_data = []
            for row in shape.table.rows:  # 读每行
                row_data = []
                for cell in row.cells:  # 读一行中的所有单元格
                    cell.text = cell.text if cell.text != "" else "未填写"
                    c = cell.text
                    row_data.append(c)
                one_table_data.append(row_data)  # 把每一行存入表
            # 用二维列表输出表格行和列的数据
            print(one_table_data)
            print("第一个单元格内容:",shape.table.rows[0].cells[0].text)

        elif isinstance(shape,Picture):
            print("==========================图片==============================")
            index = 0
            with open(f"{index}.jpg","wb") as f:
                f.write(shape.image.blob)
                index += 1

文本框对象【text_frame】:

shape.has_text_frame查看是否有文本框对象,有的话查看具体有几个段落【len(shape.text_frame.paragraphs)】,每个段落又有多少个run对象【len(paragraph.runs)】

注意:修改run对象的时候,修改run[0],后面的值都会被覆盖。

表格对象【table】:

table对象还是按照行列值来定位划分的,eg:table.rows[2]cells[3].text代表第三行第四列的值

图片对象【Picture】:

插入图片需要固定图片的位置,比如:

利用Python自动生成PPT的示例详解

def insert_pic(slide):
    #需要用到pptx库的util方法
    img_path = "./blue.png"  # 图片路径
    # 设置图片的位置和大小
    left = util.Cm(8.04)
    top = util.Cm(9.93)
    width = util.Cm(15.07)
    height = util.Cm(4.06)
    # 在页面中插入图片
    slide.shapes.add_picture(img_path, left, top, width, height)

全部代码:

from pptx import Presentation, util
from pptx.util import Pt,Cm
from pptx.shapes.picture import Picture
ppt = Presentation("./test.pptx")

def rander_template(slide):
    for shape in slide.shapes:
        if shape.has_text_frame == True:
            print("==========================文本框=============================")
            print("段落长度:",len(shape.text_frame.paragraphs))
            for paragraph in shape.text_frame.paragraphs:
                # 拼接文字
                print("段落包含字段:",len(paragraph.runs))
                print("".join(run.text for run in paragraph.runs))
                for i in range(len(paragraph.runs)):
                    print("run"+str(i)+":"+paragraph.runs[i].text)
            print(shape.text_frame.paragraphs[0].runs[0].text)
            shape.text_frame.paragraphs[0].runs[0].text = "规则是自由的第一要义"
        elif shape.has_table == True:
            print("==========================表格==============================")
            one_table_data = []
            for row in shape.table.rows:  # 读每行
                row_data = []
                for cell in row.cells:  # 读一行中的所有单元格
                    cell.text = cell.text if cell.text != "" else "未填写"
                    c = cell.text
                    row_data.append(c)
                one_table_data.append(row_data)  # 把每一行存入表
            # 用二维列表输出表格行和列的数据
            print(one_table_data)
            print("第一个单元格内容:",shape.table.rows[0].cells[0].text)

        elif isinstance(shape,Picture):
            print("==========================图片==============================")
            index = 0
            with open(f"{index}.jpg","wb") as f:
                f.write(shape.image.blob)
                index += 1
def insert_pic(slide):
    img_path = "./blue.png"  # 图片路径
    # 设置图片的位置和大小
    left = util.Cm(8.04)
    top = util.Cm(9.93)
    width = util.Cm(15.07)
    height = util.Cm(4.06)
    # 在页面中插入图片
    slide.shapes.add_picture(img_path, left, top, width, height)


if __name__ == "__main__":
    slide = ppt.slides[0] #第几页
    rander_template(slide)
    insert_pic(slide)
    ppt.save("new.pptx")  # 保存为文件

初始ppt:

利用Python自动生成PPT的示例详解

生成ppt:

利用Python自动生成PPT的示例详解

到此这篇关于利用Python自动生成PPT的示例详解的文章就介绍到这了,更多相关Python自动生成PPT内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文地址:https://blog.csdn.net/weixin_44784088/article/details/124277314

延伸 · 阅读

精彩推荐
  • PythonPython 实现静态链表案例详解

    Python 实现静态链表案例详解

    这篇文章主要介绍了Python 实现静态链表案例详解,本篇文章通过简要的案例,讲解了该项技术的了解与使用,以下就是详细内容,需要的朋友可以参考下...

    Yake19657582022-01-04
  • Pythonpython实现文件快照加密保护的方法

    python实现文件快照加密保护的方法

    这篇文章主要介绍了python实现文件快照加密保护的方法,涉及Python文件加密的技巧,可有效防止文件被篡改,需要的朋友可以参考下...

    秋风秋雨4282020-07-18
  • Pythonpython基础之while循环语句的使用

    python基础之while循环语句的使用

    这篇文章主要介绍了python基础之while循环语句的使用,文中有非常详细的代码示例,对正在学习python的小伙伴们有一定的帮助,需要的朋友可以参考下...

    佩瑞11132021-10-15
  • Pythonpython中使用xlrd读excel使用xlwt写excel的实例代码

    python中使用xlrd读excel使用xlwt写excel的实例代码

    这篇文章主要介绍了python中使用xlrd读excel使用xlwt写excel的实例代码,非常不错,具有参考借鉴价值,需要的朋友可以参考下...

    bugingcode12122021-01-11
  • PythonMatlab实现图像边缘检测

    Matlab实现图像边缘检测

    这篇文章主要为大家详细介绍了Matlab实现图像边缘检测,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    混z3382022-02-18
  • PythonPython插入Elasticsearch操作方法解析

    Python插入Elasticsearch操作方法解析

    这篇文章主要介绍了Python插入Elasticsearch操作方法解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以...

    cknds4882020-04-12
  • PythonPython爬虫实现网页信息抓取功能示例【URL与正则模块】

    Python爬虫实现网页信息抓取功能示例【URL与正则模块】

    这篇文章主要介绍了Python爬虫实现网页信息抓取功能,涉及Python使用URL与正则模块针对网页信息的读取与匹配相关操作技巧,需要的朋友可以参考下...

    九日王朝5592020-11-07
  • Pythonpython实现Dijkstra静态寻路算法

    python实现Dijkstra静态寻路算法

    这篇文章主要介绍了python实现Dijkstra静态寻路算法,常用于路由算法或者作为其他图算法的一个子模块,具有一定的参考价值,感兴趣的小伙伴们可以参考...

    By漫步11332021-05-18