{"id":96,"date":"2026-03-04T11:15:40","date_gmt":"2026-03-04T03:15:40","guid":{"rendered":"https:\/\/xlwings.net\/blog\/?p=96"},"modified":"2026-01-17T11:14:30","modified_gmt":"2026-01-17T11:14:30","slug":"how-to-create-a-chart-using-the-shapes-object","status":"publish","type":"post","link":"https:\/\/xlwings.net\/blog\/how-to-create-a-chart-using-the-shapes-object\/","title":{"rendered":"How To Create a Chart Using the Shapes Object?"},"content":{"rendered":"<h2>Method<\/h2>\n<p style=\"margin: 0in; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14.0pt;\">Creating a chart using the `Shapes` object is essentially achieved through the API method in `xlwings`. In this approach, the `AddChart2` method of the `Shapes` object is used to create the chart. The syntax is as follows:<\/p>\n<p style=\"margin: 0in; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 8.0pt;\">\u00a0<\/p>\n<p lang=\"en-US\" style=\"margin: 0in; margin-left: .375in; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14.0pt;\">sht.api.Shapes.AddChart2(Style,XlChartType,Left,Top,Width,Height,NewLayout)<\/p>\n<p style=\"margin: 0in; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 8.0pt;\">\u00a0<\/p>\n<p style=\"margin: 0in; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14.0pt;\">Here, `sht` represents the specified worksheet, and there are seven parameters, all of which are optional:<\/p>\n<p style=\"margin: 0in; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14.0pt;\">&#8211; `Style` \u2013 The chart style. If the value is `-1`, it represents the default style for each chart type.<\/p>\n<p style=\"margin: 0in; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14.0pt;\">&#8211; `XlChartType` \u2013 The chart type, represented by the `XlChartType` enumeration. A partial list of values is provided in Table 6-1.<\/p>\n<p style=\"margin: 0in; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14.0pt;\">&#8211; `Left` \u2013 The left position of the chart. If omitted, the chart will be horizontally centered.<\/p>\n<p style=\"margin: 0in; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14.0pt;\">&#8211; `Top` \u2013 The top position of the chart. If omitted, the chart will be vertically centered.<\/p>\n<p style=\"margin: 0in; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14.0pt;\">&#8211; `Width` \u2013 The width of the chart. If omitted, the default value is 354.<\/p>\n<p style=\"margin: 0in; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14.0pt;\">&#8211; `Height` \u2013 The height of the chart. If omitted, the default value is 210.<\/p>\n<p style=\"margin: 0in; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14.0pt;\">&#8211; `NewLayout` \u2013 Indicates the chart layout. If `True`, only composite charts will display legends.<\/p>\n<p style=\"margin: 0in; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14.0pt;\">This method returns a `Shape` object representing the chart.<\/p>\n<p style=\"margin: 0in; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 8.0pt;\">\u00a0<\/p>\n<p style=\"margin: 0in; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14.0pt;\">sht.api.Range(&#8216;A1&#8217;).CurrentRegion.Select()<\/p>\n<p style=\"margin: 0in; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14.0pt;\">sht.api.Shapes.AddChart2(-1,xw.constants.ChartType.xlColumnClustered,30,150,300,200,True)<\/p>\n<h2>Sample Code<\/h2>\n\n\n<pre class=\"wp-block-code\"><code>#Create chart - Shapes object\n\nimport xlwings as xw\nimport os\n\nroot = os.getcwd()\napp = xw.App(visible=True, add_book=False)\nwb=app.books.open('GDP.xlsx',read_only=False)\nsht=wb.sheets(1)\n\nsht.api.Range('A1').CurrentRegion.Select()\nsht.api.Shapes.AddChart2(-1,xw.constants.ChartType.xlColumnClustered,30,150,300,200,True)\n\n#wb.save()\n#wb.close()\n#app.kill()<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"883\" height=\"672\" src=\"https:\/\/xlwings.net\/blog\/wp-content\/uploads\/2026\/03\/image-6.png\" alt=\"Create a Chart Using the Shapes Object\" class=\"wp-image-473\" srcset=\"https:\/\/xlwings.net\/blog\/wp-content\/uploads\/2026\/03\/image-6.png 883w, https:\/\/xlwings.net\/blog\/wp-content\/uploads\/2026\/03\/image-6-300x228.png 300w, https:\/\/xlwings.net\/blog\/wp-content\/uploads\/2026\/03\/image-6-768x584.png 768w\" sizes=\"auto, (max-width: 883px) 100vw, 883px\" \/><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>Method<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[],"class_list":["post-96","post","type-post","status-publish","format-standard","hentry","category-xlwings-chart"],"_links":{"self":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/96","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=96"}],"version-history":[{"count":3,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/96\/revisions"}],"predecessor-version":[{"id":475,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/96\/revisions\/475"}],"wp:attachment":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/media?parent=96"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/categories?post=96"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/tags?post=96"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}