PPT全域性美化

👤 雷老虎 📦 v1.0.0 ⭐ 4.5 ⬇️ 9.9K 下載
📄 辦公效率 免費

📖 技能介紹


name: ppt-win32com-editor description: 使用 win32com(PowerPoint COM 介面)就地修改已有 .pptx 檔案,保留所有原始佈局、圖片、形狀、漸變背景和特效。當用戶要求"修改/美化/調整/最佳化 PPT 檔案"、"更改字型/顏色/段落間距"、"在原檔案基礎上編輯 PowerPoint"時使用。注意:新建 PPT 請使用 pptx-generator skill;本 skill 專用於已有 PPT 的就地修改,不重建檔案。


PPT Win32COM Editor

定位

本 skill 使用 win32com(PowerPoint COM 介面) 直接開啟並修改已有 PPT 檔案,完整保留所有原始元素:佈局、圖片、形狀、漸變背景、陰影、箭頭連線、FREEFORM 自由形狀、SmartArt 等。

絕不從零重建 PPT(新建 PPT 請使用 pptx-generator skill)。

何時使用本 Skill

使用者意圖 使用 Skill
修改/美化已有 PPT 檔案 ✅ 本 skill
調整字型/顏色/段落間距 ✅ 本 skill
在原檔案基礎上編輯內容 ✅ 本 skill
批次替換文字/字型/顏色 ✅ 本 skill
新建 PPT 簡報 ❌ 用 pptx-generator
僅提取 PPT 文字內容 ❌ 用 markitdown

核心原則

  1. 永遠在原檔案上就地修改,絕不重建
  2. 保留所有形狀/圖片/連線線/特效(python-pptx 會丟棄這些)
  3. 先備份:修改前複製原檔案為 *_backup.pptx
  4. 需要本地安裝 Microsoft PowerPoint(win32com 依賴)

環境要求

  • Windows 作業系統
  • 已安裝 Microsoft PowerPoint(2016 或更高版本)
  • Python 包:pywin32(通常已隨環境安裝)
  • 操作時 PowerPoint 不可在前臺被使用者同時使用(COM 獨佔)

基本工作流

Step 1:備份原檔案

import shutil
src = r"C:\path\to\file.pptx"
dst = src.replace(".pptx", "_backup.pptx")
shutil.copy2(src, dst)

Step 2:用 win32com 開啟 PPT

import win32com.client

ppt = win32com.client.Dispatch("PowerPoint.Application")
ppt.Visible = True   # 必須設為 True(win32com 限制)
pres = ppt.Presentations.Open(
    r"C:\path\to\file.pptx",
    WithWindow=False,
    ReadOnly=False,
    Untitled=False
)

Step 3:執行修改(見下方常見操作)

7w4.net小蔥技能站收錄全網優質技能,值得收藏。

Step 4:儲存並關閉

pres.Save()
pres.Close()
ppt.Quit()

常見操作速查

詳見 references/win32com-ppt-guide.md,以下是最常用操作:

全文替換字型

def replace_font(pptx_path, new_font="Microsoft YaHei", new_size=None):
    import win32com.client
    ppt = win32com.client.Dispatch("PowerPoint.Application")
    ppt.Visible = True
    pres = ppt.Presentations.Open(pptx_path, WithWindow=False, ReadOnly=False, Untitled=False)
    for i in range(1, pres.Slides.Count + 1):
        slide = pres.Slides(i)
        for shape in slide.Shapes:
            if shape.HasTextFrame == -1:
                tr = shape.TextFrame.TextRange
                tr.Font.Name = new_font
                if new_size:
                    tr.Font.Size = new_size
    pres.Save()
    pres.Close()
    ppt.Quit()

全文替換文字顏色

def replace_text_color(pptx_path, hex_color):
    import win32com.client
    # hex_color: "#7CBD3E" 格式,自動轉換為 BGR
    r, g, b = int(hex_color[1:3], 16), int(hex_color[3:5], 16), int(hex_color[5:7], 16)
    bgr = (b << 16) | (g << 8) | r
    ppt = win32com.client.Dispatch("PowerPoint.Application")
    ppt.Visible = True
    pres = ppt.Presentations.Open(pptx_path, WithWindow=False, ReadOnly=False, Untitled=False)
    for i in range(1, pres.Slides.Count + 1):
        slide = pres.Slides(i)
        for shape in slide.Shapes:
            if shape.HasTextFrame == -1:
                shape.TextFrame.TextRange.Font.Color.RGB = bgr
    pres.Save()
    pres.Close()
    ppt.Quit()

