← 返回列表
全部 Skill›内容与媒体 Content & Media›pptx

pptx

作者 @HKUDS · 语言 en
Read, create, or edit PowerPoint .pptx decks — build slides from an outline, extract slide text/speaker notes, edit shapes/tables/charts, replace images, or export to PDF/images. Use whenever a .pptx (or .ppt) file is an input or output, or the user mentions a deck, slides, or a presentation.
内容与媒体 Content & Media演示文稿通用执行命令读写文件待翻译
最近源活动
—
检测 SKILL.md 语言
en
星标 ★
62,323
分支 Fork
0
安装选项
提示
指令
使用 AI 助手安装
将下面的提示复制到你正在使用的 AI 助手中。
下载完整 Skill 目录(含全部配套文件)。 下载压缩包 ↓
SKILL.md 原文预览

pptx

Work with PowerPoint .pptx files using python-pptx (preinstalled). A .pptx

is a ZIP of XML parts; python-pptx handles the structure so you rarely touch XML.

Drop to raw OOXML only for the few things the library can't express (see Advanced).

Use exec with complete Python source (language: python). Keep asset creation,

deck generation, reopening, and structural verification in one call when

possible; later calls can revise the same relative filename. Follow the turn's

User workspace instructions for locating inputs, output boundaries, and

presenting the finished deck.

Mental model

  • A presentation has slides; each slide is built from a layout; layouts

live on slide masters. Layouts define placeholders (title, body,

picture, etc.) by idx and type.

  • A slide holds shapes: placeholders, text boxes, pictures, tables, charts.
  • Shapes with text expose .text_frame → .paragraphs → .runs. A run is the

unit that carries formatting (font, size, bold, color).

  • Units are EMU. Use the helpers: from pptx.util import Inches, Pt, Emu.

Read / extract

from pptx import Presentation

prs = Presentation("deck.pptx")
print(len(prs.slides), prs.slide_width, prs.slide_height)  # EMU dims

for i, slide in enumerate(prs.slides, 1):
    print(f"--- slide {i} (layout: {slide.slide_layout.name}) ---")
    for shape in slide.shapes:
        if shape.has_text_frame:
            print(shape.text_frame.text)  # \n-joined paragraphs
        elif shape.has_table:
            for row in shape.table.rows:
                print([c.text for c in row.cells])
    if slide.has_notes_slide:
        notes = slide.notes_slide.notes_text_frame.text
        if notes:
            print("NOTES:", notes)

Iterate slide.placeholders to see placeholder idx / placeholder_format.type.

For a fast text-only dump, just collect shape.text_frame.text across slides.

Create from an outline

List the layouts first — indices vary by template. With the default template,

layout 0 = Title, 1 = Title+Content, 5 = Title Only, 6 = Blank.

from pptx import Presentation
from pptx.util import Inches, Pt

prs = Presentation()  # or Presentation("template.pptx") to inherit a theme
for idx, lay in enumerate(prs.slide_layouts):
    print(idx, lay.name, [(p.placeholder_format.idx, p.name) for p in lay.placeholders])

# Title slide
s = prs.slides.add_slide(prs.slide_layouts[0])
s.shapes.title.text = "My Deck"
s.placeholders[1].text = "Subtitle"  # idx from the listing above

# Title + bullets
s = prs.slides.add_slide(prs.slide_layouts[1])
s.shapes.title.text = "Agenda"
tf = s.placeholders[1].text_frame
tf.text = "First point"  # first paragraph
for line, lvl in [("Second", 0), ("Sub-point", 1)]:
    p = tf.add_paragraph()
    p.text = line
    p.level = lvl

prs.save("out.pptx")

Always set text via placeholders/shapes — never hand-write bullet glyphs (•);

indentation/bullets come from the layout via paragraph.level.

Add a free text box or picture on any slide:

tb = s.shapes.add_textbox(Inches(1), Inches(1), Inches(8), Inches(1))
r = tb.text_frame.paragraphs[0].add_run()
r.text = "Hi"
r.font.size = Pt(28)
r.font.bold = True
s.shapes.add_picture("logo.png", Inches(0.5), Inches(0.5), height=Inches(1))  # omit w to keep ratio

Edit existing

Edit at the run level to preserve a run's formatting; rewriting

text_frame.text collapses to one run and drops inline formatting.

for slide in prs.slides:
    for shape in slide.shapes:
        if not shape.has_text_frame:
            continue
        for para in shape.text_frame.paragraphs:
            for run in para.runs:
                if "{{NAME}}" in run.text:
                    run.text = run.text.replace("{{NAME}}", "Frank")

To delete a shape/placeholder: sp = shape._element; sp.getparent().remove(sp).

If the template has more slots than your data, remove the extra shapes entirely

rather than leaving empty placeholders.

Replace an image in place (keep size/position)

python-pptx has no direct setter; swap the bytes of the related image part. Read

the picture's r:embed rId off its <a:blip>, then overwrite the part's blob.

The replacement bytes must use the same image encoding as the original part

(for example PNG for PNG). A different encoding also requires a new correctly

typed media part and relationship; changing _blob alone would leave invalid

content-type metadata.

from pptx.oxml.ns import qn

for shape in slide.shapes:
    if shape.shape_type == 13:  # MSO_SHAPE_TYPE.PICTURE
        blip = shape._element.find(".//" + qn("a:blip"))
        rid = blip.get(qn("r:embed"))
        with open("new.png", "rb") as f:
            shape.part.related_part(rid)._blob = f.read()

Tables and charts

from pptx.util import Inches

tbl = s.shapes.add_table(
    rows=2, cols=2, left=Inches(1), top=Inches(1), width=Inches(6), height=Inches(2)
).table
tbl.cell(0, 0).text = "Header"

from pptx.chart.data import CategoryChartData
from pptx.enum.chart import XL_CHART_TYPE

cd = CategoryChartData()
cd.categories = ["Q1", "Q2", "Q3"]
cd.add_series("Sales", (4.5, 5.5, 6.2))
s.shapes.add_chart(XL_CHART_TYPE.COLUMN_CLUSTERED, Inches(1), Inches(1), Inches(8), Inches(4.5), cd)

Design (only when the user wants a polished deck, not a data dump)

  • Pick a topic-specific palette and one accent; don't default to generic blue.

One color should dominate. Dark title/closing slides, light content slides.

  • Vary layouts across slides (two-column, stat callout, quote, section divider) —

rep

评论
HHKUDS
HKUDS
HKUDS/DeepTutor
查看 GitHub 仓库 查看作者的其他 Skill
来源信息
作者@HKUDS
所属大类内容与媒体 Content & Media / 演示文稿
国内可用性通用
最近源活动—
风险提示执行命令、读写文件
提示:风险标签为程序静态扫描结果,仅作提示,不代表安全认证。安装前请阅读 SKILL.md 及其引用的脚本、权限,自行判断是否可信。