Python and Google Slides
Create a chart from Python and place it on a slide with a Google service account, editable in ChartKit.
This guide builds a chart from Python with a team API key and puts it on a Google Slides slide with a service account. The image on the slide carries an alt text that points to the ChartKit chart, so analysts who open the deck with the ChartKit extension can edit the chart there. The full script at the end runs as is.
Before you start
- A team API key in the
CHARTKIT_API_KEYenvironment variable. See create a key. - A Google Cloud project with the Google Slides API enabled, and a service account. Locally, point
GOOGLE_APPLICATION_CREDENTIALSat its JSON key file. On Cloud Run, GKE or a VM, the attached service account works without a key file. - The presentation shared with the service account's email address as an editor. On a shared drive, add the service account to the drive instead.
- Python 3.9 or later.
Call the server
Each request is an HTTP POST with one JSON-RPC 2.0 message in the body, and the reply is plain JSON. The server keeps no session, so you can send tools/call directly without initialize first.
A tool call fails in one of three places, and the function above checks each one.
| Where | What it means |
|---|---|
| HTTP 401 | The key is missing, unknown or revoked. |
error in the JSON-RPC reply | The input did not validate, or the tool or chart id does not exist. The message says which field is wrong. |
result.isError is true | The tool ran but could not finish, for example because the team licence is not active. |
The chart input reference lists every field create_chart accepts. To check the connection or read the tool schemas, send initialize and tools/list the same way.
The response
Read result.structuredContent. The text block in result.content is written for an AI assistant and its wording can change.
| Field | Use |
|---|---|
chartId | The chart's id. Pass it to update_chart or get_chart_svg. |
imageUrl | PNG on a white background, rendered at twice the chart's size: sizePt × 8/3 pixels. Insert this into Slides. |
svgUrl | The same chart as SVG, for web pages and documents. |
slidesAltText | The alt text description to set on the image, ChartKit: followed by the chart id. |
sizePt | The size in points to insert the image at. It fits inside the size you sent. |
model | The stored ChartKit chart model and the palette it was drawn in. |
With the MCP Python SDK
The official mcp package works too. Version 2 takes headers through an httpx2 client. JSON-RPC errors raise MCPError, and a tool error result comes back with is_error set.
With the SDK, a 401 surfaces when Client opens the connection, as an ExceptionGroup that wraps an MCPError, not as an HTTP status. If the connection fails before any tool call, check the key first.
Fetch the image once
The PNG is rendered the first time someone requests imageUrl. Request it yourself before you call the Slides API, so Google's fetch during createImage gets a finished image.
The URL belongs to one version of the chart. After update_chart, use the new imageUrl it returns; the old one returns 404.
Put the chart on a slide
Send the box the chart will fill as size when you create it, and insert the image at the sizePt the result returns. ChartKit lays the chart out for that box, and the extension reopens a chart at exactly the size it was drawn for. An image inserted at sizePt opens in ChartKit with the same layout and text size it has on the slide; a chart drawn for another size and scaled into the box would change when an analyst opens it.
One batchUpdate creates the image from imageUrl and sets its alt text. Slides applies the requests in order, so the second request can refer to the image the first one creates.
- Sizes and positions are in points (
PT) or EMU, with 12,700 EMU to a point. A 16:9 slide is 720 × 405 points.sizePtcan be smaller than the box, for example a donut in a wide box, so centre the image in it. objectIdis optional. If you set one, it must be 5 to 50 characters of letters, digits,_,-or:, and unique in the presentation.- Google copies the image into the presentation at insert time. Later changes to the hosted PNG do not change the slide.
The alt text
The ChartKit extension finds its charts by the image's alt text description. Set description to slidesAltText exactly as returned, and title to ChartKit, which is what the extension writes for its own charts. Do not add other text to the description.
When someone opens the deck in Chrome with the ChartKit extension, they can open the image in ChartKit and change its data, type or styling. ChartKit reads the full chart from the id in the alt text.
Update a chart from code
Call update_chart with the chart_id and only the fields that change, for example a new table next month. It returns a new imageUrl and keeps the chart id, so the alt text stays the same. To swap the picture on the slide, send a replaceImage request for the existing image with the new URL. With an API key, your code replaces the picture. The server only refreshes charts in decks that a signed-in person has open.
The chart keeps the box it was created for, so sizePt usually stays the same. If the new sizePt differs from the image on the slide, delete the image and insert the new one at sizePt: replaceImage keeps the old size.
Full script
The script draws the chart for the box under the slide title, adds a slide with that title, and centres the chart in the box. Pass an existing slide's object id as the second argument to use that slide instead. Set CHARTKIT_MCP_URL to call another ChartKit server, such as a preview deployment.
Run it
The presentation id is the part of the deck URL between /d/ and /edit. The script prints the chart id and a link to the new slide.
Troubleshooting
The key is wrong, revoked or sent without the Bearer prefix. Check the header the request sends.
The presentation is not shared with the service account, or the id is wrong. Share the deck with the service account's email address as an editor.
Google could not fetch imageUrl. Fetch the URL once before createImage, and use the URL from the latest create_chart or update_chart response.
Check the image's alt text in Slides (right-click, Alt text). The description must be exactly the slidesAltText value, starting with ChartKit:.
More causes and fixes are on Errors and limits.