If you want to create excel charts with python, openpyxl is there to help you with your Python excel charts. Openpyxl module supports creating all major type of charts like bar chart, line chart, scatter and pie chart with the data in the cells of a given sheet. To create excel charts with openpyxl, follow these simple steps.
If you want to Read, Write and Manipulate(Copy, cut, paste, delete or search for an item etc) Excel files in Python with simple and practical examples I will suggest you to see this simple and to the point Excel Openpyxl Course with examples about how to deal with MS Excel files in Python. This video course teaches efficiently how to manipulate excel files and automate tasks.
Everything you do in Microsoft Excel, can be automated with Python. So why not use the power of Python and make your life easy. You can make intelligent and thinking Excel sheets, bringing the power of logic and thinking of Python to Excel which is usually static, hence bringing flexibility in Excel and a number of opportunities.
When we want to create reference object in python excel charts using openpyxl, we have to call a specific function, openpyxl.charts.Reference(), this function needs three arguments,
>>> import openpyxl
>>> chartwb = openpyxl.Workbook()
>>> sheet = chartwb.get_active_sheet()
>>> for x in range(1, 10): # creating temp data in column A
sheet['A' + str(i)] = i
>>> referenceobj = openpyxl.charts.Reference(sheet, (1, 1), (10, 1))
>>> serObj = openpyxl.charts.Series(referenceObj, title='Chart Series')
>>> chartObject = openpyxl.charts.BarChart()
>>> chartObject.drawing.top = 50 # set the position
>>> chartObject.drawing.left = 100
>>> chartObject.drawing.width = 300 # set the size
>>> chartObject.drawing.height = 200
The above code implements the steps mentioned above. Similarly with the help of openpyxl, we can also create pie charts, line charts and pie charts.
Openpyxl.charts.LineChart( ) will create a line chart.
Openpyxl.charts.ScatterChart( ) will create a scatter chart.
Openpyxl.charts.PieChart( ) will create a Pie chart.
Hence with the basic technique of creating reference objects and openpyxl functions, we can create most type of charts in our excel file using python, and ofcourse without using excel software.