Last modified: Aug 23, 2026
Convert HTML to PPTX with Python
Turning web content into a PowerPoint presentation can save you hours of manual work. If you have product descriptions, blog posts, or any HTML data, you can automate slide creation. This guide will show you how to handle this conversion using Python.
We will use the python-pptx library. It is a powerful tool for creating and updating PowerPoint files. While it does not read HTML directly, we can parse HTML and map its elements to slide objects. This approach gives you full control over the final design.
Why Convert HTML to PPTX?
Many businesses need to generate reports from web dashboards. Marketing teams often turn blog posts into slide decks. Manually copying text is slow and prone to errors. Automating this process ensures consistency and frees up your time.
Python makes this task straightforward. You can extract headings, paragraphs, lists, and even images from HTML. Then, you can place these elements onto slides with just a few lines of code. The result is a professional-looking presentation generated in seconds.
Setting Up Your Environment
First, install the necessary libraries. You will need python-pptx for slide creation and beautifulsoup4 for HTML parsing. Open your terminal and run the following command:
pip install python-pptx beautifulsoup4
This command installs both libraries. Ensure you have Python 3.6 or later for full compatibility. Once installed, you can start writing your conversion script.
Basic HTML Parsing with BeautifulSoup
BeautifulSoup helps us navigate the HTML structure easily. We can find all headings, paragraphs, and lists. Let's start by reading an HTML string and extracting the main content.
from bs4 import BeautifulSoup
html_content = """
<html>
<body>
<h1>Project Update</h1>
<p>We have made significant progress this week.</p>
<ul>
<li>Completed the API integration</li>
<li>Fixed the login bug</li>
</ul>
<h2>Next Steps</h2>
<p>Focus on testing and deployment.</p>
</body>
</html>
"""
soup = BeautifulSoup(html_content, 'html.parser')
# Find all top-level elements
for element in soup.body.children:
if element.name:
print(element.name, ':', element.get_text(strip=True))
This code prints the tag name and text content for each child of the body. It is a simple way to see what we are working with. Now, let's use this to build a presentation.
Creating Slides from HTML Elements
We will create a new presentation and add a slide for each major section. We can use the title slide layout for headings and the content layout for paragraphs and lists. The key is to map HTML tags to slide placeholders.
from pptx import Presentation
from pptx.util import Inches
from bs4 import BeautifulSoup
# Create a presentation object
prs = Presentation()
# Use the first layout (Title Slide) and second layout (Title and Content)
title_slide_layout = prs.slide_layouts[0]
content_slide_layout = prs.slide_layouts[1]
# Our HTML content
html_content = """
<html>
<body>
<h1>Project Update</h1>
<p>We have made significant progress this week.</p>
<ul>
<li>Completed the API integration</li>
<li>Fixed the login bug</li>
</ul>
<h2>Next Steps</h2>
<p>Focus on testing and deployment.</p>
</body>
</html>
"""
soup = BeautifulSoup(html_content, 'html.parser')
# Track if we have created the first slide
first_slide = True
for element in soup.body.children:
if not element.name:
continue
if element.name in ['h1', 'h2', 'h3']:
# Add a new slide for each heading
slide = prs.slides.add_slide(title_slide_layout if first_slide else content_slide_layout)
slide.shapes.title.text = element.get_text(strip=True)
first_slide = False
elif element.name == 'p':
# Add a content slide if no heading exists, else add to the last slide
if not first_slide:
slide = prs.slides.add_slide(content_slide_layout)
slide.shapes.title.text = "Details"
slide.placeholders[1].text = element.get_text(strip=True)
elif element.name == 'ul':
# Add list items to the content placeholder
if not first_slide:
slide = prs.slides.add_slide(content_slide_layout)
slide.shapes.title.text = "Key Points"
for li in element.find_all('li'):
slide.placeholders[1].text += li.get_text(strip=True) + "\n"
# Save the presentation
prs.save('output.pptx')
print("Presentation created successfully!")
This script iterates through the HTML body. For each heading, it creates a new slide. For paragraphs and lists, it adds them to a content slide. Notice how we handle the first slide differently to use the title layout.
Output
Presentation created successfully!
You will now have an output.pptx file in your directory. It will contain slides for each heading and bullet points for the list. This is a basic example, but you can expand it to handle images and tables.
Handling Images in HTML
Images are a common part of web content. To include them, we need to download the image and add it to the slide. This requires an extra step using the requests library. Let's see how to handle an image tag.
import requests
from pptx import Presentation
from pptx.util import Inches
from bs4 import BeautifulSoup
# HTML with an image
html_content = """
<html>
<body>
<h1>Team Photo</h1>
<img src="https://example.com/team.jpg" alt="Team">
</body>
</html>
"""
soup = BeautifulSoup(html_content, 'html.parser')
prs = Presentation()
slide_layout = prs.slide_layouts[5] # Blank layout
slide = prs.slides.add_slide(slide_layout)
# Add title
title = slide.shapes.title
title.text = soup.find('h1').get_text()
# Find image tag
img_tag = soup.find('img')
if img_tag:
img_url = img_tag['src']
response = requests.get(img_url)
with open('temp_img.png', 'wb') as f:
f.write(response.content)
slide.shapes.add_picture('temp_img.png', Inches(1), Inches(1), width=Inches(6))
prs.save('with_image.pptx')
print("Image slide created")
This code downloads the image from the URL and places it on the slide. Remember to handle missing images gracefully with a try-except block to avoid crashes.
Using Templates for Better Design
Creating slides from scratch gives you basic layouts. For a more polished look, you can use a template. The python-pptx library allows you to load an existing presentation and modify it. This is especially useful when you need to match your company's branding.
If you want to learn more about this, check out our guide on using templates for easy slides. It will show you how to work with pre-designed layouts and placeholders effectively.
Adding Pictures to Your Slides
When your HTML contains multiple images, you might want to position them precisely. The python-pptx library gives you full control over image placement. You can set the left, top, height, and width of any picture.
For a detailed walkthrough on adding and manipulating images, refer to our Python PPTX add picture guide. It covers advanced techniques like cropping and layering.
Advanced Formatting Options
You can format text within slides. For example, you can make text bold or italic based on HTML tags. Use the font property of a text frame. Here is a quick example of making text bold.
from pptx import Presentation
from pptx.util import Pt
prs = Presentation()
slide = prs.slides.add_slide(prs.slide_layouts[1])
slide.shapes.title.text = "Bold Text Example"
body = slide.placeholders[1].text_frame
p = body.paragraphs[0]
run = p.add_run()
run.text = "This is bold text"
run.font.bold = True
run.font.size = Pt(18)
prs.save('formatted.pptx')
This code snippet shows how to apply bold formatting to a run of text. You can combine this with your HTML parser to apply styles from tags like <strong> or <em>.
Handling Tables from HTML
Tables are another common element. Converting an HTML table to a PowerPoint table is possible but requires a bit more work. You need to create a table shape and populate it cell by cell.
from pptx import Presentation
from pptx.util import Inches
from bs4 import BeautifulSoup
html_table = """
<table>
<tr><th>Name</th><th>Age</th></tr>
<tr><td>Alice</td><td>30</td></tr>
<tr><td>Bob</td><td>25</td></tr>
</table>
"""
soup = BeautifulSoup(html_table, 'html.parser')
rows = soup.find_all('tr')
prs = Presentation()
slide = prs.slides.add_slide(prs.slide_layouts[5]) # Blank layout
# Determine table size
num_rows = len(rows)
num_cols = len(rows[0].find_all(['th', 'td']))
table_shape = slide.shapes.add_table(num_rows, num_cols, Inches(1), Inches(1), Inches(6), Inches(2))
table = table_shape.table
for i, row in enumerate(rows):
cells = row.find_all(['th', 'td'])
for j, cell in enumerate(cells):
table.cell(i, j).text = cell.get_text(strip=True)
prs.save('table_slide.pptx')
print("Table slide created")
This script creates a table on a slide and fills it with data from the HTML. You must ensure the number of columns is consistent across all rows to avoid errors.
Conclusion
Converting HTML to PPTX with Python is a powerful automation technique. With beautifulsoup4 and python-pptx, you can transform web content into visually appealing presentations. We covered headings, paragraphs, lists, images, and tables. The key is to map HTML elements to slide objects systematically.
Start with simple text conversion, then gradually add images and tables. Always test your script with different HTML structures to ensure robustness. This approach can save you countless hours when creating recurring reports or presentations from web data.
Remember to explore the full capabilities of python-pptx for styling and animations. Combine this with your HTML parsing logic to create dynamic and interactive presentations. Happy coding!