欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

利用Python自動(dòng)生成PPT的示例詳解

 更新時(shí)間:2022年07月14日 16:45:50   作者:阿濤的一天  
在日常工作中,PPT制作是常見的工作。這篇文章主要為大家詳細(xì)介紹了如何利用Python自動(dòng)生成PPT,文中的示例代碼講解詳細(xì),感興趣的可以了解一下

在日常工作中,PPT制作是常見的工作,如果制作創(chuàng)意類PPT,則無法通過自動(dòng)化的形式生成,因?yàn)閯?chuàng)意本身具有隨機(jī)性,而自動(dòng)化解決的是重復(fù)性工作,兩者有所沖突。

python-pptx是python處理PPT的一個(gè)庫,注重的是讀和寫,無法導(dǎo)出,沒有渲染功能。

廢話不多說,第一步,安裝python-pptx庫:

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

ppt里面處理的主要對象一般為文本框,表格,圖片。

每一頁的ppt為一個(gè)slide

from pptx import Presentation, util
from pptx.util import Pt,Cm
from pptx.shapes.picture import Picture
#實(shí)例化一個(gè)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 = "規(guī)則是自由的第一要義"
        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)  # 把每一行存入表
            # 用二維列表輸出表格行和列的數(shù)據(jù)
            print(one_table_data)
            print("第一個(gè)單元格內(nèi)容:",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

文本框?qū)ο蟆総ext_frame】:

shape.has_text_frame查看是否有文本框?qū)ο螅械脑挷榭淳唧w有幾個(gè)段落【len(shape.text_frame.paragraphs)】,每個(gè)段落又有多少個(gè)run對象【len(paragraph.runs)】

注意:修改run對象的時(shí)候,修改run[0],后面的值都會被覆蓋。

表格對象【table】:

table對象還是按照行列值來定位劃分的,eg:table.rows[2]cells[3].text代表第三行第四列的值

圖片對象【Picture】:

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

def insert_pic(slide):
    #需要用到pptx庫的util方法
    img_path = './blue.png'  # 圖片路徑
    # 設(shè)置圖片的位置和大小
    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 = "規(guī)則是自由的第一要義"
        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)  # 把每一行存入表
            # 用二維列表輸出表格行和列的數(shù)據(jù)
            print(one_table_data)
            print("第一個(gè)單元格內(nèi)容:",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'  # 圖片路徑
    # 設(shè)置圖片的位置和大小
    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:

生成ppt:

到此這篇關(guān)于利用Python自動(dòng)生成PPT的示例詳解的文章就介紹到這了,更多相關(guān)Python自動(dòng)生成PPT內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • python自動(dòng)化測試三部曲之unittest框架的實(shí)現(xiàn)

    python自動(dòng)化測試三部曲之unittest框架的實(shí)現(xiàn)

    這篇文章主要介紹了python自動(dòng)化測試三部曲之unittest框架的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-10-10
  • Python單鏈表原理與實(shí)現(xiàn)方法詳解

    Python單鏈表原理與實(shí)現(xiàn)方法詳解

    這篇文章主要介紹了Python單鏈表原理與實(shí)現(xiàn)方法,結(jié)合實(shí)例形式詳細(xì)分析了Python單鏈表的具體概念、原理、實(shí)現(xiàn)方法與操作注意事項(xiàng),需要的朋友可以參考下
    2020-02-02
  • 深入解析Python編程中JSON模塊的使用

    深入解析Python編程中JSON模塊的使用

    這篇文章主要介紹了深入解析Python編程中JSON模塊的使用,舉例講解了如何使用Python解析JSON數(shù)據(jù),需要的朋友可以參考下
    2015-10-10
  • windows系統(tǒng)中python使用rar命令壓縮多個(gè)文件夾示例

    windows系統(tǒng)中python使用rar命令壓縮多個(gè)文件夾示例

    這篇文章主要介紹了windows系統(tǒng)中python使用rar命令壓縮多個(gè)文件夾示例,需要的朋友可以參考下
    2014-05-05
  • Django中l(wèi)og日志記錄的最佳實(shí)踐

    Django中l(wèi)og日志記錄的最佳實(shí)踐

    Django中的log日志記錄是一個(gè)非常重要的功能,可以幫助開發(fā)者快速定位和解決問題。本文將介紹Django中l(wèi)og日志記錄的基本概念和使用方法,并提供一些最佳實(shí)踐,幫助開發(fā)者更好地利用log日志記錄功能。
    2023-04-04
  • python用裝飾器自動(dòng)注冊Tornado路由詳解

    python用裝飾器自動(dòng)注冊Tornado路由詳解

    這篇文章主要給大家介紹了python用裝飾器自動(dòng)注冊Tornado路由,文中給出了三個(gè)版本的解決方法,有需要的朋友可以參考借鑒,下面來一起看看吧。
    2017-02-02
  • python字典的常用操作方法小結(jié)

    python字典的常用操作方法小結(jié)

    下面小編就為大家?guī)硪黄猵ython字典的常用操作方法小結(jié)。小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考,一起跟隨小編過來看看吧
    2016-05-05
  • Python爬取網(wǎng)頁信息的示例

    Python爬取網(wǎng)頁信息的示例

    這篇文章主要介紹了Python爬取網(wǎng)頁信息的示例,幫助大家更好的理解和學(xué)習(xí)python 爬蟲,感興趣的朋友可以了解下
    2020-09-09
  • Python3解決棋盤覆蓋問題的方法示例

    Python3解決棋盤覆蓋問題的方法示例

    這篇文章主要介紹了Python3解決棋盤覆蓋問題的方法,簡單描述了棋盤覆蓋問題的概念、原理及Python相關(guān)操作技巧,需要的朋友可以參考下
    2017-12-12
  • pycharm中使用request和Pytest進(jìn)行接口測試的方法

    pycharm中使用request和Pytest進(jìn)行接口測試的方法

    這篇文章主要介紹了pycharm中使用request和Pytest進(jìn)行接口測試的方法,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-07-07

最新評論