溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

如何利用Python自動(dòng)生成PPT

發(fā)布時(shí)間:2022-07-15 09:57:00 來(lái)源:億速云 閱讀:223 作者:iii 欄目:開發(fā)技術(shù)

今天小編給大家分享一下如何利用Python自動(dòng)生成PPT的相關(guān)知識(shí)點(diǎn),內(nèi)容詳細(xì),邏輯清晰,相信大部分人都還太了解這方面的知識(shí),所以分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后有所收獲,下面我們一起來(lái)了解一下吧。

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

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

廢話不多說(shuō),第一步,安裝python-pptx庫(kù):

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

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

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

from pptx import Presentation, util
from pptx.util import Pt,Cm
from pptx.shapes.picture import Picture
#實(shí)例化一個(gè)ppt對(duì)象
ppt = Presentation("./test.pptx")
slide = ppt.slides[0] #第幾頁(yè)

然后遍歷查看這一頁(yè)ppt中都包含哪些對(duì)象:

def rander_template(slide):
    for shape in slide.shapes:
        if shape.has_text_frame == True:
            print("==========================文本框=============================")
            print("段落長(zhǎng)度:",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對(duì)象【len(paragraph.runs)】

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

表格對(duì)象【table】:

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

圖片對(duì)象【Picture】:

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

如何利用Python自動(dòng)生成PPT

def insert_pic(slide):
    #需要用到pptx庫(kù)的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)
    # 在頁(yè)面中插入圖片
    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("段落長(zhǎng)度:",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)
    # 在頁(yè)面中插入圖片
    slide.shapes.add_picture(img_path, left, top, width, height)


if __name__ == "__main__":
    slide = ppt.slides[0] #第幾頁(yè)
    rander_template(slide)
    insert_pic(slide)
    ppt.save('new.pptx')  # 保存為文件

初始ppt:

如何利用Python自動(dòng)生成PPT

生成ppt:

如何利用Python自動(dòng)生成PPT

以上就是“如何利用Python自動(dòng)生成PPT”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家閱讀完這篇文章都有很大的收獲,小編每天都會(huì)為大家更新不同的知識(shí),如果還想學(xué)習(xí)更多的知識(shí),請(qǐng)關(guān)注億速云行業(yè)資訊頻道。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI