{"id":2274,"date":"2026-08-28T15:22:25","date_gmt":"2026-08-28T07:22:25","guid":{"rendered":"https:\/\/xlwings.net\/blog\/?p=2274"},"modified":"2026-03-28T12:13:50","modified_gmt":"2026-03-28T12:13:50","slug":"how-to-use-worksheetchartobjects-in-the-xlwings-api-way","status":"publish","type":"post","link":"https:\/\/xlwings.net\/blog\/how-to-use-worksheetchartobjects-in-the-xlwings-api-way\/","title":{"rendered":"How to use Worksheet.ChartObjects in the xlwings API way"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">The <code>ChartObjects<\/code> member of a <code>Worksheet<\/code> object in Excel&#8217;s object model represents a collection of all embedded chart objects (charts that are placed directly on a worksheet, as opposed to chart sheets). In xlwings, this collection is accessible via the <code>api<\/code> property, which provides direct access to the underlying COM object model. This allows for detailed control over chart creation, modification, and management directly from Python.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Functionality:<\/strong><br>The primary function of the <code>ChartObjects<\/code> collection is to enable programmatic manipulation of charts embedded within a specific worksheet. Through xlwings, you can add new charts, access existing ones, modify their properties (such as position, size, and chart type), or delete them. This is particularly useful for automating report generation, dynamically updating visualizations based on data changes, or creating dashboards where charts need to be precisely positioned alongside data.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Syntax:<\/strong><br>In xlwings, you access the <code>ChartObjects<\/code> collection via the <code>api<\/code> property of a <code>Sheet<\/code> object (which corresponds to a worksheet). The typical syntax is:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>chart_objects = ws.api.ChartObjects<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Here, <code>ws<\/code> is an xlwings <code>Sheet<\/code> object representing the worksheet. The <code>ChartObjects<\/code> object has several methods and properties. A key method for adding a new chart is <code>Add<\/code>, which has the following parameters:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>Left<\/code> (Required, <code>float<\/code>): The position, in points, of the left edge of the new chart object relative to the left edge of column A on the worksheet.<\/li>\n\n\n\n<li><code>Top<\/code> (Required, <code>float<\/code>): The position, in points, of the top edge of the new chart object relative to the top edge of row 1.<\/li>\n\n\n\n<li><code>Width<\/code> (Required, <code>float<\/code>): The width of the new chart object in points.<\/li>\n\n\n\n<li><code>Height<\/code> (Required, <code>float<\/code>): The height of the new chart object in points.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">The method returns a <code>ChartObject<\/code> object, which has a <code>Chart<\/code> property that provides access to the actual chart (where you set the chart type, data source, etc.).<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Code Examples:<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Adding a New Chart:<\/strong><\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>import xlwings as xw\n\n# Connect to an existing workbook or create a new one\nwb = xw.Book() # Creates a new workbook\nws = wb.sheets&#91;'Sheet1']\n\n# Add some sample data\nws.range('A1').value = &#91;&#91;'Month', 'Sales'], &#91;'Jan', 150], &#91;'Feb', 200], &#91;'Mar', 180]]\n\n# Access the ChartObjects collection and add a new chart\nchart_obj = ws.api.ChartObjects().Add(Left=100, Top=50, Width=300, Height=200)\nchart = chart_obj.Chart\n\n# Set the chart type and data source\nchart.ChartType = 51 # xlColumnClustered (value from Excel constants)\nchart.SetSourceData(Source=ws.range('A1:B4').api)\nchart.HasTitle = True\nchart.ChartTitle.Text = \"Monthly Sales\"<\/code><\/pre>\n\n\n\n<ol start=\"2\" class=\"wp-block-list\">\n<li><strong>Accessing and Modifying an Existing Chart:<\/strong><br>Assume there is already an embedded chart named &#8220;Chart 1&#8221; on the worksheet.<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code># Access the ChartObjects collection\nall_charts = ws.api.ChartObjects\n\n# Access a specific chart by its index (1-based) or name\nchart_obj = all_charts(1) # First chart in the collection\n# Or by name: chart_obj = all_charts(\"Chart 1\")\n\n# Modify its position and size\nchart_obj.Left = 200\nchart_obj.Top = 100\nchart_obj.Width = 400\nchart_obj.Height = 250\n\n# Change the chart title via the Chart property\nchart_obj.Chart.ChartTitle.Text = \"Updated Sales Report\"<\/code><\/pre>\n\n\n\n<ol start=\"3\" class=\"wp-block-list\">\n<li><strong>Looping Through All Charts on a Worksheet:<\/strong><\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code># Iterate over all embedded chart objects\nfor chart_obj in ws.api.ChartObjects:\nprint(f\"Chart Name: {chart_obj.Name}\")\nprint(f\"Chart Type: {chart_obj.Chart.ChartType}\")\n# You can perform operations like checking title or changing data series\nif chart_obj.Chart.HasTitle:\n    print(f\"Title: {chart_obj.Chart.ChartTitle.Text}\")<\/code><\/pre>\n\n\n\n<ol start=\"4\" class=\"wp-block-list\">\n<li><strong>Deleting a Chart:<\/strong><\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code># Delete a specific chart by name\nws.api.ChartObjects(\"Chart 1\").Delete()\n# Or delete all charts on the worksheet\nfor chart_obj in ws.api.ChartObjects:\n    chart_obj.Delete()<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The `ChartObjects` member of a `Worksheet` object in Excel&apos;s object model represents a collection of&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[25],"tags":[],"class_list":["post-2274","post","type-post","status-publish","format-standard","hentry","category-xlwings-api-reference"],"_links":{"self":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2274","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/comments?post=2274"}],"version-history":[{"count":1,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2274\/revisions"}],"predecessor-version":[{"id":3472,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2274\/revisions\/3472"}],"wp:attachment":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/media?parent=2274"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/categories?post=2274"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/tags?post=2274"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}