每个 PPTX 文件包含一个主题文件:
ppt/theme/theme1.xml
其中定义了 12 种主题颜色:
| 名称 | 含义 |
|---|---|
| bg1 | Background 1 |
| tx1 | Text 1 |
| bg2 | Background 2 |
| tx2 | Text 2 |
| accent1 | 强调色 1 |
| accent2 | 强调色 2 |
| accent3 | 强调色 3 |
| accent4 | 强调色 4 |
| accent5 | 强调色 5 |
| accent6 | 强调色 6 |
| hlink | 超链接颜色 |
| folHlink | 访问过的超链接 |
accent6 就是 PPT 主题的第六个强调色。
不同模板 RGB 会不同。
🔍 如何获取 accent6 的 RGB 颜色?
Python-pptx 不支持直接取 theme 色的 RGB,需要解析 XML:
代码:读取主题颜色(精准版)
1from pptx import Presentation 2from lxml import etree 3 4def get_theme_colors(ppt_path): 5 prs = Presentation(ppt_path) 6 theme_part = prs.part.theme.part 7 xml = etree.fromstring(theme_part.blob) 8 9 ns = {"a": "http://schemas.openxmlformats.org/drawingml/2006/main"} 10 11 colors = {} 12 scheme_clrs = xml.xpath("//a:themeElements/a:clrScheme/*", namespaces=ns) 13 14 for clr in scheme_clrs: 15 name = clr.tag.split("}")[-1] # accent1, accent2... 16 rgb = None 17 18 srgbClr = clr.find("a:srgbClr", ns) 19 if srgbClr is not None: 20 rgb = srgbClr.get("val") 21 22 colors[name] = rgb 23 24 return colors 25 26print(get_theme_colors("你的PPT路径.pptx")) 27
返回示例:
1{ 2 'accent1': '4472C4', 3 'accent6': '70AD47', 4 'bg1': 'FFFFFF', 5 ... 6} 7
《PPT 主题颜色解释(Theme Color Scheme)》 是转载文章,点击查看原文。
