C# Libraries for Document Manipulation in OfficeCLI: Open XML SDK Deep Dive

OfficeCLI relies exclusively on the Open XML SDK (DocumentFormat.OpenXml) to read, write, and transform Office documents without requiring Microsoft Office installation.

The iOfficeAI/OfficeCLI repository demonstrates how modern .NET command-line tools can manipulate Word, Excel, and PowerPoint files using Microsoft's official Open XML SDK. This article examines the specific C# libraries for document manipulation in OfficeCLI, revealing how the codebase leverages the DocumentFormat.OpenXml namespaces to handle .docx, .pptx, and .xlsx files directly through their underlying XML structures.

Core Library: Open XML SDK (DocumentFormat.OpenXml)

OfficeCLI uses the Open XML SDK as its sole third-party dependency for document manipulation. The library provides the DocumentFormat.OpenXml namespaces that enable direct access to the Open Packaging Conventions (OPC) format used by modern Microsoft Office files.

According to the source code in officecli.csproj, the package is declared as:

<PackageReference Include="DocumentFormat.OpenXml" Version="2.20.0" />

This single dependency powers all document operations across Word, PowerPoint, and Excel handlers, eliminating the need for interop assemblies or commercial office suites.

Implementation Architecture

Word Document Processing

The Word handling logic resides in Handlers/WordHandler.cs and its partial class extensions. These files import the SDK's core namespaces to manipulate document parts:

using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;

The WordprocessingDocument class serves as the entry point for all Word operations. The handlers use this class to open document packages, access the MainDocumentPart, and traverse the XML element tree using strongly-typed classes like Paragraph, Run, and Text.

PowerPoint and Excel Support

While Word commands dominate the CLI's feature set, PowerPoint manipulation follows identical patterns using PresentationDocument from the DocumentFormat.OpenXml.Presentation namespace. Excel support similarly relies on SpreadsheetDocument and related namespaces, all provided by the same Open XML SDK package.

Practical Code Examples

Opening a Word Document and Extracting the Title

The WordHandler.View.cs file demonstrates how to open a read-only document stream and extract metadata:

using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;

string filePath = "example.docx";
using var wordDoc = WordprocessingDocument.Open(filePath, false);
var title = wordDoc.MainDocumentPart?.Document?.Body?
               .Descendants<Title>()
               .FirstOrDefault()?.InnerText;
Console.WriteLine($"Title: {title}");

This pattern appears in the view implementation at lines 231-235 of WordHandler.cs.

Adding Content to Documents

For document modification, the SDK requires opening the package with write access (true parameter). The WordHandler.Set.cs file implements content insertion using the following approach:

using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;

string filePath = "example.docx";
using var wordDoc = WordprocessingDocument.Open(filePath, true);
var body = wordDoc.MainDocumentPart?.Document?.Body;

if (body != null)
{
    var para = new Paragraph(new Run(new Text("New paragraph added via OfficeCLI")));
    body.AppendChild(para);
    wordDoc.MainDocumentPart.Document.Save();
}

The code creates strongly-typed XML elements and appends them to the document body, then explicitly saves the package to persist changes.

Manipulating PowerPoint Images

The Open XML SDK handles binary content through ImagePart objects. This example from the PowerPoint handling logic shows how to replace slide images:

using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Presentation;

string pptxPath = "slide.pptx";
using var ppt = PresentationDocument.Open(pptxPath, true);
var slidePart = ppt.PresentationPart?.SlideParts.First();
var imagePart = slidePart?.GetPartById("rId2") as ImagePart;

if (imagePart != null)
{
    using var stream = File.OpenRead("newImage.png");
    imagePart.FeedData(stream);
}

The FeedData method streams new binary content into the existing image part without altering the slide's XML markup.

Key Source Files

The following files demonstrate how OfficeCLI leverages the Open XML SDK:

Summary

  • OfficeCLI uses the Open XML SDK (DocumentFormat.OpenXml) as its exclusive C# library for document manipulation.

  • The SDK provides strongly-typed access to Word, PowerPoint, and Excel file formats without requiring Microsoft Office installation.

  • Key namespaces include DocumentFormat.OpenXml.Packaging, DocumentFormat.OpenXml.Wordprocessing, and DocumentFormat.OpenXml.Presentation.

  • Document operations follow a consistent pattern: open the package, manipulate XML parts, and save changes.

  • All document handlers reside in the Handlers/ directory, with Word-specific logic split across modular partial classes.

Frequently Asked Questions

What C# library does OfficeCLI use for document manipulation?

OfficeCLI uses the Open XML SDK (DocumentFormat.OpenXml package) as its primary and only third-party library for document manipulation. This Microsoft-maintained SDK provides APIs for creating, reading, and modifying Open XML documents including Word, Excel, and PowerPoint files.

Does OfficeCLI require Microsoft Office to be installed?

No. Because OfficeCLI uses the Open XML SDK rather than COM interop or Office automation, it operates entirely independently of Microsoft Office installations. The SDK directly manipulates the ZIP-based Open XML package structure and underlying XML parts.

Which namespaces are imported for Word document handling?

The Word handlers import DocumentFormat.OpenXml.Packaging for document container operations and DocumentFormat.OpenXml.Wordprocessing for content-specific elements like Paragraph, Run, and Text objects. These appear in WordHandler.cs at lines 4-6.

How does OfficeCLI handle images in Office documents?

OfficeCLI uses the SDK's ImagePart class to handle binary image data. For Word documents, the WordHandler.ImageHelpers.cs file implements methods to extract existing images or inject new ones using relationship IDs and FeedData() streams, while PowerPoint handling follows similar patterns in the presentation namespace.

Have a question about this repo?

These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:

Share the following with your agent to get started:
curl -s "https://instagit.com/install.md"

Works with
Claude Codex Cursor VS Code OpenClaw Any MCP Client

Maintain an open-source project? Get it listed too →