How to Create and Render Mermaid Diagrams in PowerPoint Using OfficeCLI
OfficeCLI converts Mermaid syntax into editable native PowerPoint shapes by parsing the source into SVG, translating graphic elements into grouped slide objects, and storing the original source as alt-text for later editing.
OfficeCLI is an open-source, cross-platform command-line interface for automating Microsoft Office documents without requiring Office installation. When you need to create and render Mermaid diagrams in PowerPoint using OfficeCLI, the tool treats diagrams as first-class elements, translating text-based flowcharts and sequence diagrams into fully editable slide objects. This headless approach works on Linux, macOS, and Windows, enabling automated documentation pipelines that output presentation-ready visuals.
The Mermaid-to-PowerPoint Rendering Pipeline
OfficeCLI processes diagram generation through a four-stage pipeline implemented in src/officecli/Handlers/Pptx/PowerPointHandler.Add.Diagram.cs. Understanding this workflow helps optimize your diagrams for native editing capabilities.
Parsing the Mermaid Source
The process begins when you issue an add ... --type diagram command. The Mermaid source—whether provided inline or via a .mmd file—is fed to the built-in parser wrapped by DiagramParser.cs. This component generates an intermediate SVG representation that serves as the canonical model for all subsequent rendering steps.
Converting SVG to Native PowerPoint Shapes
The core innovation lies in the SVG-to-shapes conversion. The engine walks the SVG node-by-node, mapping each graphic element—rectangles, lines, arrows, and text—into corresponding native PowerPoint shape primitives. These elements are grouped under a single anchor object within the slide's <p:spTree>, allowing the entire diagram to behave like a standard slide object that you can move, resize, or ungroup for individual element editing.
PNG Fallback for Complex Graphics
When diagrams contain features that cannot map one-to-one to native PowerPoint shapes—such as complex gradients or exotic SVG filters—OfficeCLI employs a high-resolution PNG fallback. The system rasterizes the diagram while preserving the original Mermaid source code in the shape's alt-text attribute, ensuring you retain the ability to regenerate or modify the diagram later.
Package Storage and Relationships
Finally, the diagram group is serialized into the .pptx package structure with the necessary relationship entries. The PowerPointHandler.Add.Diagram implementation handles the XML generation required to register the shape group within the presentation's content tree, maintaining file validity and compatibility with Microsoft PowerPoint.
Step-by-Step Implementation
Creating presentations with Mermaid diagrams requires only standard bash commands and the OfficeCLI binary.
Creating a Presentation and Adding Slides
Initialize your presentation file and optionally add a titled slide:
# Create a new presentation
officecli create deck.pptx
# Add a blank slide with a title
officecli add deck.pptx / --type slide --prop title="Demo"
Inserting Inline Mermaid Flowcharts
Add a flowchart directly via command-line arguments using the --prop text parameter:
officecli add deck.pptx '/slide[1]' \
--type diagram \
--prop text='graph TD; A[Start] --> B{Decision}; B -->|Yes| C[Result]; B -->|No| D[Alternative]'
This command targets the first slide (/slide[1]) and inserts a decision flowchart with directional edges and labeled connections.
Loading Diagrams from External Files
For complex sequence diagrams, maintain your Mermaid code in separate .mmd files:
cat > seq.mmd <<'EOF'
sequenceDiagram
participant Alice
participant Bob
Alice->>Bob: Hello Bob, how are you?
Bob-->>Alice: I am good thanks!
EOF
officecli add deck.pptx '/slide[1]' \
--type diagram \
--prop src=seq.mmd
Storing diagrams externally facilitates version control and reuse across multiple presentations.
Previewing in HTML
Verify your diagrams without opening PowerPoint by rendering the deck to HTML:
officecli view deck.pptx html
This launches a browser-based preview showing the rendered Mermaid diagrams as they will appear in the final presentation.
Customization and Export Options
Control the visual presentation of your diagrams using additional command-line properties:
- Dimensions: Specify
--prop width=15cmand--prop height=10cmto set the exact bounding box of the diagram group. - Editability: Native shapes remain fully editable in PowerPoint—ungroup the object to modify individual colors, fonts, or positions.
- Raster Export: Convert specific slides to PNG for external embedding using
officecli export deck.pptx slide[1] png.
Summary
- Native Conversion: OfficeCLI translates Mermaid syntax into editable PowerPoint shapes via
PowerPointHandler.Add.Diagram.cs, not static images. - Dual Rendering: The engine prioritizes vector-based native shapes but falls back to high-resolution PNG when necessary, preserving source code in alt-text.
- File-Based Workflows: Support for both inline
--prop textand external--prop srcinputs enables flexible documentation pipelines. - Cross-Platform: The entire process runs headlessly without Microsoft Office installation on Linux, macOS, or Windows.
Frequently Asked Questions
Does OfficeCLI require Microsoft PowerPoint to be installed?
No. OfficeCLI operates as a completely headless tool that manipulates the .pptx file format directly. It generates valid Office Open XML documents without COM automation or Office binaries, making it suitable for CI/CD pipelines and server environments.
Can I edit Mermaid diagrams after inserting them into PowerPoint?
Yes. When rendered as native shapes, you can ungroup the diagram object and modify individual elements—changing colors, text, or line styles. If you need to alter the underlying Mermaid logic, extract the source from the shape's alt-text or regenerate the diagram using your original command.
What happens if my Mermaid diagram uses unsupported features?
OfficeCLI automatically detects features that cannot map to native PowerPoint primitives. In these cases, it embeds a high-resolution PNG rasterization of the diagram while storing the original Mermaid source in the shape's alt-text. This ensures visual fidelity while maintaining future editability.
How do I control the size of the diagram on the slide?
Pass dimensional properties during insertion: --prop width=15cm --prop height=10cm. These values define the bounding box of the grouped shape object. You can also resize the diagram manually in PowerPoint after insertion, as the native vector shapes scale without quality loss.
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 →