Skip to content
Home » openpyxl Part 21 – 3D Charts

openpyxl Part 21 – 3D Charts

Spread the love

In the previous parts of the series we created a bar chart, a line chart and a pie chart. There are lots of other chart types, so if you’re interested, you should check it out in the documentation. In this article, which is the last in my openpyxl series, I’d like to demonstrate an example of a 3D chart. There are several types of 3D charts like 3D line chart, 3D bar chart, 3D area chart or 3D pie chart. I’m going to discuss just one of them, the 3D Line chart. I’ll be using the wb4.xlsx file where we added a 2D line chart. It contains the following random data:

20152016201720182019
tables13431113113917611827
closets15721492163710671824
chairs10971752199115121587

The data is random, so your data is different if you were following along. Here’s the line chart we generated for the data:

3d charts

We’ll place the 3D chart next to the 2D one. So, let’s load our workbook:

>>> from openpyxl import load_workbook
>>> workbook = load_workbook(filename="wb4.xlsx")
>>> sheet = workbook.active

Let’s import the classes we need:

>>> from openpyxl.chart import LineChart3D, Reference

Let’s create the chart and the references:

>>> chart = LineChart3D()
>>> data = Reference(worksheet=sheet, min_row=1, max_row=4, min_col=2, max_col=6)
>>> titles = Reference(worksheet=sheet, min_row=2, max_row=4, min_col=1)

Now we can set the title, add the data to the chart, set the categories and the titles of the X and Y axes:

>>> chart.title = 'Furniture Sales'
>>> chart.add_data(data, titles_from_data=True)
>>> chart.set_categories(titles)
>>> chart.x_axis.title = 'Products'
>>> chart.y_axis.title = 'Sales'

Your Panda3D Magazine

Make Awesome Games and Other 3D Apps

with Panda3D and Blender using Python.

Cool stuff, easy to follow articles.

Get the magazine here (PDF).

Finally, let’s add the chart and save the file:

>>> sheet.add_chart(chart, 'N10')
>>> workbook.save("wb4c.xlsx")

If you now open the file, you will see the two charts, 2D and 3D, side by side:

3d charts

That’s it as far as adding charts to spreadsheets is concerned in our openpyxl series. And that’s it as far as the whole openpyxl series is concerned. There’s lot of stuff that I didn’t even touch upon, so feel free to explore the openpyxl module.

Python Jumpstart Course

Learn the basics of Python, including OOP.

with lots of exercises, easy to follow

The course is available on Udemy.

Blender Jumpstart Course

Learn the basics of 3D modeling in Blender.

step-by-step, easy to follow, visually rich

The course is available on Udemy and on Skillshare.


Spread the love

Leave a Reply