@betteroffice/pptx

中文文档
0.5.1发布于 2026/10/27Apache-2.0TypeScriptES Module零依赖构建来源已验证近 30 天下载 36.5万

PowerPoint-faithful PPTX editor and viewer core: full OOXML fidelity, real-time human + AI agent collaboration. Framework-free (Rust/Wasm).

原始使用文档
文档目录12 节
  1. Install
  2. Usage
  3. Entry points
  4. Layout and rendering
  5. Saving and ownership
  6. Agent proposals
  7. Version-checked edit batches
  8. Structured export
  9. Comments and history
  10. Collaboration
  11. Operation profiles
  12. Documentation

@betteroffice/pptx

Framework-free PPTX parsing, editing, rendering, and saving through a Rust/WebAssembly engine. Use it to build a presentation viewer or editor with your own interface.

Install

bun add @betteroffice/pptx

No peer dependencies.

Usage

Render the first slide of a presentation onto a browser canvas:

import {
  initWasm,
  openPresentation,
  paintSlide,
  sizeCanvasForSlide,
} from '@betteroffice/pptx';

export async function renderFirstSlide(
  bytes: Uint8Array,
  fontBytes: Uint8Array,
  canvas: HTMLCanvasElement,
) {
  await initWasm();
  const deck = openPresentation(bytes, {
    fonts: [{ family: 'My Sans', bytes: fontBytes }],
  });
  try {
    const frame = deck.layoutSlide(0);
    const dpr = window.devicePixelRatio;
    sizeCanvasForSlide(canvas, frame, dpr);
    const context = canvas.getContext('2d');
    if (!context) throw new Error('Canvas 2D is unavailable');
    await paintSlide(context, frame, dpr);
  } finally {
    deck.dispose();
  }
}

fontBytes holds a TTF or OTF file, for example fetched from your server or loaded with loadBundledFontBytes from @betteroffice/fonts. Slides containing images also need an image resolver, described below. The React editor in @betteroffice/pptx-react manages rendering and user input.

Entry points

All entry points below are exported from @betteroffice/pptx.

entry pointdescription
initWasm(input?)Initialize WebAssembly before opening or inspecting a file. Browsers can omit the input; Node hosts can pass WASM bytes.
openPresentation(bytes, options?)Open a synchronous PresentationHandle for reads, edits, layout, and saving.
inspectPresentation(bytes, options?)Inspect parsed content without opening an editing handle; includeMedia: false omits media.
sizeCanvasForSlide(canvas, frame, dpr, scale?)Set canvas backing dimensions and CSS size for a slide.
paintSlide(context, frame, dpr?, scale?, options?)Paint a display list asynchronously onto a canvas.
slideToPng(frame, options?)Return a PNG Blob asynchronously using a browser canvas backend.
exportPptxStructured(bytes, options?)Export structured content asynchronously without fonts or a DOM.
exportPptxMarkdown(bytes, options?)Export Markdown and source anchors asynchronously.
renderPptxMarkdown(content, options?)Render structured content as Markdown asynchronously.

openPresentationSession(bytes, options?) from @betteroffice/pptx/worker opens an experimental asynchronous worker session.

openPresentation accepts fonts, fallbackFonts, clientId, and initialUpdate. Fallback fonts supply glyphs missing from a run's own face. When opening from a collaboration update, retain the source file bytes used to seed it so the handle can save.

The core's directly owned handle is synchronous. The React editor runs its engine in a Web Worker by default and exposes promise-returning reads and exports.

Layout and rendering

layoutSlide(index) uses a zero-based slide index. snapshot() returns the deck model, and layouts() lists layout IDs and names for insertSlide. duplicateSlide copies slide content and speaker notes, excluding comments. Shape coordinates use EMU (English Metric Units, 914,400 per inch); text offsets use UTF-16 code units.

For embedded images, pass paintSlide an options object with resolveImage, a function mapping an asset ID to a canvas image source or a promise for one. It may return null for an unavailable image. slideToPng accepts the same resolver and an optional scale.

Saving and ownership

save() returns PPTX bytes with edits applied; untouched slides retain their source part bytes. Call dispose() when finished with a handle. fork() creates an independently owned copy that must also be disposed.

Agent proposals

propose(agentId, note, edits) stages edits without changing the committed deck. Use listProposals, previewProposal, and layoutProposalSlide to inspect them, then acceptProposal or rejectProposal. Acceptance applies the group in one undo step; pending proposals stay in the open session.

layoutProposalDiffSlide returns a review frame and textChanges; pass both to paintSlide to show insertions and deletions. Review frames contain temporary text offsets and are for display only. If a target changed, acceptance throws StaleProposalError; inspect a fresh preview before choosing acceptProposal(id, { force: true }).

Version-checked edit batches

readContent, findText, validateEdits, and applyEdits support edits against a session version. See Edit PPTX in version-checked batches for request shapes, atomicity, refusals, and limits.

Structured export

exportStructured and exportMarkdown read committed state; headless exports accept file bytes. See PPTX structured export for options, anchors, diagnostics, and truncation.

Comments and history

comments() lists comments. setCommentFlavor selects legacy or modern only while that list is empty. replyToComment supports both flavors; setCommentStatus requires modern comments. addComment takes the author's name, text, and caller-supplied created timestamp. setCommentPosition moves a root comment using safe integer EMU coordinates; replies share their root's position.

setUndoCaptureMode('manual') groups tracked local edits until addUndoBoundary(); undoCaptureMode() reads the current mode. undo() and redo() navigate history. anchorCaret(storyId, index) creates an anchor that follows edits; resolveCaretAnchor returns its current offset, or null if its story no longer exists.

Collaboration

PresentationHandle implements the replica interface used by CollaborationProvider from @betteroffice/pptx/collaboration. See the collaboration guide for transports, shared seeds, presence, and connection lifecycle.

Operation profiles

layoutSlideProfiled, insertTextProfiled, and undoProfiled expose engine stage timings. See Operation profiles for result shapes and measurement guidance.

Documentation

版本与发布记录

@betteroffice/pptx 共发布 9 个版本,首次发布于 2026/07/20;过去 12 个月发布了 9 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.5.1latest2026/10/2712 天前
0.5.02026/10/2613 天前
0.2.02026/09/291 个月前
0.1.12026/09/171 个月前
0.1.02026/09/161 个月前
0.0.42026/08/202 个月前

关于 @betteroffice/pptx 的常见问题

@betteroffice/pptx 是什么?

PowerPoint-faithful PPTX editor and viewer core: full OOXML fidelity, real-time human + AI agent collaboration. Framework-free (Rust/Wasm).

如何安装 @betteroffice/pptx?

在项目目录运行 npm install @betteroffice/pptx,或使用 pnpm add / yarn add / bun add。

@betteroffice/pptx 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@betteroffice/pptx 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@betteroffice/pptx 可以商用吗?

@betteroffice/pptx 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@betteroffice/pptx 的下载量有多少?

根据 npm 官方下载统计,@betteroffice/pptx 在 2026/10/08 – 2026/11/06 这 30 天内被下载 365,226 次,日均约 12,174 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@betteroffice/pptx 还在维护吗?

最新版本 0.5.1 发布于 2026/10/27(12 天前);至今共发布 9 个版本,其中过去 12 个月发布了 9 个;首次发布于 2026/07/20。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。