1. 程式人生 > 其它 >[Python]批量替換PPT字型指令碼

[Python]批量替換PPT字型指令碼

目錄

使用說明

  1. 將指令碼放置在需要批量修改的PPT資料夾根目錄
  2. 修改配置檔案 conf.ini 中的字型
  3. 執行指令碼檔案

​ exe檔案 下載:PPT換字型指令碼.zip - 藍奏雲 (lanzouw.com)

指令碼程式碼

from pptx import Presentation
import pptx_ea_font
import os
import configparser

def setFont(file):
   # 定義函式修改字型
   prs = Presentation(file)
   for slide in prs.slides:
      for shape in slide.shapes:
         if shape.has_text_frame:
            text_frame = shape.text_frame
            for paragraph in text_frame.paragraphs:
               for run in paragraph.runs:
                  pptx_ea_font.set_font(run, FONT)
   prs.save(file)


def traverse_directory_tree ():
   # 遍歷目錄樹,更改字尾為.ppt和.pptx的檔案
   path = os.getcwd()
   for root,dirs,files in os.walk(path):
      for file in files:
         if file.endswith(".pptx") or file.endswith(".ppt"):
            print(os.path.join(root,file) + " 的字型已被更改為:" + FONT)
            file = os.path.join(root,file)
            setFont(file)


if __name__ == "__main__":
   # 初始化物件
   conf = configparser.ConfigParser()
   conf.read("config.ini")
   print("目錄下所有的PPT字型將設定為:" + conf.get("CONF", "FONT"))
   FONT = conf.get("CONF","FONT")
   # 執行命令
   traverse_directory_tree()

配置檔案

[CONF]
FONT = 思源黑體 CN Regular