export_deck_pptx.mjs 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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> [--width 1920] [--height 1080]
  7. *
  8. * 特点:
  9. * - 每张 slide 截图成 PNG,满铺一张 PPTX 页面
  10. * - 视觉 100% 保真(因为就是图片)
  11. * - ⚠️ 文字不可编辑(文字变成了图片)
  12. *
  13. * 如果用户需要「可编辑文字」的 PPTX:
  14. * ❌ 不要在 claude-design 的 HTML 上硬上 html2pptx——claude-design 的 HTML 视觉自由度高,
  15. * 很少能满足 html2pptx 的严格约束(p 标签语法、无 ::before、无 span margin 等)。
  16. * 实测 32 页里能 pass 的不到 30%,剩下的要逐页改造 + 逐页修字体溢出——工时失控。
  17. * ✅ 正确做法:切换到 **huashu-slides** skill 的 Path A,按它的 HTML 格式**从头重构**
  18. * 每一页。huashu-slides 的 HTML 从一开始就符合 html2pptx 约束,可 100% 导出可编辑 PPTX。
  19. *
  20. * 依赖:playwright pptxgenjs
  21. * npm install playwright pptxgenjs
  22. *
  23. * 会按文件名排序(01-xxx.html → 02-xxx.html → ...)
  24. */
  25. import { chromium } from 'playwright';
  26. import pptxgen from 'pptxgenjs';
  27. import fs from 'fs/promises';
  28. import path from 'path';
  29. import os from 'os';
  30. function parseArgs() {
  31. const args = { width: 1920, height: 1080 };
  32. const a = process.argv.slice(2);
  33. for (let i = 0; i < a.length; i += 2) {
  34. const k = a[i].replace(/^--/, '');
  35. args[k] = a[i + 1];
  36. }
  37. if (!args.slides || !args.out) {
  38. console.error('用法: node export_deck_pptx.mjs --slides <dir> --out <file.pptx> [--width 1920] [--height 1080]');
  39. process.exit(1);
  40. }
  41. args.width = parseInt(args.width);
  42. args.height = parseInt(args.height);
  43. return args;
  44. }
  45. async function main() {
  46. const { slides, out, width, height } = parseArgs();
  47. const slidesDir = path.resolve(slides);
  48. const outFile = path.resolve(out);
  49. const files = (await fs.readdir(slidesDir))
  50. .filter(f => f.endsWith('.html'))
  51. .sort();
  52. if (!files.length) {
  53. console.error(`No .html files found in ${slidesDir}`);
  54. process.exit(1);
  55. }
  56. console.log(`Found ${files.length} slides, rendering to PNG...`);
  57. const browser = await chromium.launch();
  58. const ctx = await browser.newContext({ viewport: { width, height } });
  59. const page = await ctx.newPage();
  60. const tmpDir = await fs.mkdtemp(path.join(os.tmpdir(), 'deck-pptx-'));
  61. const pngs = [];
  62. for (const f of files) {
  63. const url = 'file://' + path.join(slidesDir, f);
  64. await page.goto(url, { waitUntil: 'networkidle' }).catch(() => page.goto(url));
  65. await page.waitForTimeout(1200);
  66. const out = path.join(tmpDir, f.replace(/\.html$/, '.png'));
  67. await page.screenshot({ path: out, fullPage: false });
  68. pngs.push(out);
  69. console.log(` [${pngs.length}/${files.length}] ${f}`);
  70. }
  71. await browser.close();
  72. // Build PPTX
  73. const pres = new pptxgen();
  74. pres.defineLayout({ name: 'DECK', width: width / 96, height: height / 96 });
  75. pres.layout = 'DECK';
  76. for (const png of pngs) {
  77. const s = pres.addSlide();
  78. s.addImage({ path: png, x: 0, y: 0, w: pres.width, h: pres.height });
  79. }
  80. await pres.writeFile({ fileName: outFile });
  81. // cleanup
  82. for (const p of pngs) await fs.unlink(p).catch(() => {});
  83. await fs.rmdir(tmpDir).catch(() => {});
  84. console.log(`\n✓ Wrote ${outFile} (${files.length} slides, image mode)`);
  85. console.log(` 要可编辑?改走 huashu-slides 的 Path A 从头重构 HTML。`);
  86. }
  87. main().catch(e => { console.error(e); process.exit(1); });