PowerPoint files

Hi - Inspired by the action to create word documents and some help from GPT :), I have created the same except it makes a PowerPoint file for presentations. I’d like to share it with the community at large but I’m not sure how to do this, the action just seems to be in my personal section?

Thanks!

1 Like

Hi Lachb, I’m very interested in this action! Please keep us posted as to when it’s available. Thank you in advance for your willingness to share :slight_smile:

I am also interested. Can you share the steps you took in a document? Thanks in advance for your help.

Sure, I will share it here. Functions and packages are in the image and the action code is below, there’s a section where you can apply basic text formatting for the heading and slide text.

import requests
import markdown
from bs4 import BeautifulSoup
from pptx import Presentation
from pptx.util import Pt
from pptx.dml.color import RGBColor

def export_to_powerpoint(markdown_input: str, file_name: str):
“”"
Allows the user to create a PowerPoint file

Args:
    markdown_input (string): Markdown that will be turned into the powerpoint document
    file_name (string): Name of the document
"""

# Insert your PYTHON code below. You can access environment variables using os.environ[].
# Currently, only the requests library is supported, but more libraries will be available soon.
# Use print statements or return values to display results to the user.
# If you save a png, pdf, csv, jpg, webp, gif, or html file in the root directory, it will be automatically displayed to the user.
# You do not have to call this function as the bot will automatically call and fill in the parameters.

 # Ensure the file name ends with .pptx
if not file_name.endswith('.pptx'):
    file_name += '.pptx'

# Convert Markdown to HTML
html_content = markdown.markdown(markdown_input)
# Parse HTML using BeautifulSoup
soup = BeautifulSoup(html_content, "html.parser")

# Create a new PowerPoint presentation
presentation = Presentation()
slide = None
content_lines = []

# Define styles here
header_font = "Arial"
header_size = 32
header_color = "FF0000"  # Red in hex

body_font = "Arial"
body_size = 24
body_color = "000000"  # Black in hex

for element in soup.children:
    if element.name in ["h1", "h2", "h3"]:
        # If there's existing content, create a slide with it
        if slide is not None:
            content_placeholder.text = "\n".join(content_lines)
            # Apply body text formatting
            for paragraph in content_placeholder.text_frame.paragraphs:
                for run in paragraph.runs:
                    run.font.name = body_font
                    run.font.size = Pt(body_size)
                    run.font.color.rgb = RGBColor.from_string(body_color)

        # Start a new slide
        slide_layout = presentation.slide_layouts[1]
        slide = presentation.slides.add_slide(slide_layout)
        title_placeholder = slide.shapes.title
        content_placeholder = slide.placeholders[1]

        # Set the new slide's title and format it
        title_placeholder.text = element.get_text()
        for paragraph in title_placeholder.text_frame.paragraphs:
            for run in paragraph.runs:
                run.font.name = header_font
                run.font.size = Pt(header_size)
                run.font.color.rgb = RGBColor.from_string(header_color)

        content_lines = []  # Reset content lines for the new slide

    elif element.name == "p":
        content_lines.append(element.get_text())
    elif element.name == "ul":
        content_lines.extend(li.get_text() for li in element.find_all("li"))
    elif element.name == "ol":
        content_lines.extend(f"{i+1}. {li.get_text()}" for i, li in enumerate(element.find_all("li")))

# Create the last slide with any remaining content and format it
if slide is not None and content_lines:
    content_placeholder.text = "\n".join(content_lines)
    for paragraph in content_placeholder.text_frame.paragraphs:
        for run in paragraph.runs:
            run.font.name = body_font
            run.font.size = Pt(body_size)
            run.font.color.rgb = RGBColor.from_string(body_color)

# Save the PowerPoint presentation
presentation.save(file_name)