How to create PowerPoint Presentation with Python

Before we start, please make sure to install python-pptx library. With python-pptx library, you can create/edit (add a slide, text, paragraph, table, and much more) a PowerPoint (.pptx) file, but remember this won’t work on MS Office 2003.

Installation: Open the command prompt on your system and write the given below command:

pip install python-pptx

From this post, you will learn:

  • How to create a PowerPoint presentation file
  • Add a slide to PowerPoint presentation
  • Add an Image to Slide
  • Add a Text Box to slide and add some text/paragraph to Text Box

Before we go to code, first let see how we can structure the PowerPoint presentation and create some classes or objects as shown in below picture:

PowerPoint_Presentation_Structure

From the picture above, in a PPTX presentation file, we can have many slides; and in a slide, we can have many Text Box (with many paragraphs) and many images. So, for now, we have 4 classes (CPptx, CSlide, CTextBox, and CParagraph) but you can extend or modify the class as needed, below is the description of each class:

  • CPptx, this class will initialize the presentation instance and initiate slide width/height; Add blank slide to presentation; and save the presentation to pptx file
  • CSlide: this class will wrap slide object; and you can add image or text box in this slide
  • CTextBox: this class will wrap all the text box properties and text frame; and you can add paragraph to this text box
  • CParagraph: this class will wrap all the paragraph and font properties

Below is the code implementation of the above structure:

from pptx import Presentation
from pptx.util import Inches
from pptx.util import Pt
from pptx.dml.color import RGBColor


class CParagraph: 
    def __init__(self, paragraph): 
        self.paragraph = paragraph
        self.font = paragraph.font
        
class CTextBox: 
    def __init__(self, textbox):
        self.textbox = textbox
        self.textframe = textbox.text_frame
        self.isDefaultParagraph = True
        
    def addParagraph(self): 
        if self.isDefaultParagraph: 
            self.isDefaultParagraph = False
            return CParagraph(self.textframe.paragraphs[0])
        else: 
            return CParagraph(self.textframe.add_paragraph())
    

class CSlide: 
    def __init__(self, slide):
        self.slide = slide
        
    #image: image can be either a path to a file (a string) or a file-like object (ByteIO)
    def addImage(self, image, left, top, width=None, height=None, border_color=RGBColor(166, 166, 166)):
        picture = self.slide.shapes.add_picture(image, Inches(left), Inches(top), width, height)
        picture.line.color.rgb = border_color
        
    def addTextBox(self, left, top, width, height): 
        return CTextBox(self.slide.shapes.add_textbox(left, top, width, height))

class CPptx: 
    def __init__(self): 
        self.presentation = Presentation()
        self.presentation.slide_width = Inches(13.333)
        self.presentation.slide_height = Inches(7.5)
        
    def addBlankSlide(self): 
        blank_slide_layout = self.presentation.slide_layouts[6]
        return CSlide(self.presentation.slides.add_slide(blank_slide_layout))
    
    def save(self, filename): 
        self.presentation.save(filename)

Let’s see the detailed description of each class and its functions:

CPptx class

Below is the description of each function:

  • __init__(self): construct function, to create presentation instance and initialize the slide width and height
  • addBlankSlide(self): this function will return blank slide and wrapped with CSlide class. If you see from your new pptx in PowerPoint you will see that Blank slide at number 6:
PowerPoint_Blank_Slide
  • save (self, filename): this function will save Presentation object to PPTX file. filename is a path to a file (a string)

CSlide Class

Below is the description of each function:

  • __init__(self, slide): construct function, to wrap slide object
  • addImage(self, image, left, top, width=None, height=None, border_color=RGBColor(166, 166, 166)): image can be either a path to a file (a string) or a file-like object. The picture is positioned with its top-left corner at (top, left). If width and height are both None, the native size of the image is used. If only one of width or height is used, the unspecified dimension is calculated to preserve the aspect ratio of the image. If both are specified, the picture is stretched to fit, without regard to its native aspect ratio. And default border color of the image is RGBColor(166, 166, 166)
  • addTextBox(self, left, top, width, height): this function will add text box object to a slide with specified size, located at the specified position on the slide. And this text box will be wrapped by CTextBox class.

CTextBox class

Below is the description of each function:

  • __init__(self, textbox): construct function, to wrap text box and text frame objects
  • addParagraph(self): this function will add a paragraph to a text box and remember every text box by default will add a paragraph to its object that’s why we call self.textframe.paragraphs[0] for the first time and self.textframe.add_paragraph() for the next.

CParagraph class

Below is the description of each function:

  • __init__(self, paragraph): construct function, to wrap paragraph and font objects

Test the code

To test all classes that we create above, we’ll create a presentation file, add a slide to the presentation; And add an image and text box (as a header) to the slide:

Nb. On the below code you have an image (out1.png) in your working directory and then save the presentation object with the name ‘pptx_text.pptx’ to your working directory.

#Create Pptx Object
pptx = CPptx()

#Add slide1 and Add Image in the slide1
slide1 = pptx.addBlankSlide()
slide1.addImage('out1.png', 2.44, 0.68, width=None, height=Inches(2.22))

#Text Box properties
fontSizeParagraph1 = Pt(22)
fontSizeParagraph2 = Pt(18)
lineSpacing = Pt(1)
width = pptx.presentation.slide_width
height = fontSizeParagraph1 + fontSizeParagraph2 + 5*lineSpacing

#Add Text Box to Slide1
header = slide1.addTextBox(Inches(0), Inches(0), width, height)
header.textframe.word_wrap = True
header.textframe.margin_left = Inches(0.3)
header.textframe.margin_right = Inches(0.3)
header.textframe.margin_top = Inches(0.05)
header.textframe.margin_bottom = Inches(0.05)

#Add paragraph1 to Text Box
paragraph1 = header.addParagraph()
paragraph1.paragraph.text = '4G Low Revenue Site Optimization'
paragraph1.font.bold = True
paragraph1.font.color.rgb = RGBColor(255, 0, 0)
paragraph1.font_size = fontSizeParagraph1

#Add paragraph2 to Text Box
paragraph2 = header.addParagraph()
paragraph2.paragraph.text = 'Site PADANGBAI_BALI_APR_ST (20PON094) - Huawei FP'
paragraph2.font.bold = True
paragraph2.font.size = fontSizeParagraph2

#Save pptx to file
pptx.save('pptx_test.pptx')

The Output:

the_result

Conclusion

In this post, we create 4 classes (CPptx, CSlide, CTextBox, and CParagraph) to create a presentation object, add a slide to a presentation object, add a text box to a slide, add an image to a slide, and save the presentation object to PowerPoint file (.pptx). You can freely modify all the classes above or add another class to wrap another object (like table, etc).

For the next post, we’ll learn how to process excel data to chart and directly add it to PowerPoint presentation. Here is the post.

Leave a comment