調整全文段落間距

def adjust_paragraph_spacing(pptx_path, space_before=4, space_after=6, line_spacing=1.5):
    import win32com.client
    ppt = win32com.client.Dispatch("PowerPoint.Application")
    ppt.Visible = True
    pres = ppt.Presentations.Open(pptx_path, WithWindow=False, ReadOnly=False, Untitled=False)
    for i in range(1, pres.Slides.Count + 1):
        slide = pres.Slides(i)
        for shape in slide.Shapes:
            if shape.HasTextFrame == -1:
                tr = shape.TextFrame.TextRange
                for p in range(1, tr.Paragraphs().Count + 1):
                    para = tr.Paragraphs(p)
                    para.ParagraphFormat.SpaceBefore = space_before
                    para.ParagraphFormat.SpaceAfter = space_after
                    para.ParagraphFormat.LineSpacing = line_spacing
    pres.Save()
    pres.Close()
    ppt.Quit()

遍歷所有幻燈片的所有文字框

for i in range(1, pres.Slides.Count + 1):
    slide = pres.Slides(i)
    for shape in slide.Shapes:
        if shape.HasTextFrame == -1:
            tf = shape.TextFrame
            tr = tf.TextRange
            print(f"Slide {i}: {tr.Text}")

⚠️ 關鍵陷阱

1. RGB 顏色是 BGR 順序!

PowerPoint COM 的 .RGB 屬性是 BGR(藍-綠-紅),不是 RGB:

顏色 期望 RGB 寫入值(十進位制)
紅色 #FF0000 0xFF0000 0x0000FF
綠色 #7CBD3E 0x7CBD3E 0x3EBD7C
藍色 #0000FF 0x0000FF 0xFF0000

必須轉換bgr = (b << 16) | (g << 8) | r

本 skill 中所有涉及顏色的操作已內建轉換函式。

2. COM 索引從 1 開始

Slides(1) 是第一張幻燈片,不是 Slides[0]

3. Visible 必須設為 True

ppt.Visible = True 是強制要求,win32com 的限制。

4. 不要同時開啟同一個檔案

PowerPoint 開啟檔案後會鎖定檔案,確保之前的操作已呼叫 Close()

與 python-pptx / PptxGenJS 的對比

能力 win32com(本 skill) python-pptx PptxGenJS
保留漸變背景 ❌ 丟棄 ❌ 不支援
保留陰影效果 ❌ 丟棄 ❌ 不支援
保留 FREEFORM 形狀 ❌ 丟失 ❌ 不支援
保留箭頭連線 ❌ 丟失 ❌ 不支援
保留圖片裁剪 ⚠️ 部分 ❌ 不支援
批次改字型/顏色
需要安裝 PowerPoint ✅ 必須 ❌ 不需要 ❌ 不需要
跨平臺 ❌ Windows only ✅ 任意 ✅ 任意
新建 PPT ⚠️ 可以但不推薦 ✅ 推薦

結論:修改已有 PPT → 用本 skill(win32com);新建 PPT → 用 pptx-generator skill(PptxGenJS)。

參考資料

完整 API 操作指南:references/win32com-ppt-guide.md

🤖 AI 評測

這個 Skill 質量不錯,專注於修改已有的 PPT 檔案,能夠保留原檔案的所有複雜設計元素。文件清晰易懂,操作步驟詳細,常見的坑(如顏色顯示不對)都有提醒。對 Windows 使用者、需要修改重要 PPT 的人來說很有用。不過它只能在 Windows 上用,而且必須安裝 PowerPoint,這對部分使用者可能是限制。沒有提供實際例子或常見問題解答,新手可能需要多花點時間摸索。總體推薦,Windows 使用者用它來美化 PPT 很靠譜。

📊 多維度評分

適應性4.7
規範性4.6
有效性4.5
可靠性4.2
可信度4.9

📁 包含檔案 (2 個)

📄 SKILL.md 6.8 KB
📄 references/win32com-ppt-guide.md 11.2 KB