name: ppt-win32com-editor description: 使用 win32com(PowerPoint COM 介面)就地修改已有 .pptx 檔案,保留所有原始佈局、圖片、形狀、漸變背景和特效。當用戶要求"修改/美化/調整/最佳化 PPT 檔案"、"更改字型/顏色/段落間距"、"在原檔案基礎上編輯 PowerPoint"時使用。注意:新建 PPT 請使用 pptx-generator skill;本 skill 專用於已有 PPT 的就地修改,不重建檔案。
本 skill 使用 win32com(PowerPoint COM 介面) 直接開啟並修改已有 PPT 檔案,完整保留所有原始元素:佈局、圖片、形狀、漸變背景、陰影、箭頭連線、FREEFORM 自由形狀、SmartArt 等。
絕不從零重建 PPT(新建 PPT 請使用 pptx-generator skill)。
| 使用者意圖 | 使用 Skill |
|---|---|
| 修改/美化已有 PPT 檔案 | ✅ 本 skill |
| 調整字型/顏色/段落間距 | ✅ 本 skill |
| 在原檔案基礎上編輯內容 | ✅ 本 skill |
| 批次替換文字/字型/顏色 | ✅ 本 skill |
| 新建 PPT 簡報 | ❌ 用 pptx-generator |
| 僅提取 PPT 文字內容 | ❌ 用 markitdown |
*_backup.pptxpywin32(通常已隨環境安裝)import shutil
src = r"C:\path\to\file.pptx"
dst = src.replace(".pptx", "_backup.pptx")
shutil.copy2(src, dst)
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
)
7w4.net小蔥技能站收錄全網優質技能,值得收藏。
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}")
PowerPoint COM 的 .RGB 屬性是 BGR(藍-綠-紅),不是 RGB:
| 顏色 | 期望 RGB | 寫入值(十進位制) |
|---|---|---|
紅色 #FF0000 |
0xFF0000 |
0x0000FF |
綠色 #7CBD3E |
0x7CBD3E |
0x3EBD7C |
藍色 #0000FF |
0x0000FF |
0xFF0000 |
必須轉換:bgr = (b << 16) | (g << 8) | r
本 skill 中所有涉及顏色的操作已內建轉換函式。
Slides(1) 是第一張幻燈片,不是 Slides[0]。
ppt.Visible = True 是強制要求,win32com 的限制。
PowerPoint 開啟檔案後會鎖定檔案,確保之前的操作已呼叫 Close()。
| 能力 | win32com(本 skill) | python-pptx | PptxGenJS |
|---|---|---|---|
| 保留漸變背景 | ✅ | ❌ 丟棄 | ❌ 不支援 |
| 保留陰影效果 | ✅ | ❌ 丟棄 | ❌ 不支援 |
| 保留 FREEFORM 形狀 | ✅ | ❌ 丟失 | ❌ 不支援 |
| 保留箭頭連線 | ✅ | ❌ 丟失 | ❌ 不支援 |
| 保留圖片裁剪 | ✅ | ⚠️ 部分 | ❌ 不支援 |
| 批次改字型/顏色 | ✅ | ✅ | ✅ |
| 需要安裝 PowerPoint | ✅ 必須 | ❌ 不需要 | ❌ 不需要 |
| 跨平臺 | ❌ Windows only | ✅ 任意 | ✅ 任意 |
| 新建 PPT | ⚠️ 可以但不推薦 | ✅ | ✅ 推薦 |
結論:修改已有 PPT → 用本 skill(win32com);新建 PPT → 用 pptx-generator skill(PptxGenJS)。
完整 API 操作指南:references/win32com-ppt-guide.md
這個 Skill 質量不錯,專注於修改已有的 PPT 檔案,能夠保留原檔案的所有複雜設計元素。文件清晰易懂,操作步驟詳細,常見的坑(如顏色顯示不對)都有提醒。對 Windows 使用者、需要修改重要 PPT 的人來說很有用。不過它只能在 Windows 上用,而且必須安裝 PowerPoint,這對部分使用者可能是限制。沒有提供實際例子或常見問題解答,新手可能需要多花點時間摸索。總體推薦,Windows 使用者用它來美化 PPT 很靠譜。