OpenXmlSdkTs supports three I/O formats for opening and saving documents. The open() method auto-detects which format you provide, so you can use whichever is most convenient for your scenario.
The standard approach for file system I/O. Use Blob (or Buffer in Node.js) to read and write .docx, .xlsx, and .pptx files directly.
import { WmlPackage } from "openxmlsdkts";
import fs from "fs";
// Open from Blob
const buffer = fs.readFileSync("document.docx");
const doc = await WmlPackage.open(new Blob([buffer]));
// Save to Blob
const blob = await doc.saveToBlobAsync();
The type alias OpcBinary represents binary document data.
Flat OPC is an XML representation of the entire Office document package in a single XML string. This format is especially useful for:
import { WmlPackage } from "openxmlsdkts";
// Open from Flat OPC string
const flatOpc: string = getFlatOpcFromSomewhere();
const doc = await WmlPackage.open(flatOpc);
// Save to Flat OPC
const flatOpcOut = await doc.saveToFlatOpcAsync();
The type alias FlatOpcString represents Flat OPC XML data.
A Base64-encoded representation of the binary package. Useful for:
import { WmlPackage } from "openxmlsdkts";
// Open from Base64 string
const base64: string = getBase64FromApi();
const doc = await WmlPackage.open(base64);
// Save to Base64
const base64Out = await doc.saveToBase64Async();
The type alias Base64String represents Base64-encoded document data.
The open() method on each package class automatically detects the input format:
// All three work with the same open() method
const doc1 = await WmlPackage.open(blob); // Blob detected
const doc2 = await WmlPackage.open(flatOpc); // Flat OPC XML detected
const doc3 = await WmlPackage.open(base64Str); // Base64 detected
Use the package class that matches your document type:
| Class | Document Type | Extension |
|---|---|---|
WmlPackage |
Word documents | .docx |
SmlPackage |
Excel workbooks | .xlsx |
PmlPackage |
PowerPoint presentations | .pptx |
Each package provides three save methods corresponding to the three I/O formats:
| Method | Returns | Description |
|---|---|---|
saveToBlobAsync() |
Promise<Blob> |
Binary blob for file I/O |
saveToBase64Async() |
Promise<string> |
Base64-encoded string |
saveToFlatOpcAsync() |
Promise<string> |
Flat OPC XML string |
A typical workflow opens a document in one format, modifies it, and saves it in any format:
import { WmlPackage, W, XElement } from "openxmlsdkts";
// Open from Base64 (e.g., received from an API)
const doc = await WmlPackage.open(base64Input);
// Modify the document
const mainPart = await doc.mainDocumentPart();
const xDoc = await mainPart!.getXDocument();
const body = xDoc.root!.element(W.body);
body!.add(
new XElement(W.p,
new XElement(W.r,
new XElement(W.t, "Added by OpenXmlSdkTs")
)
)
);
mainPart!.putXDocument(xDoc);
// Save as Flat OPC (e.g., to send to an Office Add-in)
const flatOpc = await doc.saveToFlatOpcAsync();
You can open in any format and save in any other format -- the library handles all conversions internally.