export_deck_pptx.mjs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. #!/usr/bin/env node
  2. /**
  3. * export_deck_pptx.mjs — 把多文件 slide deck 导出为可编辑 PPTX
  4. *
  5. * 用法:
  6. * node export_deck_pptx.mjs --slides <dir> --out <file.pptx>
  7. *
  8. * 行为:
  9. * - 调用 scripts/html2pptx.js 把 HTML DOM 逐元素翻译成 PowerPoint 原生对象
  10. * - 文字是真文本框,PPT 里直接双击能编辑
  11. * - body 尺寸 960pt × 540pt(LAYOUT_WIDE,13.333″ × 7.5″)
  12. *
  13. * ⚠️ HTML 必须符合 4 条硬约束(见 references/editable-pptx.md):
  14. * 1. 文字包在 <p>/<h1>-<h6> 里(div 不能直接放文字)
  15. * 2. 不用 CSS 渐变
  16. * 3. <p>/<h*> 不能有 background/border/shadow(放外层 div)
  17. * 4. div 不能 background-image(用 <img>)
  18. *
  19. * 视觉驱动的 HTML 几乎无法 pass —— 必须从写 HTML 的第一行就按约束写。
  20. * 视觉自由度优先的场景(动画、web component、CSS 渐变、复杂 SVG)
  21. * 应改用 export_deck_pdf.mjs / export_deck_stage_pdf.mjs 导出 PDF。
  22. *
  23. * 依赖:npm install playwright pptxgenjs sharp
  24. *
  25. * 按文件名排序(01-xxx.html → 02-xxx.html → ...)。
  26. */
  27. import pptxgen from 'pptxgenjs';
  28. import fs from 'fs/promises';
  29. import path from 'path';
  30. import { fileURLToPath } from 'url';
  31. const __dirname = path.dirname(fileURLToPath(import.meta.url));
  32. function parseArgs() {
  33. const args = {};
  34. const a = process.argv.slice(2);
  35. for (let i = 0; i < a.length; i += 2) {
  36. const k = a[i].replace(/^--/, '');
  37. args[k] = a[i + 1];
  38. }
  39. if (!args.slides || !args.out) {
  40. console.error('用法: node export_deck_pptx.mjs --slides <dir> --out <file.pptx>');
  41. console.error('');
  42. console.error('⚠️ HTML 必须符合 4 条硬约束(见 references/editable-pptx.md)。');
  43. console.error(' 视觉自由度优先的场景请改用 export_deck_pdf.mjs 导出 PDF。');
  44. process.exit(1);
  45. }
  46. return args;
  47. }
  48. async function main() {
  49. const { slides, out } = parseArgs();
  50. const slidesDir = path.resolve(slides);
  51. const outFile = path.resolve(out);
  52. const files = (await fs.readdir(slidesDir))
  53. .filter(f => f.endsWith('.html'))
  54. .sort();
  55. if (!files.length) {
  56. console.error(`No .html files found in ${slidesDir}`);
  57. process.exit(1);
  58. }
  59. console.log(`Converting ${files.length} slides via html2pptx...`);
  60. const { createRequire } = await import('module');
  61. const require = createRequire(import.meta.url);
  62. let html2pptx;
  63. try {
  64. html2pptx = require(path.join(__dirname, 'html2pptx.js'));
  65. } catch (e) {
  66. console.error(`✗ 加载 html2pptx.js 失败:${e.message}`);
  67. console.error(` 依赖缺失时请跑:npm install playwright pptxgenjs sharp`);
  68. process.exit(1);
  69. }
  70. const pres = new pptxgen();
  71. pres.layout = 'LAYOUT_WIDE'; // 13.333 × 7.5 inch,对应 HTML body 960 × 540 pt
  72. const errors = [];
  73. for (let i = 0; i < files.length; i++) {
  74. const f = files[i];
  75. const fullPath = path.join(slidesDir, f);
  76. try {
  77. await html2pptx(fullPath, pres);
  78. console.log(` [${i + 1}/${files.length}] ${f} ✓`);
  79. } catch (e) {
  80. console.error(` [${i + 1}/${files.length}] ${f} ✗ ${e.message}`);
  81. errors.push({ file: f, error: e.message });
  82. }
  83. }
  84. if (errors.length) {
  85. console.error(`\n⚠️ ${errors.length} 张 slide 转换失败。常见原因:HTML 不符合 4 条硬约束。`);
  86. console.error(` 详见 references/editable-pptx.md 的「常见错误速查」。`);
  87. if (errors.length === files.length) {
  88. console.error(`✗ 全部失败,不生成 PPTX。`);
  89. process.exit(1);
  90. }
  91. }
  92. await pres.writeFile({ fileName: outFile });
  93. console.log(`\n✓ Wrote ${outFile} (${files.length - errors.length}/${files.length} slides, 可编辑 PPTX)`);
  94. }
  95. main().catch(e => { console.error(e); process.exit(1); });