How to Create and Manipulate Charts in PowerPoint Presentations Using OfficeCLI

OfficeCLI enables programmatic chart management through the PowerPointHandler class and ChartHelper utilities, exposing CRUD operations via CLI commands like officecli add, set, and get using XPath-style paths such as /slide[1]/chart[1].

OfficeCLI is an open-source command-line interface for automating Microsoft Office documents without COM interop dependencies. When you need to programmatically create and manipulate charts in PowerPoint presentations using OfficeCLI, the tool leverages a handler-based architecture that translates simple CLI verbs into complex OpenXML operations.

Architecture of Chart Handling in OfficeCLI

OfficeCLI treats a PowerPoint file (*.pptx) as a collection of handlers. The PowerPointHandler (src/officecli/Handlers/Pptx/PowerPointHandler.cs) serves as the central component implementing CRUD verbs—add, set, get, query, and remove—for every element inside a slide deck.

The actual XML manipulation occurs in ChartHelper (src/officecli/Core/Chart/ChartHelper.cs), which abstracts the OpenXML schema through specialized reader, writer, and setter methods. This design ensures consistent behavior across PowerPoint, Word, and Excel chart operations.

When you execute a command, the CLI parses your input into a BatchItem that records the verb, target path, and properties. The ResidentServer routes this to PowerPointHandler, which uses ResolveChart (located in PowerPointHandler.Resolve.cs) to parse paths like /slide[1]/chart[2] and retrieve the underlying OpenXML parts.

Adding Charts to PowerPoint Slides

To create a new chart, use the add verb with the --type chart parameter. The handler invokes ChartHelper.BuildChartSpace to generate the OpenXML <c:chart> structure, creates a graphic frame on the slide, and establishes the necessary package relationships.

officecli add deck.pptx /slide[1] \
  --type chart \
  --chartType bar \
  --title "Quarterly Sales" \
  --categories "Q1 Q2 Q3 Q4" \
  --series "North,100,150,200,250" \
  --series "South,80,120,160,200"

Under the hood, PowerPointHandler.Add.Chart (implemented in PowerPointHandler.Add.Chart.cs) builds the chart part and links it to the slide's <p:graphicFrame> element before saving the package.

Modifying Chart Position and Appearance

Use the set verb with specific paths to resize, reposition, or update chart metadata. The SetChartByPath method in src/officecli/Handlers/Pptx/PowerPointHandler.Set.Chart.cs handles property distribution between the graphic frame (position/size) and the chart part (titles, styles).

Resizing and Repositioning Charts

To move or resize a chart, provide the --anchor parameter with comma-separated values for x, y, width, and height:

officecli set deck.pptx /slide[1]/chart[1] \
  --anchor 2cm,3cm,12cm,8cm \
  --title "Updated Sales"

The method expands the anchor string into individual coordinates (lines 47-60 of PowerPointHandler.Set.Chart.cs) and updates the <a:transform> element of the chart's graphic frame via TryApplyPositionSize.

Updating Chart Data and Series

OfficeCLI allows granular manipulation of individual data series through path extensions like /series[N].

Modifying Series Values

Target specific series by appending /series[2] (or appropriate index) to your path. The system automatically prefixes properties with series2. (lines 65-67) and forwards them to ChartHelper.SetChartProperties:

officecli set deck.pptx /slide[1]/chart[1]/series[2] \
  --values "90,130,170,210"

This rewrites the <c:val> nodes within the corresponding <c:ser> element in the chart XML.

Configuring Chart Axes

For axis-specific modifications, use the /axis[@role=...] path selector. The SetChartAxisByPath method (lines 18-26 of PowerPointHandler.Set.Chart.cs) resolves the chart part and delegates to ChartHelper.SetAxisProperties.

Rotating Axis Labels

Adjust label alignment and rotation by targeting the value or category axis:

officecli set deck.pptx /slide[1]/chart[1]/axis[@role=val] \
  --labelrotation 45

This updates <c:lblAlgn> and <c:lblOffset> elements within the axis definition.

Querying Chart Metadata

Retrieve chart information using the get verb with the --json flag for structured output. The PowerPointHandler.Query method utilizes ChartHelper.ReadChartProperties to serialize the chart's XML into JSON.

officecli get deck.pptx /slide[1]/chart[1] --json

The output includes properties such as chartType, series arrays, categories, and axis configurations extracted from the chart part.

Summary

  • Handler-based architecture: PowerPointHandler.cs routes CLI commands to specific chart operations while ChartHelper.cs manages OpenXML intricacies.
  • Path-based addressing: Use XPath-style syntax like /slide[1]/chart[1]/series[2] to target specific elements.
  • Comprehensive CRUD support: Create charts with add, modify properties with set, and inspect data with get or query.
  • Cross-application consistency: The same ChartHelper utilities power chart operations in Word and Excel, ensuring uniform behavior.

Frequently Asked Questions

What file formats does OfficeCLI support for PowerPoint charts?

OfficeCLI operates on standard Office Open XML format (*.pptx) files. According to the source code in PowerPointHandler.cs, the tool manipulates the underlying <c:chart> elements within the presentation package, ensuring compatibility with PowerPoint 2007 and later versions.

How do I update multiple series at once?

While the CLI processes one path per command, you can chain multiple set operations or use batch scripts. Each series requires its own path specification (e.g., /series[1], /series[2]), with ChartHelper.SetChartProperties handling each update independently through the property prefixing mechanism described in PowerPointHandler.Set.Chart.cs.

Can I convert PowerPoint charts to images using OfficeCLI?

Yes. The PowerPointHandler.HtmlPreview.Charts.cs file implements rendering capabilities that generate SVG/PNG representations of charts when using the --render or --html export commands, enabling automated chart extraction for web displays or documentation.

Where is the core chart manipulation logic located?

The primary chart XML processing occurs in src/officecli/Core/Chart/ChartHelper.cs, with specialized setter logic in ChartHelper.Setter.cs and reading capabilities in ChartHelper.Reader.cs. The PowerPoint-specific orchestration resides in src/officecli/Handlers/Pptx/PowerPointHandler.Set.Chart.cs and PowerPointHandler.Add.Chart.cs.

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 →