How to Extract Plain Text from Office Documents with OfficeCLI
OfficeCLI extracts clean plain text from Word, PowerPoint, and Excel files using the get command with the --format text flag, parsing Open XML nodes while discarding markup and metadata.
OfficeCLI is a cross-platform command-line tool that reads Microsoft Office Open XML files without requiring the Office desktop suite. Whether you need to index document content, perform batch text analysis, or convert presentations to readable logs, you can extract plain text from Office documents with OfficeCLI using straightforward syntax that targets specific document parts.
How OfficeCLI Extracts Plain Text
OfficeCLI implements format-specific handlers that traverse the Open XML structure differently for each application.
Word Document Extraction
In src/officecli/Handlers/Word/WordHandler.View.cs, the extraction routine walks the <w:p> (paragraph) and <w:r> (run) hierarchy defined in the Office Open XML specification. It aggregates the text content from each run node while stripping formatting attributes, field codes, and revision marks. This produces a continuous string that preserves paragraph boundaries as line breaks.
PowerPoint Presentation Extraction
For slide decks, src/officecli/Handlers/Pptx/PowerPointHandler.Query.cs targets <a:t> elements—the text runs inside drawing shapes. When the CLI receives a request with the text format flag, the handler iterates through each slide's shape tree, concatenating visible text nodes and omitting graphics, charts, and placeholder metadata.
Extracting Text via the Command Line
The get command supports the --format text option (alias -t) to stream extracted content to standard output or redirect it to a file.
Print Text to Console
To output the text of a Word document or PowerPoint deck directly to your terminal:
officecli get document.docx --format text
officecli get slides.pptx -t
Save Text to a File
Redirect STDOUT to create a plain text copy:
officecli get report.docx -t > report.txt
Extract Specific Document Parts
Combine the --path selector with the text flag to target specific sections. For example, to extract only the first section of a Word file:
officecli get contract.docx --path "/document/body/sectPr[1]" -t
Batch Process Multiple Files
Process entire directories using shell loops:
for file in *.docx; do
officecli get "$file" -t > "${file%.docx}.txt"
done
Under the Hood: Open XML Parsing
According to the src/officecli/officecli.csproj configuration, the CLI leverages the Open XML SDK for .NET builds and a pure JavaScript parser for the Node SDK. The extraction engine applies a sibling-range extractor that skips non-text nodes such as images, charts, and hidden fields. This approach respects the original document's reading order, preserving line breaks and paragraph structure while discarding all presentation markup.
Summary
- OfficeCLI extracts plain text using the
getcommand with--format textor-t. - Word extraction logic resides in
WordHandler.View.cs, parsing<w:p>and<w:r>nodes. - PowerPoint extraction is handled in
PowerPointHandler.Query.cs, reading<a:t>elements from shapes. - Output can be redirected to files or piped to other tools for batch processing.
- The underlying parser uses the Open XML SDK to navigate document parts while filtering out non-content nodes.
Frequently Asked Questions
Does OfficeCLI require Microsoft Office to be installed?
No. OfficeCLI operates independently by reading the underlying Open XML files directly through the Open XML SDK or its JavaScript parser, making it suitable for server environments and CI/CD pipelines.
Can I extract text from Excel spreadsheets using the same method?
Yes. The --format text flag works with Excel files (.xlsx), though the specific handler logic differs from Word and PowerPoint. The tool extracts cell values and concatenates them with appropriate delimiters based on the sheet structure.
What happens to formatting and images during extraction?
The sibling-range extractor intentionally skips non-text nodes including images, charts, drawings, and styling attributes. Only visible text content is returned, with paragraph breaks preserved as line breaks.
Is there a programmatic API for text extraction?
Yes. The Node SDK exposes the same functionality documented in sdk/node/README.md, allowing you to call extraction methods directly from JavaScript applications without invoking the CLI subprocess.
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:
curl -s "https://instagit.com/install.md" Maintain an open-source project? Get it listed too →