SKILL.md
唯讀
名稱
pptx-manipulation
描述
>
版本
1.0
PPTX 操作技能
概述
本技能可透過 python-pptx 程式庫,以程式化方式建立、編輯及操作 Microsoft PowerPoint (.pptx) 簡報。無需手動編輯,即可建立包含文字、圖案、圖片、圖表及表格的專業投影片。
使用方式
- 描述您想建立或修改的簡報
- 提供要包含的內容、資料或圖片
- 我將產生 python-pptx 程式碼並執行
範例提示:
- "根據這個大綱建立一份 10 頁的募資簡報"
- "將這筆資料以圖表形式加入第 3 張投影片"
- "從這份簡報中擷取所有文字"
- "根據這份 Markdown 內容產生投影片"
領域知識
python-pptx 基礎
from pptx import Presentation
from pptx.util import Inches, Pt
from pptx.enum.shapes import MSO_SHAPE
from pptx.enum.text import PP_ALIGN
# 建立新簡報
prs = Presentation()
# 或開啟現有簡報
prs = Presentation('existing.pptx')
簡報結構
Presentation
├── slide_layouts (預先定義的版面配置)
├── slides (個別投影片)
│ ├── shapes (文字、圖片、圖表)
│ │ ├── text_frame (段落)
│ │ └── table (列、儲存格)
│ └── placeholders (標題、內容)
└── slide_masters (範本)
投影片版面配置
# 常見版面配置索引(依範本可能不同)
TITLE_SLIDE = 0
TITLE_CONTENT = 1
SECTION_HEADER = 2
TWO_CONTENT = 3
COMPARISON = 4
TITLE_ONLY = 5
BLANK = 6
# 以版面配置新增投影片
slide_layout = prs.slide_layouts[TITLE_CONTENT]
slide = prs.slides.add_slide(slide_layout)
新增內容
標題投影片
slide_layout = prs.slide_layouts[0] # 標題投影片
slide = prs.slides.add_slide(slide_layout)
title = slide.shapes.title
subtitle = slide.placeholders[1]
title.text = "季度報告"
subtitle.text = "2024 年第四季績效回顧"
文字內容
# 使用預留位置
body = slide.placeholders[1]
tf = body.text_frame
tf.text = "第一個項目符號"
# 新增更多段落
p = tf.add_paragraph()
p.text = "第二個項目符號"
p.level = 0
p = tf.add_paragraph()
p.text = "子項目"
p.level = 1
文字方塊
from pptx.util import Inches, Pt
left = Inches(1)
top = Inches(2)
width = Inches(4)
height = Inches(1)
txBox = slide.shapes.add_textbox(left, top, width, height)
tf = txBox.text_frame
p = tf.paragraphs[0]
p.text = "自訂文字方塊"
p.font.bold = True
p.font.size = Pt(18)
圖案
from pptx.enum.shapes import MSO_SHAPE
# 矩形
shape = slide.shapes.add_shape(
MSO_SHAPE.RECTANGLE,
Inches(1), Inches(2), # left, top
Inches(3), Inches(1.5) # width, height
)
shape.text = "含文字的矩形"
# 常用圖案:
# MSO_SHAPE.RECTANGLE, ROUNDED_RECTANGLE
# MSO_SHAPE.OVAL, CHEVRON, ARROW_RIGHT
# MSO_SHAPE.CALLOUT_ROUNDED_RECTANGLE
圖片
# 新增圖片
slide.shapes.add_picture(
'image.png',
Inches(1), Inches(2), # 位置
width=Inches(4) # 自動調整高度
)
# 或指定長寬
slide.shapes.add_picture(
'logo.png',
Inches(8), Inches(0.5),
Inches(1.5), Inches(0.75)
)
表格
# 建立表格
rows, cols = 4, 3
left = Inches(1)
top = Inches(2)
width = Inches(8)
height = Inches(2)
table = slide.shapes.add_table(rows, cols, left, top, width, height).table
# 設定欄寬
table.columns[0].width = Inches(2)
table.columns[1].width = Inches(3)
table.columns[2].width = Inches(3)
# 新增標題列
headers = ['產品', '第三季銷售額', '第四季銷售額']
for i, header in enumerate(headers):
cell = table.cell(0, i)
cell.text = header
cell.text_frame.paragraphs[0].font.bold = True
# 新增資料
data = [
['Widget A', '$10,000', '$12,500'],
['Widget B', '$8,000', '$9,200'],
['Widget C', '$15,000', '$18,000'],
]
for row_idx, row_data in enumerate(data, 1):
for col_idx, value in enumerate(row_data):
table.cell(row_idx, col_idx).text = value
圖表
from pptx.chart.data import CategoryChartData
from pptx.enum.chart import XL_CHART_TYPE
# 圖表資料
chart_data = CategoryChartData()
chart_data.categories = ['Q1', 'Q2', 'Q3', 'Q4']
chart_data.add_series('銷售額', (19.2, 21.4, 16.7, 23.8))
chart_data.add_series('支出', (12.1, 15.3, 14.2, 18.1))
# 新增圖表
x, y, cx, cy = Inches(1), Inches(2), Inches(8), Inches(4)
chart = slide.shapes.add_chart(
XL_CHART_TYPE.COLUMN_CLUSTERED,
x, y, cx, cy, chart_data
).chart
# 自訂設定
chart.has_legend = True
chart.legend.include_in_layout = False
格式設定
文字格式
from pptx.dml.color import RGBColor
run = p.runs[0]
run.font.name = 'Arial'
run.font.size = Pt(24)
run.font.bold = True
run.font.italic = True
run.font.color.rgb = RGBColor(0x00, 0x66, 0xCC)
圖案填滿與線條
from pptx.dml.color import RGBColor
shape.fill.solid()
shape.fill.fore_color.rgb = RGBColor(0x00, 0x80, 0x00)
shape.line.color.rgb = RGBColor(0x00, 0x00, 0x00)
shape.line.width = Pt(2)
段落對齊
from pptx.enum.text import PP_ALIGN
p.alignment = PP_ALIGN.CENTER # LEFT, RIGHT, JUSTIFY
最佳實務
- 使用範本:從 .pptx 範本開始,確保品牌一致性
- 先規劃版面:在編寫程式碼前先規劃投影片結構
- 重複使用投影片母片:維持簡報間的一致性
- 最佳化圖片:在加入前壓縮圖片
- 測試輸出:務必驗證產生的簡報
常見模式
投影片組產生器
def create_deck(title, slides_content):
prs = Presentation()
# 標題投影片
slide = prs.slides.add_slide(prs.slide_layouts[0])
slide.shapes.title.text = title
# 內容投影片
for slide_data in slides_content:
slide = prs.slides.add_slide(prs.slide_layouts[1])
slide.shapes.title.text = slide_data['title']
body = slide.placeholders[1]
tf = body.text_frame
for i, point in enumerate(slide_data['points']):
if i == 0:
tf.text = point
else:
p = tf.add_paragraph()
p.text = point
return prs
資料驅動圖表
def add_bar_chart(slide, title, categories, values):
from pptx.chart.data import CategoryChartData
from pptx.enum.chart import XL_CHART_TYPE
chart_data = CategoryChartData()
chart_data.categories = categories
chart_data.add_series('數值', values)
chart = slide.shapes.add_chart(
XL_CHART_TYPE.BAR_CLUSTERED,
Inches(1), Inches(2),
Inches(8), Inches(4),
chart_data
).chart
chart.chart_title.text_frame.text = title
return chart
範例
範例 1:建立募資簡報
from pptx import Presentation
from pptx.util import Inches, Pt
prs = Presentation()
# 投影片 1:標題
slide = prs.slides.add_slide(prs.slide_layouts[0])
slide.shapes.title.text = "StartupX"
slide.placeholders[1].text = "革新文件處理流程"
# 投影片 2:問題
slide = prs.slides.add_slide(prs.slide_layouts[1])
slide.shapes.title.text = "問題"
body = slide.placeholders[1].text_frame
body.text = "手動文件處理每年耗費企業 1 兆美元"
p = body.add_paragraph()
p.text = "員工平均花費 20% 時間在文件任務上"
p.level = 1
# 投影片 3:解決方案
slide = prs.slides.add_slide(prs.slide_layouts[1])
slide.shapes.title.text = "我們的解決方案"
body = slide.placeholders[1].text_frame
body.text = "AI 驅動的文件自動化"
body.add_paragraph().text = "處理速度提升 90%"
body.add_paragraph().text = "準確率高達 99.5%"
body.add_paragraph().text = "與現有工具相容"
# 投影片 4:市場
slide = prs.slides.add_slide(prs.slide_layouts[5]) # 僅標題
slide.shapes.title.text = "市場機會:2028 年達 500 億美元"
# 新增圖表
from pptx.chart.data import CategoryChartData
from pptx.enum.chart import XL_CHART_TYPE
data = CategoryChartData()
data.categories = ['2024', '2025', '2026', '2027', '2028']
data.add_series('市場規模(十億美元)', [30, 35, 40, 45, 50])
slide.shapes.add_chart(
XL_CHART_TYPE.LINE,
Inches(1), Inches(1.5),
Inches(8), Inches(5),
data
)
prs.save('pitch_deck.pptx')
範例 2:含資料表格的報告
from pptx import Presentation
from pptx.util import Inches, Pt
prs = Presentation()
# 標題投影片
slide = prs.slides.add_slide(prs.slide_layouts[0])
slide.shapes.title.text = "銷售績效報告"
slide.placeholders[1].text = "2024 年第四季"
# 資料投影片
slide = prs.slides.add_slide(prs.slide_layouts[5])
slide.shapes.title.text = "區域績效"
# 建立表格
table = slide.shapes.add_table(5, 4, Inches(0.5), Inches(1.5), Inches(9), Inches(4)).table
# 標題列
headers = ['區域', '營收', '成長率', '目標達成']
for i, h in enumerate(headers):
table.cell(0, i).text = h
table.cell(0, i).text_frame.paragraphs[0].font.bold = True
# 資料
data = [
['北美', '$5.2M', '+15%', '達成'],
['歐洲', '$3.8M', '+12%', '達成'],
['亞太', '$2.9M', '+28%', '超標'],
['拉丁美洲', '$1.1M', '+8%', '未達'],
]
for row_idx, row_data in enumerate(data, 1):
for col_idx, value in enumerate(row_data):
table.cell(row_idx, col_idx).text = value
prs.save('sales_report.pptx')
限制
- 無法呈現複雜動畫
- SmartArt 支援有限
- 無法透過 API 嵌入影片
- 母片編輯較為複雜
- 圖表類型僅限標準 Office 圖表
安裝
pip install python-pptx






