Follow this article to create a column chart in Word using Python. It has the details to set the IDE, a list of steps, and a sample code showing how to make bar charts in Word using Python. The sample code shares all the necessary classes and methods required for creating and customizing a chart.
Steps to Create a Bar Chart in Word
- Set the environment to use Aspose.Words for Python via .NET to add a chart
- Create a Word file using the Document class and create a DocumentBuilder object
- Call the insert_chart() method using the ChartType COLUMN and size
- Access the series collection of the chart and make it empty
- Define the categories and add data for each category
- Save the Word file with the chart in it
These steps explain how to make a column chart in Word using Python. Create a document with a DocumentBuilder object, create a chart object, access its series collection, and make it empty. Define the collection of data categories and add a data series for each category to fill the chart.
Code for Creating a Bar Graph in Word using Python
import aspose.words as aw | |
# Load the license | |
wordLic = aw.License() | |
wordLic.set_license("license.lic") | |
doc = aw.Document() | |
builder = aw.DocumentBuilder(doc) | |
# Add column chart | |
shape = builder.insert_chart(aw.drawing.charts.ChartType.COLUMN, 400, 250) | |
# Access the chart object | |
chart = shape.chart | |
# Access the chart series | |
series = chart.series | |
# Clear the series | |
series.clear() | |
# Create categories | |
categories = [ "Data Category 1", "Data Category 2" ] | |
# Add series data | |
series.add("Albania", categories, [3, 4]) | |
series.add("Andorra", categories, [5, 2]) | |
series.add("Armenia", categories, [3, 7]) | |
series.add("Austria", categories, [2, 3]) | |
series.add("Azerbaijan", categories, [5, 5]) | |
doc.save("output.docx") | |
print ("Charts added successfully") |
This sample code assists in working with Microsoft Word bar chart using Python. The ChartType enumerator contains a long list of charts that you can create including AREA_STACKED, AREA_3D_PERCENT_STACKED, BAR_3D, DOUGHNUT, PIE, RADAR, STOCK, and HISTOGRAM. The chart series data should be of the same size in the array and should not be empty.
This article taught us how to create a bar graph. If you want to insert bullets in a Word file, refer to the article Insert bullets in Word using Python.