export_deck_pptx.mjs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. #!/usr/bin/env node
  2. /**
  3. * export_deck_pptx.mjs — 把多文件 slide deck 导出为 PPTX
  4. *
  5. * 两种模式:
  6. * --mode image 图片铺底,视觉 100% 保真,⚠️ 文字不可编辑
  7. * --mode editable 文本框原生,文字可编辑,要求 HTML 符合 4 条硬约束(见 references/editable-pptx.md)
  8. *
  9. * 用法:
  10. * # 图片模式(默认)
  11. * node export_deck_pptx.mjs --slides <dir> --out <file.pptx>
  12. * # 可编辑模式
  13. * node export_deck_pptx.mjs --slides <dir> --out <file.pptx> --mode editable
  14. *
  15. * --mode image 特点:
  16. * - 每张 slide 截图成 PNG,满铺一张 PPTX 页面
  17. * - 视觉 100% 保真(因为就是图片)
  18. * - 文字不可编辑
  19. * - HTML 随便写,不挑格式
  20. *
  21. * --mode editable 特点:
  22. * - 调用 scripts/html2pptx.js 把 HTML DOM 逐元素翻译成 PowerPoint 对象
  23. * - 文字是真文本框,PPT 里直接双击能编辑
  24. * - ⚠️ HTML 必须符合 4 条硬约束(见 references/editable-pptx.md):
  25. * 1. 文字包在 <p>/<h1>-<h6> 里(div 不能直接放文字)
  26. * 2. 不用 CSS 渐变
  27. * 3. <p>/<h*> 不能有 background/border/shadow(放外层 div)
  28. * 4. div 不能 background-image(用 <img>)
  29. * - body 尺寸默认 960pt × 540pt(LAYOUT_WIDE,13.333″ × 7.5″)
  30. * - 视觉驱动的 HTML 几乎无法 pass —— 必须从写 HTML 的第一行就按约束写
  31. *
  32. * 依赖:
  33. * --mode image: npm install playwright pptxgenjs
  34. * --mode editable: npm install playwright pptxgenjs sharp
  35. *
  36. * 按文件名排序(01-xxx.html → 02-xxx.html → ...)。
  37. */
  38. import { chromium } from 'playwright';
  39. import pptxgen from 'pptxgenjs';
  40. import fs from 'fs/promises';
  41. import path from 'path';
  42. import os from 'os';
  43. import { fileURLToPath } from 'url';
  44. const __dirname = path.dirname(fileURLToPath(import.meta.url));
  45. function parseArgs() {
  46. const args = { width: 1920, height: 1080, mode: 'image' };
  47. const a = process.argv.slice(2);
  48. for (let i = 0; i < a.length; i += 2) {
  49. const k = a[i].replace(/^--/, '');
  50. args[k] = a[i + 1];
  51. }
  52. if (!args.slides || !args.out) {
  53. console.error('用法: node export_deck_pptx.mjs --slides <dir> --out <file.pptx> [--mode image|editable] [--width 1920] [--height 1080]');
  54. process.exit(1);
  55. }
  56. args.width = parseInt(args.width);
  57. args.height = parseInt(args.height);
  58. if (!['image', 'editable'].includes(args.mode)) {
  59. console.error(`未知 --mode: ${args.mode}。支持: image, editable`);
  60. process.exit(1);
  61. }
  62. return args;
  63. }
  64. async function exportImage({ slidesDir, outFile, files, width, height }) {
  65. console.log(`[image mode] Rendering ${files.length} slides as PNG...`);
  66. const browser = await chromium.launch();
  67. const ctx = await browser.newContext({ viewport: { width, height } });
  68. const page = await ctx.newPage();
  69. const tmpDir = await fs.mkdtemp(path.join(os.tmpdir(), 'deck-pptx-'));
  70. const pngs = [];
  71. for (const f of files) {
  72. const url = 'file://' + path.join(slidesDir, f);
  73. await page.goto(url, { waitUntil: 'networkidle' }).catch(() => page.goto(url));
  74. await page.waitForTimeout(1200);
  75. const out = path.join(tmpDir, f.replace(/\.html$/, '.png'));
  76. await page.screenshot({ path: out, fullPage: false });
  77. pngs.push(out);
  78. console.log(` [${pngs.length}/${files.length}] ${f}`);
  79. }
  80. await browser.close();
  81. const pres = new pptxgen();
  82. pres.defineLayout({ name: 'DECK', width: width / 96, height: height / 96 });
  83. pres.layout = 'DECK';
  84. for (const png of pngs) {
  85. const s = pres.addSlide();
  86. s.addImage({ path: png, x: 0, y: 0, w: pres.width, h: pres.height });
  87. }
  88. await pres.writeFile({ fileName: outFile });
  89. for (const p of pngs) await fs.unlink(p).catch(() => {});
  90. await fs.rmdir(tmpDir).catch(() => {});
  91. console.log(`\n✓ Wrote ${outFile} (${files.length} slides, image mode, 文字不可编辑)`);
  92. }
  93. async function exportEditable({ slidesDir, outFile, files }) {
  94. console.log(`[editable mode] Converting ${files.length} slides via html2pptx...`);
  95. // 动态 require html2pptx.js(CommonJS 模块)
  96. const { createRequire } = await import('module');
  97. const require = createRequire(import.meta.url);
  98. let html2pptx;
  99. try {
  100. html2pptx = require(path.join(__dirname, 'html2pptx.js'));
  101. } catch (e) {
  102. console.error(`✗ 加载 html2pptx.js 失败:${e.message}`);
  103. console.error(` 该模块依赖 sharp —— 请跑 npm install sharp 后重试。`);
  104. process.exit(1);
  105. }
  106. const pres = new pptxgen();
  107. pres.layout = 'LAYOUT_WIDE'; // 13.333 × 7.5 inch,对应 HTML body 960 × 540 pt
  108. const errors = [];
  109. for (let i = 0; i < files.length; i++) {
  110. const f = files[i];
  111. const fullPath = path.join(slidesDir, f);
  112. try {
  113. await html2pptx(fullPath, pres);
  114. console.log(` [${i + 1}/${files.length}] ${f} ✓`);
  115. } catch (e) {
  116. console.error(` [${i + 1}/${files.length}] ${f} ✗ ${e.message}`);
  117. errors.push({ file: f, error: e.message });
  118. }
  119. }
  120. if (errors.length) {
  121. console.error(`\n⚠️ ${errors.length} 张 slide 转换失败。常见原因:HTML 不符合 4 条硬约束。`);
  122. console.error(` 详见 references/editable-pptx.md 的「常见错误速查」。`);
  123. if (errors.length === files.length) {
  124. console.error(`✗ 全部失败,不生成 PPTX。`);
  125. process.exit(1);
  126. }
  127. }
  128. await pres.writeFile({ fileName: outFile });
  129. console.log(`\n✓ Wrote ${outFile} (${files.length - errors.length}/${files.length} slides, editable mode, 文字可在 PPT 中直接编辑)`);
  130. }
  131. async function main() {
  132. const { slides, out, width, height, mode } = parseArgs();
  133. const slidesDir = path.resolve(slides);
  134. const outFile = path.resolve(out);
  135. const files = (await fs.readdir(slidesDir))
  136. .filter(f => f.endsWith('.html'))
  137. .sort();
  138. if (!files.length) {
  139. console.error(`No .html files found in ${slidesDir}`);
  140. process.exit(1);
  141. }
  142. if (mode === 'image') {
  143. await exportImage({ slidesDir, outFile, files, width, height });
  144. } else {
  145. await exportEditable({ slidesDir, outFile, files });
  146. }
  147. }
  148. main().catch(e => { console.error(e); process.exit(1); });