• 利用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
  • 相关阅读:
    etherlime-1-Quick Start
    etherlime-4-Etherlime CLI
    Sequelize-nodejs-2-basic usage
    Sequelize-nodejs-6-Instances
    Sequelize-nodejs-5-Querying
    Sequelize-nodejs-4-Model usage
    Sequelize-nodejs-3-model definition
    eclipse快捷键大全
    java第一课
    java程序员认证考试题库
  • 原文地址:https://www.cnblogs.com/oikoumene/p/11201864.html
Copyright © 2020-2023  润新知