How to Create a Bar Chart in Excel Like a Pro [Python Tutorial]
Ever spent hours tweaking Excel charts to make them look just right? You’re not alone. Creating visually appealing bar charts in Excel can be time-consuming—especially when you need to repeat the process for multiple datasets. In this tutorial, we’ll show you how to automate it all with Python. From generating standard bar charts to building stacked versions, you’ll learn how to transform your raw data into polished, presentation-ready visuals in just a few lines of code.
Introduction to Spire.XLS for Python
To turn those polished, presentation-ready bar charts into reality, we’ll use Spire.XLS for Python. This powerful library lets you create and customize Excel charts—standard or stacked—directly from Python, without ever opening Excel. Best of all, the free version works without adding watermarks, making it ideal for testing and small projects.
Installation:
Install Spire.XLS for Python via pip:
pip install spire.xls
With Spire.XLS ready, you can start transforming raw data into professional Excel visuals in just a few lines of code.
How to Create a Bar Chart in Excel Quickly with Python
Now that Spire.XLS is set up, it’s time to bring your data to life. Bar charts are one of the most effective ways to compare values across categories, and Python makes it easy to automate their creation in Excel. In the following steps, we’ll guide you through building a clean, professional bar chart and then provide a ready-to-use code example so you can see it in action immediately.
How it works in Python:
Instantiate a Workbook object to start working with an Excel file.
Access a specific worksheet using the Workbook.Worksheets[index] property.
Populate the worksheet with chart data in designated cells and apply formatting to enhance readability.
Add a clustered bar chart to the worksheet via the Worksheet.Charts.Add(ExcelChartType.BarClustered) method.
Define the chart’s data range using the Chart.DataRange property.
Configure the chart’s layout, including its position, title, category axis, and value axis.
Save the workbook to a file using the Workbook.SaveToFile() method.
The code example below shows how to create a bar chart in a new Excel file:
from spire.xls import *
from spire.xls.common import *
# Create a Workbook instance
workbook = Workbook()
# Get the first sheet and set its name
sheet = workbook.Worksheets[0]
sheet.Name = "Bar Chart"
# Add chart data to specified cells
sheet.Range["A1"].Value = "Category"
sheet.Range["A2"].Value = "Apples"
sheet.Range["A3"].Value = "Oranges"
sheet.Range["A4"].Value = "Bananas"
sheet.Range["B1"].Value = "Q1"
sheet.Range["B2"].NumberValue = 120
sheet.Range["B3"].NumberValue = 95
sheet.Range["B4"].NumberValue = 140
sheet.Range["C1"].Value = "Q2"
sheet.Range["C2"].NumberValue = 135
sheet.Range["C3"].NumberValue = 110
sheet.Range["C4"].NumberValue = 125
sheet.Range["D1"].Value = "Q3"
sheet.Range["D2"].NumberValue = 150
sheet.Range["D3"].NumberValue = 105
sheet.Range["D4"].NumberValue = 130
sheet.Range["E1"].Value = "Q4"
sheet.Range["E2"].NumberValue = 160
sheet.Range["E3"].NumberValue = 120
sheet.Range["E4"].NumberValue = 145
# Set cell styles
sheet.Range["A1:E1"].RowHeight = 20
sheet.Range["A1:E1"].Style.Color = Color.get_Black()
sheet.Range["A1:E1"].Style.Font.Color = Color.get_White()
sheet.Range["A1:E1"].Style.Font.IsBold = True
sheet.Range["A1:E1"].Style.Font.Size = 11
sheet.Range["A1:E1"].Style.VerticalAlignment = VerticalAlignType.Center
sheet.Range["A1:E1"].Style.HorizontalAlignment = HorizontalAlignType.Center
sheet.Range["B2:E4"].Style.NumberFormat = "#,##0"
# Add a bar chart to the worksheet
chart = sheet.Charts.Add(ExcelChartType.BarClustered)
# Set data range for the chart
chart.DataRange = sheet.Range["A1:E4"]
# Set position of chart
chart.LeftColumn = 1
chart.TopRow = 6
chart.RightColumn = 12
chart.BottomRow = 27
# Set and format chart title
chart.ChartTitle = "Quarterly Sales by Category"
chart.ChartTitleArea.IsBold = True
chart.ChartTitleArea.Size = 12
# Set the category axis of the chart
chart.PrimaryCategoryAxis.Title = "Category"
chart.PrimaryCategoryAxis.Font.IsBold = True
chart.PrimaryCategoryAxis.TitleArea.IsBold = True
# Set the value axis of the chart
chart.PrimaryValueAxis.Title = "Units Sold"
chart.PrimaryValueAxis.HasMajorGridLines = False
chart.PrimaryValueAxis.TitleArea.TextRotationAngle = 90
chart.PrimaryValueAxis.MinValue = 0
chart.PrimaryValueAxis.TitleArea.IsBold = True
# Set series colors and data labels
for cs in chart.Series:
cs.Format.Options.IsVaryColor = True
cs.DataPoints.DefaultDataPoint.DataLabels.HasValue = True
# Set legend position
chart.Legend.Position = LegendPositionType.Top
# Save the document
workbook.SaveToFile("/output/BarChart.xlsx", ExcelVersion.Version2016)
workbook.Dispose()
Preview of the result file:

