Visualize Project Status with PowerPoint
Now we will use Python to visualize the project status data retrieved from Excel into PowerPoint slides.
| Project Name | Progress (%) | Start Date | End Date | Issues |
|---|---|---|---|---|
| Website Redesign | 80 | 2024-07-01 | 2024-12-31 | Development delays |
| Marketing Campaign | 50 | 2024-08-15 | 2024-11-15 | None |
| New Product Launch | 30 | 2024-09-01 | 2025-01-31 | Production 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
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
# 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
# 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 (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
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.
Lecture
AI Tutor
Design
Upload
Notes
Favorites
Help
Code Editor
Execution Result
The document is empty.
Try running the code.