This guide walks you through installing OpenXmlSdkTs and performing your first document operations.
npm install openxmlsdkts
The library has only two runtime dependencies (jszip and ltxmlts), which are installed automatically.
Here is a complete example that opens a Word document, inspects it, adds a paragraph, and saves it:
import { WmlPackage, W, XElement } from "openxmlsdkts";
import fs from "fs";
// Read the file into a Blob
const buffer = fs.readFileSync("report.docx");
const doc = await WmlPackage.open(new Blob([buffer]));
// Navigate to the main document part
const mainPart = await doc.mainDocumentPart();
const xDoc = await mainPart!.getXDocument();
// Query the XML
const body = xDoc.root!.element(W.body);
const paragraphs = body!.elements(W.p);
console.log(`Document has ${paragraphs.length} paragraphs`);
// Add a new paragraph
const newPara = new XElement(W.p,
new XElement(W.r,
new XElement(W.t, "Hello from OpenXmlSdkTs!")
)
);
body!.add(newPara);
// Save changes back to the part and export
mainPart!.putXDocument(xDoc);
const blob = await doc.saveToBlobAsync();
Every interaction with OpenXmlSdkTs follows the same five-step pattern:
WmlPackage.open(), SmlPackage.open(), or PmlPackage.open() with a Blob, Flat OPC string, or Base64 string. The library auto-detects the format.mainDocumentPart(), workbookPart()).getXDocument() to get the XML tree, then use LINQ to XML methods to read or change it. Call putXDocument() to write changes back to the part.saveToBlobAsync(), saveToBase64Async(), or saveToFlatOpcAsync().