HTML 转 PPTX 完整工作流 — 自动嵌入外部 CSS,使用 pptxgenjs 解析 HTML 结构,生成可编辑 PPTX,保留文字、布局、样式
text
HTML (带外部 CSS)
↓ [步骤 1: CSS 内嵌]
HTML (内嵌 CSS)
↓ [步骤 2: pptxgenjs 解析]
PPTX (可编辑)
↓ [步骤 3: 导出]
最终文件
bash
cd /Users/panda/.openclaw/workspace/skills/html2pptx-complete
# Python 依赖(CSS 内嵌)
pip3 install -r requirements-python.txt
# Node.js 依赖(PPTX 生成)
npm install
bash
# 一键转换
node scripts/convert.js input.html output.pptx
# 或分步执行
# 步骤 1: CSS 内嵌
python3 scripts/embed-css.py input.html embedded.html
# 步骤 2: PPTX 生成
node scripts/generate-pptx.js embedded.html output.pptx
<link rel="stylesheet"> 标签<style>CSS 内容</style>类型支持度说明本地相对路径✅href="style.css"本地绝对路径✅href="/path/to/style.css"远程 URL⚠️ 跳过href="https://..."CSS 变量✅完整解析渐变/动画✅保留原始代码
转换前:
html
<head>
<link rel="stylesheet" href="style.css">
<link rel="stylesheet" href="theme.css">
</head>
转换后:
html
<head>
<style>
/* style.css 内容 */
/* theme.css 内容 */
</style>
</head>
HTML 结构PPTX 映射说明section.slide新幻灯片每张 slide 一页<h1>标题文本框幻灯片标题<h2>- <h6>小标题层级递减字号<p>正文文本框保留段落<ul>, <ol>列表保留项目符号<img>图片支持路径/URL/Base64<div>容器/文本框根据内容判断CSS 样式PPTX 属性颜色/字体/大小/对齐
CSS 属性PPTX 映射支持度color文字颜色✅background-color填充色✅font-size字号✅ px→ptfont-weight: bold粗体✅font-style: italic斜体✅text-align对齐方式✅border边框⚠️ 简化border-radius圆角⚠️ 部分box-shadow阴影❌linear-gradient渐变填充⚠️ 简化
优先级:
section.slide 结构(最高优先级)<h1> 标题(备选方案)属性值格式PPTX (Office Open XML)比例16:9 宽屏可编辑性✅ 文字/形状可编辑文件大小50-200KB(取决于内容)
bash
# 输入:html-ppt 生成的多页 HTML
node scripts/convert.js my-deck/index.html my-deck.pptx
# 输出:包含所有 slide 的 PPTX
bash
# 输入:引用多个 CSS 文件的 HTML
node scripts/convert.js presentation.html presentation.pptx
# 自动:
# 1. 嵌入 style.css, theme.css
# 2. 解析 slide 结构
# 3. 生成可编辑 PPTX
bash
# 输入:普通 HTML 文档
node scripts/convert.js document.html document.pptx
# 输出:按 h1 标题分页的 PPTX
python
#!/usr/bin/env python3
"""CSS 内嵌脚本"""
import re
from pathlib import Path
from bs4 import BeautifulSoup
def embed_css(html_path, output_path=None):
# 读取 HTML
# 查找 <link> 标签
# 读取 CSS 文件
# 替换为 <style>
# 保存
pass
javascript
#!/usr/bin/env node
/**
* PPTX 生成脚本
* 使用 pptxgenjs 解析 HTML 并生成 PPTX
*/
const PptxGenJS = require('pptxgenjs');
const cheerio = require('cheerio');
async function generate(htmlPath, outputPath) {
// 读取 HTML
// 解析 slide 结构
// 应用 CSS 样式
// 创建 PPTX
// 导出文件
}
javascript
#!/usr/bin/env node
/**
* 完整工作流脚本
* 步骤 1: CSS 内嵌 (Python)
* 步骤 2: PPTX 生成 (Node.js)
* 步骤 3: 导出文件
*/
async function convert(htmlPath, outputPath) {
// 调用 embed-css.py
// 调用 generate-pptx.js
// 清理临时文件
}
text
html2pptx-complete/
├── SKILL.md (本文档)
├── README.md (快速入门)
├── package.json (Node.js 依赖)
├── requirements-python.txt (Python 依赖)
├── scripts/
│ ├── convert.js (一键转换)
│ ├── embed-css.py (CSS 内嵌)
│ └── generate-pptx.js (PPTX 生成)
├── refs/
│ └── pptxgenjs-mapping.md (样式映射表)
└── examples/
├── demo.html (示例 HTML)
├── demo.css (示例 CSS)
└── demo_converted.pptx (输出示例)
特性html2pptx-completehtml2pptx-shapeCSS 内嵌✅ 自动✅ 自动核心库pptxgenjs (JS)python-pptx环境Node.js + PythonPython样式保留⚠️ 基础 + 部分 CSS✅ 完整 CSS布局还原⚠️ 简化✅ 精确分页规则section.slide / h1section.slide适用场景通用 HTMLhtml-ppt 生成
bash
# Python 依赖
pip3 install beautifulsoup4 cssutils requests
# Node.js 依赖
npm install pptxgenjs cheerio
检查:
解决:
原因:
解决:
style="..."检查:
MIT
老 6 🎯