Skip to main content
Practice

Visualize Project Status with PowerPoint

Now we will use Python to visualize the project status data retrieved from Excel into PowerPoint slides.

Project NameProgress (%)Start DateEnd DateIssues
Website Redesign802024-07-012024-12-31Development delays
Marketing Campaign502024-08-152024-11-15None
New Product Launch302024-09-012025-01-31Production issues

We will use python-pptx to convert the Excel data into a table and visualize the progress of each project with a bar chart.


Main Code Explanation

The practice code generally proceeds in the following order.


1. Define the Table's Position and Size

Define the table's position and size in Inches
rows, cols = len(data), len(data[0])

left = Inches(0.5)
top = Inches(0.5)
width = Inches(9.0)
height = Inches(2.0)

Using the Inches class from the python-pptx library, we define the position and size of the table.

We set the left position of the table to 0.5 inch and the top position to 0.5 inch, and set the width and height of the table to 9.0 inches and 2.0 inches, respectively.


2. Adding the Table to the Slide

Create and add a table to the slide
# add_table to add a table to the slide
table = slide.shapes.add_table(rows, cols, left, top, width, height).table

We use the slide.shapes.add_table method to add a table to the slide.


3. Populate the Table with Data

Insert Excel data into the PowerPoint table
# Populate the table with Excel data
for row_idx, row_data in enumerate(data):
# Iterate through each row's data and insert it into the cell
for col_idx, cell_value in enumerate(row_data):
# Insert data into each cell
table.cell(row_idx, col_idx).text = str(cell_value)

Using the enumerate function, we iterate through each row of data and insert the data into each cell of the table.

We use table.cell(row_idx, col_idx).text to insert data into each cell.


4. Prepare Data for Chart Creation

Prepare data for chart creation
# Prepare data for chart creation (Project Name and Progress %)
chart_data = CategoryChartData()

# Exclude the first row (header)
chart_data.categories = [row[0] for row in data[1:]]

# Add Progress (%) data
chart_data.add_series('Progress (%)', (row[1] for row in data[1:]))

We prepare data for chart creation.

Using the CategoryChartData class, we generate chart data and add project names to chart_data.categories and Progress (%) data to chart_data.add_series.


5. Adding the Chart to the Slide

Create and add the chart to the slide
x, y, cx, cy = Inches(0.5), Inches(3.0), Inches(5.0), Inches(3.0)

# Add the chart (clustered column chart)
chart = slide.shapes.add_chart(
XL_CHART_TYPE.COLUMN_CLUSTERED, x, y, cx, cy, chart_data
).chart

Using x, y, cx, cy, we set the position and size of the chart, and we use the slide.shapes.add_chart method to add the chart to the slide.

XL_CHART_TYPE is an enumeration constant specifying the type of chart, and COLUMN_CLUSTERED indicates a clustered column chart.

Want to learn more?

Join CodeFriends Plus membership or enroll in a course to start your journey.