To achieve the goal of extracting slide headings, text content, and images from a PowerPoint file (PPT or PPTX), we can use the python-pptx library for reading the PowerPoint file and extracting its content. Additionally, the base64 library can help convert images to a format suitable for embedding in HTML.
First, ensure that the python-pptx library is installed:
pip install python-pptx
Here's a complete Python program to extract slide headings, text content, and images from a PPT or PPTX file and return them in HTML format:
from pptx import Presentation
from pptx.enum.shapes import MSO_SHAPE_TYPE
import base64
import os
from io import BytesIO
def extract_content_from_ppt(file_path):
presentation = Presentation(file_path)
slides_content = []
for slide in presentation.slides:
slide_data = {
'heading': "",
'text_content': "",
'images': []
}
for shape in slide.shapes:
if not shape.has_text_frame:
continue
heading = shape.text_frame.paragraphs[0].text.strip() if shape.text_frame.paragraphs else ""
text_content = "\n".join([para.text.strip() for para in shape.text_frame.paragraphs])
if shape.shape_type == MSO_SHAPE_TYPE.PICTURE:
image_stream = shape.image.blob
image_base64 = base64.b64encode(image_stream).decode('utf-8')
slide_data['images'].append(image_base64)
slide_data['heading'] = heading
slide_data['text_content'] += text_content + "\n"
slides_content.append(slide_data)
return slides_content
def generate_html(slides_content):
html_content = """
<!DOCTYPE html>
<html>
<head>
<title>Presentation Content</title>
</head>
<body>
"""
for index, slide in enumerate(slides_content):
html_content += f"<h2>Slide {index + 1}</h2>\n"
html_content += f"<h3>{slide['heading']}</h3>\n"
html_content += f"<p>{slide['text_content'].replace('\n', '<br>')}</p>\n"
for image_base64 in slide['images']:
html_content += f'<img src="data:image/png;base64,{image_base64}" alt="slide image">\n'
html_content += """
</body>
</html>
"""
return html_content
def save_html(html_content, output_file):
with open(output_file, 'w', encoding='utf-8') as f:
f.write(html_content)
def main():
input_file = input("Enter the path to the PPT or PPTX file: ")
output_file = "presentation.html"
slides_content = extract_content_from_ppt(input_file)
html_content = generate_html(slides_content)
save_html(html_content, output_file)
print(f"HTML content saved to {output_file}")
if __name__ == "__main__":
main()
Explanation:
-
Extracting Content:
- The
extract_content_from_ppt function takes the file path of the PPT or PPTX file and opens it using the Presentation class.
- It iterates through each slide and each shape (text, image) on the slide.
- For text shapes, it extracts the text content.
- For image shapes, it converts the image to a Base64 string.
-
Generating HTML:
- The
generate_html function creates an HTML string from the extracted content. Each slide's heading, text content, and images are formatted accordingly.
-
Saving HTML:
- The
save_html function writes the generated HTML content to a file.
-
Main Function:
- The
main function handles user input and calls the necessary functions to extract content from the input file, generate HTML, and save it to an output file.
Make sure to have the python-pptx library installed and run the code. This will generate an presentation.html file containing the slides content in HTML format.
This solution extracts text from the first paragraph of each text frame as the heading, concatenates all the text from the paragraphs, and converts images to base64 format for embedding. Adjustments might be needed depending on the specific structure of your slides.