Creating and updating PowerPoint Presentations in Python using python – pptx

18 Aug, 2020
python-pptx is library used to create/edit a PowerPoint (.pptx) files. This won’t work on MS office 2003 and previous versions.  We can add shapes, paragraphs, texts and slides and much more thing using this library.
Installation: Open the command prompt on your system and write given below command:
pip install python-pptx

Let’s see some of its usage:
Example 1: Creating new PowerPoint file with title and subtitle slide.
Python3
# import Presentation class# from pptx libraryfrom pptx import Presentation       # Creating presentation objectroot = Presentation()  # Creating slide layoutfirst_slide_layout = root.slide_layouts[0]   """ Ref for slide types: 0 ->  title and subtitle1 ->  title and content2 ->  section header3 ->  two content4 ->  Comparison5 ->  Title only 6 ->  Blank7 ->  Content with caption8 ->  Pic with caption"""  # Creating slide object to add # in ppt i.e. Attaching slides # with Presentation i.e. pptslide = root.slides.add_slide(first_slide_layout)  # Adding title and subtitle in # slide i.e. first page of slide slide.shapes.title.text = " Created By python-pptx"  # We have different formats of # subtitles in ppts, for simple# subtitle this method should # implemented, you can change# 0 to 1 for different designslide.placeholders[1].text = " This is 2nd way"  # Saving fileroot.save("Output.pptx")  print("done")
Output:
Example 2: Adding Text-Box in PowerPoint.
Python3
# import required thingsfrom pptx import Presentation from pptx.util import Inches, Pt  # Creating Objectppt = Presentation()   # To create blank slide layout# We have to use 6 as an argument# of slide_layouts  blank_slide_layout = ppt.slide_layouts[6]   # Attaching slide obj to slideslide = ppt.slides.add_slide(blank_slide_layout)  # For adjusting the  Margins in inches left = top = width = height = Inches(1)   # creating textBoxtxBox = slide.shapes.add_textbox(left, top,                                 width, height)  # creating textFramestf = txBox.text_frametf.text = "This is text inside a textbox"  # adding Paragraphsp = tf.add_paragraph()   # adding textp.text = "This is a second paragraph that's bold and italic"   # font p.font.bold = Truep.font.italic = True  p = tf.add_paragraph()p.text = "This is a third paragraph that's big " p.font.size = Pt(40)  # save fileppt.save('test_2.pptx')  print("done")
Output:
Example 3: PowerPoint (.pptx) file to Text (.txt) file conversion.
Python3
# import Presentation class# from pptx libraryfrom pptx import Presentation  # creating an objectppt = Presentation("sample.pptx")  # open file in write modeFile_to_write_data = open("File_To_Extract_ppt.txt", "w")  # write text from powerpoint# file into .txt filefor slide in ppt.slides:     for shape in slide.shapes:         if not shape.has_text_frame:             continue         for paragraph in shape.text_frame.paragraphs:             for run in paragraph.runs:                 File_to_write_data.write(run.text)  # close the file               File_to_write_data.close()  print("Done")
Output:
Example 4: Inserting image into the PowerPoint file.
Python3
from pptx import Presentation from pptx.util import Inches   # Giving Image path img_path = 'bg_bg.png'   # Creating an Presentation objectppt = Presentation()   # Selecting blank slideblank_slide_layout = ppt.slide_layouts[6]   # Attaching slide to pptslide = ppt.slides.add_slide(blank_slide_layout)   # For marginsleft = top = Inches(1)   # adding imagespic = slide.shapes.add_picture(img_path,                               left, top)  left = Inches(1) height = Inches(1)   pic = slide.shapes.add_picture(img_path, left,                               top, height = height)# save fileppt.save('test_4.pptx')  print("Done")
Output:
Example 5: Adding Charts to the PowerPoint file.
Python3
# import required classes/functions/methodfrom pptx import Presentation from pptx.chart.data import CategoryChartData from pptx.enum.chart import XL_CHART_TYPE from pptx.util import Inches    # Create presentation objectppt = Presentation()   # Adding slide with specific layoutslide = ppt.slides.add_slide(ppt.slide_layouts[6])  # Define chart data # Creating object of chartchart_data = CategoryChartData()   # Adding categories to chartchart_data.categories = ['East', 'West', 'Midwest']    # Adding serieschart_data.add_series('Series 1',                       (int(input("Enter Value:")),                         int(input("Enter Value:")),                        int(input("Enter Value:"))))   x, y, cx, cy = Inches(2), Inches(2), Inches(6), Inches(4.5)   slide.shapes.add_chart( XL_CHART_TYPE.COLUMN_CLUSTERED, x,                       y, cx, cy, chart_data )  # Saving fileppt.save('chart-Tutorial.pptx')  print("done")
Output:
Example 6: Adding tables to the PowerPoint file.
Python3
# importingfrom pptx import Presentation from pptx.util import Inches  # create a Presentation objectppt = Presentation()   # Adding a blank slide in out pptslide = ppt.slides.add_slide(ppt.slide_layouts[6])  # Adjusting the width !  x, y, cx, cy = Inches(2), Inches(2), Inches(4), Inches(1.5)   # Adding tablesshape = slide.shapes.add_table(3, 4, x,                                y, cx, cy)  # Saving the fileppt.save("Tabel_Tutorial.pptx")  print("done")
Output:
python-modules
python-utility
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.