Easily Create a Stacked Bar Chart in Excel Using Python
While standard bar charts are great for comparing individual values, stacked bar charts let you visualize how different components contribute to a total within each category. This makes them ideal for showing proportions or segment breakdowns. Using Spire.XLS for Python, you can create stacked bar charts just as easily as regular ones, with full control over colors, labels, and layout. In the next section, we’ll walk through the steps and provide a code example to illustrate how each segment stacks neatly in Excel.
Steps to create stacked bar charts in Excel with Python:
Create a Workbook object to start working with an Excel file.
Access a specific worksheet via the Workbook.Worksheets[index] property.
Populate the worksheet with chart data in designated cells and apply appropriate cell styles.
Add a stacked bar chart to the worksheet using Worksheet.Charts.Add(ExcelChartType.BarStacked).
Specify the chart’s data range using the Chart.DataRange property.
Configure the chart’s position, title, category axis, and value axis for proper layout.
Save the final workbook using Workbook.SaveToFile().
from spire.xls import *
# Create a Workbook instance
workbook = Workbook()
# Get the first sheet and set its name
sheet = workbook.Worksheets[0]
sheet.Name = "StackedBar"
# Add chart data to specified cells
sheet.Range["A1"].Value = "Quarter"
sheet.Range["A2"].Value = "Q1"
sheet.Range["A3"].Value = "Q2"
sheet.Range["A4"].Value = "Q3"
sheet.Range["A5"].Value = "Q4"
sheet.Range["B1"].Value = "Apples"
sheet.Range["B2"].NumberValue = 120
sheet.Range["B3"].NumberValue = 135
sheet.Range["B4"].NumberValue = 150
sheet.Range["B5"].NumberValue = 160
sheet.Range["C1"].Value = "Oranges"
sheet.Range["C2"].NumberValue = 95
sheet.Range["C3"].NumberValue = 110
sheet.Range["C4"].NumberValue = 105
sheet.Range["C5"].NumberValue = 120
sheet.Range["D1"].Value = "Bananas"
sheet.Range["D2"].NumberValue = 140
sheet.Range["D3"].NumberValue = 125
sheet.Range["D4"].NumberValue = 130
sheet.Range["D5"].NumberValue = 145
# Set cell style
sheet.Range["A1:D1"].RowHeight = 18
sheet.Range["A1:D1"].Style.Color = Color.get_Black()
sheet.Range["A1:D1"].Style.Font.Color = Color.get_White()
sheet.Range["A1:D1"].Style.Font.IsBold = True
sheet.Range["A1:D1"].Style.VerticalAlignment = VerticalAlignType.Center
sheet.Range["A1:D1"].Style.HorizontalAlignment = HorizontalAlignType.Center
sheet.Range["A2:A5"].Style.HorizontalAlignment = HorizontalAlignType.Center
sheet.Range["B2:D5"].Style.NumberFormat = "#,##0"
# Add a stacked bar chart to the sheet
chart = sheet.Charts.Add(ExcelChartType.BarStacked)
# Set data range of the chart
chart.DataRange = sheet.Range["A1:D5"]
chart.SeriesDataFromRange = False
# Set position of the chart
chart.LeftColumn = 1
chart.TopRow = 6
chart.RightColumn = 11
chart.BottomRow = 29
# Set and format chart title
chart.ChartTitle = "Quarterly Sales by Category"
chart.ChartTitleArea.IsBold = True
chart.ChartTitleArea.Size = 12
# Set and format category axis
chart.PrimaryCategoryAxis.Title = "Quarter"
chart.PrimaryCategoryAxis.Font.IsBold = True
chart.PrimaryCategoryAxis.TitleArea.IsBold = True
chart.PrimaryCategoryAxis.TitleArea.TextRotationAngle = 0
# Set and format value axis
chart.PrimaryValueAxis.Title = "Units Sold"
chart.PrimaryValueAxis.HasMajorGridLines = False
chart.PrimaryValueAxis.MinValue = 0
chart.PrimaryValueAxis.TitleArea.IsBold = True
# Show data labels for data points
for cs in chart.Series:
cs.Format.Options.IsVaryColor = True
cs.DataPoints.DefaultDataPoint.DataLabels.HasValue = True
# Set legend position
chart.Legend.Position = LegendPositionType.Top
# Save the result file
workbook.SaveToFile("/output/StackedBarChart.xlsx", ExcelVersion.Version2016)
workbook.Dispose()
Result Preview:

The Conclusion
By following these steps, you can effortlessly create both standard and stacked bar charts in Excel using Python and Spire.XLS. Automating chart creation not only saves time but also ensures consistency across your reports, making your data more insightful and visually appealing. Whether you’re summarizing sales, tracking performance, or comparing categories, these charts help turn raw numbers into clear, actionable insights—empowering you to present your data like a pro.