利用python-pptx包批量修改ppt格式


最近實習需要對若干ppt進行格式上的調整,主要就是將標題的位置、對齊方式、字體等統一,人工修改又麻煩又容易錯。

因此結合網上的pptx包資料,使用python腳本完成處理。

主要的坑點在於,shape的text_frame不能直接修改字體,甚至paragraph也不行,由於一個框里多個字體存在,它會報為“None”,需要進一步去run層修改。

from pptx import Presentation
from pptx.enum.text import PP_ALIGN

prs = Presentation('originalppt.pptx') #導入ppt

sthead=prs.slides[2].shapes[1]  #以第3張ppt為標准標題格式
print(sthead.text)
stleft=sthead.left
stwidth=sthead.width
sttop=sthead.top
stheight=sthead.height

#進行逐個修改
i=1
for slide in prs.slides:
    for shape in slide.shapes:
        if shape.has_text_frame:
            if 36000<shape.left<1200000 and shape.height<1400000:
                    ftname=(shape.text_frame.paragraphs[0].runs[0].font.name)
                    ftsize=int(shape.text_frame.paragraphs[0].runs[0].font.size.pt)
                    if  (ftsize>27):
                        head=shape
                        head.left=stleft
                        head.top=sttop
                        head.width=10080000
                        head.height=stheight
                       
                        head.text_frame.vertical_anchor = MSO_ANCHOR.MIDDLE
                        head.text_frame.paragraphs[0].line_spacing=1

                        for paragraph in head.text_frame.paragraphs:
                            for run in paragraph.runs:
                                run.font.size = 32*12700 
                                run.font.name = '宋體'

                        #print(i,'ok')

    i +=1

prs.save('adjnew.pptx') #保存修改后的ppt

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM