File size: 2,227 Bytes
de061eb
 
41af1c8
de061eb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41af1c8
 
 
 
 
de061eb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41af1c8
 
 
 
de061eb
 
 
 
 
 
 
41af1c8
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import json5
import pptx
from pptx.dml.color import RGBColor


def generate_powerpoint_presentation(structured_contents: str, output_file_name: str):
    """
    Create and save a PowerPoint presentation file containing the contents.

    :param structured_contents: The presentation contents in "JSON" format (may contain trailing commas)
    :param output_file_name: The name of the PPTX file to save as
    """

    # The structured "JSON" contains trailing commas, so using json5
    json_data = json5.loads(structured_contents)

    presentation = pptx.Presentation()

    # The title slide
    title_slide_layout = presentation.slide_layouts[0]
    slide = presentation.slides.add_slide(title_slide_layout)
    title = slide.shapes.title
    subtitle = slide.placeholders[1]
    title.text = json_data['presentation_title']
    subtitle.text = 'Generated by Slides Wizard AI :)'

    background = slide.background
    background.fill.solid()
    background.fill.fore_color.rgb = RGBColor.from_string('C0C0C0')  # Silver
    title.text_frame.paragraphs[0].font.color.rgb = RGBColor(0, 0, 128)  # Navy blue

    # Add content in a loop
    for a_slide in json_data['slides']:
        bullet_slide_layout = presentation.slide_layouts[1]
        slide = presentation.slides.add_slide(bullet_slide_layout)
        shapes = slide.shapes

        title_shape = shapes.title
        body_shape = shapes.placeholders[1]
        title_shape.text = a_slide['slide_heading']
        text_frame = body_shape.text_frame

        for an_item in a_slide['slide_contents']:
            paragraph = text_frame.add_paragraph()
            paragraph.text = an_item
            paragraph.level = 0

        background = slide.background
        background.fill.gradient()
        background.fill.gradient_angle = -225.0

    # The thank-you slide
    last_slide_layout = presentation.slide_layouts[0]
    slide = presentation.slides.add_slide(last_slide_layout)
    title = slide.shapes.title
    title.text = 'Thank you!'

    presentation.save(output_file_name)


if __name__ == '__main__':
    generate_powerpoint_presentation(
        json5.loads(open('examples/example_02_structured_output.json', 'r').read()),
        'test.pptx'
    )