{"id":48,"date":"2026-02-10T18:30:01","date_gmt":"2026-02-10T10:30:01","guid":{"rendered":"https:\/\/openpyxl.com\/blog\/?p=48"},"modified":"2026-01-16T07:35:16","modified_gmt":"2026-01-16T07:35:16","slug":"general-process-of-creating-charts-in-openpyxl","status":"publish","type":"post","link":"https:\/\/openpyxl.com\/blog\/general-process-of-creating-charts-in-openpyxl.html","title":{"rendered":"General Process of Creating Charts in OpenPyXL"},"content":{"rendered":"<h2>Method<\/h2>\n<p style=\"margin: 0in; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14.0pt;\">from openpyxl import Workbook, load_workbook<\/p>\n<p style=\"margin: 0in; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14.0pt;\">from openpyxl.chart import LineChart, Reference, Series<\/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;\">wb=load_workbook(&#8216;Sales.xlsx&#8217;)<\/p>\n<p style=\"margin: 0in; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14.0pt;\">ws=wb.worksheets[0]<\/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;\">data=Reference(ws, min_col=2, min_row=1, max_col=4, max_row=6)<\/p>\n<p style=\"margin: 0in; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14.0pt;\">labels=Reference(ws, min_col=1, min_row=2, max_col=1, max_row=6)<\/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;\">chart=LineChart()<\/p>\n<p style=\"margin: 0in; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14.0pt;\">chart.add_data(data, titles_from_data=True)<\/p>\n<p style=\"margin: 0in; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14.0pt;\">chart.set_categories(labels)<\/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;\">chart.title=&#8217;Product Sales&#8217;<\/p>\n<p style=\"margin: 0in; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14.0pt;\">chart.x_axis.title=&#8217;Product Name&#8217;<\/p>\n<p style=\"margin: 0in; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14.0pt;\">chart.y_axis.title=&#8217;Sales&#8217;<\/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;\">ws.add_chart(chart, &#8216;E2&#8217;)<\/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;\">wb.save(&#8216;test.xlsx&#8217;)<\/p>\n<p style=\"margin: 0in; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14.0pt;\">wb.close()<\/p>\n<h2>Sample Code<\/h2>\n\n\n<pre class=\"wp-block-code\"><code>#General Process of Plotting with OpenPyXL\n\n# Import relevant modules such as Workbook, load_workbook,\n# and charts from openpyxl\nfrom openpyxl import Workbook, load_workbook\nfrom openpyxl.chart import LineChart, Reference, Series\n\n# Read the file\nwb=load_workbook('Sales.xlsx')\n# Select the first worksheet\nws=wb.worksheets&#91;0]\n\n# Get the data from the range B2:D6 and\n# save it as a Reference object\ndata=Reference(ws, min_col=2, min_row=1, max_col=4, max_row=6)\n# Get the data from the range A2:A6 for the x-axis of the bar chart\nlabels=Reference(ws, min_col=1, min_row=2, max_col=1, max_row=6)\n\n# Create a bar chart object\nchart=LineChart()\n# Set the data source\nchart.add_data(data, titles_from_data=True)\n# Set x-axis labels\nchart.set_categories(labels)\n\n# Set chart and axis titles\nchart.title='Product Sales'\nchart.x_axis.title='Product Name'\nchart.y_axis.title='Sales'\n\n# Insert the bar chart into a specified cell\nws.add_chart(chart, 'E2')\n\n# Save the file\nwb.save('test.xlsx')\nwb.close()<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"872\" height=\"680\" src=\"https:\/\/openpyxl.com\/blog\/wp-content\/uploads\/2026\/02\/30.png\" alt=\"Creating Charts in OpenPyXL\" class=\"wp-image-314\" srcset=\"https:\/\/openpyxl.com\/blog\/wp-content\/uploads\/2026\/02\/30.png 872w, https:\/\/openpyxl.com\/blog\/wp-content\/uploads\/2026\/02\/30-300x234.png 300w, https:\/\/openpyxl.com\/blog\/wp-content\/uploads\/2026\/02\/30-768x599.png 768w\" sizes=\"auto, (max-width: 706px) 89vw, (max-width: 767px) 82vw, 740px\" \/><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>OpenPyXL<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[],"class_list":["post-48","post","type-post","status-publish","format-standard","hentry","category-openpyxl-chart"],"_links":{"self":[{"href":"https:\/\/openpyxl.com\/blog\/wp-json\/wp\/v2\/posts\/48","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/openpyxl.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/openpyxl.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/openpyxl.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/openpyxl.com\/blog\/wp-json\/wp\/v2\/comments?post=48"}],"version-history":[{"count":2,"href":"https:\/\/openpyxl.com\/blog\/wp-json\/wp\/v2\/posts\/48\/revisions"}],"predecessor-version":[{"id":315,"href":"https:\/\/openpyxl.com\/blog\/wp-json\/wp\/v2\/posts\/48\/revisions\/315"}],"wp:attachment":[{"href":"https:\/\/openpyxl.com\/blog\/wp-json\/wp\/v2\/media?parent=48"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/openpyxl.com\/blog\/wp-json\/wp\/v2\/categories?post=48"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/openpyxl.com\/blog\/wp-json\/wp\/v2\/tags?post=48"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}