The Wayback Machine - https://web.archive.org/web/20240821211905/https://www.geeksforgeeks.org/creating-and-updating-powerpoint-presentations-in-python-using-python-pptx/
Open In App

Creating and updating PowerPoint Presentations in Python using python – pptx

Last Updated : 18 Aug, 2020
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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 library
from pptx import Presentation     
  
# Creating presentation object
root = Presentation()
  
# Creating slide layout
first_slide_layout = root.slide_layouts[0] 
  
""" Ref for slide types: 
0 ->  title and subtitle
1 ->  title and content
2 ->  section header
3 ->  two content
4 ->  Comparison
5 ->  Title only 
6 ->  Blank
7 ->  Content with caption
8 ->  Pic with caption
"""
  
# Creating slide object to add 
# in ppt i.e. Attaching slides 
# with Presentation i.e. ppt
slide = 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 design
slide.placeholders[1].text = " This is 2nd way"
  
# Saving file
root.save("Output.pptx")
  
print("done")


Output:

Adding title and subtitle to the powerpoint

Example 2: Adding Text-Box in PowerPoint.

Python3




# import required things
from pptx import Presentation 
from pptx.util import Inches, Pt
  
# Creating Object
ppt = 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 slide
slide = ppt.slides.add_slide(blank_slide_layout)
  
# For adjusting the  Margins in inches 
left = top = width = height = Inches(1) 
  
# creating textBox
txBox = slide.shapes.add_textbox(left, top,
                                 width, height)
  
# creating textFrames
tf = txBox.text_frame
tf.text = "This is text inside a textbox"
  
# adding Paragraphs
p = tf.add_paragraph() 
  
# adding text
p.text = "This is a second paragraph that's bold and italic" 
  
# font 
p.font.bold = True
p.font.italic = True
  
p = tf.add_paragraph()
p.text = "This is a third paragraph that's big " 
p.font.size = Pt(40)
  
# save file
ppt.save('test_2.pptx')
  
print("done")


Output:

Adding text box to the powerpoint

Example 3: PowerPoint (.pptx) file to Text (.txt) file conversion.

Python3




# import Presentation class
# from pptx library
from pptx import Presentation
  
# creating an object
ppt = Presentation("sample.pptx")
  
# open file in write mode
File_to_write_data = open("File_To_Extract_ppt.txt", "w")
  
# write text from powerpoint
# file into .txt file
for 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 object
ppt = Presentation() 
  
# Selecting blank slide
blank_slide_layout = ppt.slide_layouts[6] 
  
# Attaching slide to ppt
slide = ppt.slides.add_slide(blank_slide_layout) 
  
# For margins
left = top = Inches(1) 
  
# adding images
pic = 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 file
ppt.save('test_4.pptx')
  
print("Done")


Output:

Adding images to the powerpoint

Example 5: Adding Charts to the PowerPoint file.

Python3




# import required classes/functions/method
from pptx import Presentation 
from pptx.chart.data import CategoryChartData 
from pptx.enum.chart import XL_CHART_TYPE 
from pptx.util import Inches
  
  
# Create presentation object
ppt = Presentation() 
  
# Adding slide with specific layout
slide = ppt.slides.add_slide(ppt.slide_layouts[6])
  
# Define chart data 
# Creating object of chart
chart_data = CategoryChartData() 
  
# Adding categories to chart
chart_data.categories = ['East', 'West', 'Midwest']  
  
# Adding series
chart_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 file
ppt.save('chart-Tutorial.pptx')
  
print("done")


Output:

Adding charts to the powerpoint

Example 6: Adding tables to the PowerPoint file.

Python3




# importing
from pptx import Presentation 
from pptx.util import Inches
  
# create a Presentation object
ppt = Presentation() 
  
# Adding a blank slide in out ppt
slide = 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 tables
shape = slide.shapes.add_table(3, 4, x, 
                               y, cx, cy)
  
# Saving the file
ppt.save("Tabel_Tutorial.pptx")
  
print("done")


Output:

Adding table to the powerpoint



Similar Reads

How to create a bar chart and save in pptx using Python?
World Wide Web holds large amounts of data available that is consistently growing both in quantity and to a fine form. Python API allows us to collect data/information of interest from the World Wide Web. API is a very useful tool for data scientists, web developers, and even any casual person who wants to find and extract information programmatica
10 min read
Create Presentations Using RISE in Jupyter Notebook
RISE is an extension of Jupyter Notebook designed to revolutionize the way you create presentations. By seamlessly integrating with Jupyter Notebook, RISE allows you to transform your static code and data into dynamic, interactive presentations. In this guide, we'll explore how to install RISE, set up presentation slides, view and present your slid
3 min read
Retrieving And Updating Data Contained in Shelve in Python
In Python shelve you access the keys randomly. In order to access the keys randomly in python shelve we use open() function. This function works a lot like the file open() function in File handling. Syntax for open the file using Python shelve shelve.open(filename, flag='c' , writeback=True) In Order to access the keys randomly in shelve in Python,
3 min read
Merging and Updating Dictionary Operators in Python 3.9
Python 3.9 is still in development and scheduled to be released in October this year. On Feb 26, alpha 4 versions have been released by the development team. One of the latest features in Python 3.9 is the merge and update operators. There are various ways in which Dictionaries can be merged by the use of various functions and constructors in Pytho
3 min read
Updating dependencies in Python Poetry
Updating dependencies in Python Poetry is straightforward and efficient, thanks to its comprehensive command-line interface and powerful dependency resolution capabilities. Regularly updating your dependencies ensures that your project benefits from the latest features, bug fixes, and security improvements. Why Update Dependencies in Python Poetry?
3 min read
PyQt5 QCalendarWidget - Updating it
In this article we will see how we can update the QCalendarWidget. Updating means to reflect any changes that has occur at the back-end to the screen. Updating has no effect if it is disabled. In order to do this we will use update method with the QCalendarWidget object. Syntax : calendar.update() Argument : It takes no argument Return : It returns
1 min read
PyQt5 QCalendarWidget - Updating Micro focus
In this article we will see how we can update the micro focus QCalendarWidget. There are many focus i.e focus reason available like backtab, popup reason etc. Setting focus is the overloaded function, and we can reflect the change in micro focus by updating it. In order to do this we will use updateMicroFocus method with the QCalendarWidget object.
2 min read
PYGLET – Updating Sprite
In this article we will see how we can update the sprite in PYGLET module in python. Pyglet is easy to use but powerful library for developing visually rich GUI applications like games, multimedia etc. A window is a "heavyweight" object occupying operating system resources. Windows may appear as floating regions or can be set to fill an entire scre
3 min read
PYGLET – Manually Updating Texture of Media in Player
In this article we will see how we can update texture of media manually in player of PYGLET module in python. Pyglet is easy to use but powerful library for developing visually rich GUI applications like games, multimedia etc. A window is a "heavyweight" object occupying operating system resources. Windows may appear as floating regions or can be s
3 min read
Django ORM - Inserting, Updating & Deleting Data
Django lets us interact with its database models, i.e. add, delete, modify, and query objects, using a database-abstraction API called ORM(Object Relational Mapper). This article discusses all the functional operations we can perform using Django ORM. Prerequisite: Django models Django ORM Django's Object-Relational Mapping (ORM) system, a fundamen
3 min read
Dynamically Updating Plot In Matplotlib
This article shows how to dynamically update a Matplotlib (a data visualization library for Python programming language) plot as the data changes. It provides two methods to plot - first the API (useful for large programs and/or ones requiring deep control) and second the Pyplot interface (inspired by Matlab). In this article, we will show how to u
5 min read
Creating multiple user types and using proxy models in Python Django
In this article, we will understand the concept of the proxy model and the implementation of multiple user types depending on the requirements of your application in Django. What is the Proxy model in Django? An inherited proxy model can have a few extra functionalities, methods, and different behaviors than the parent model as defined by the creat
9 min read
Creating a JSON Response Using Django and Python
In Django, we can give responses to the front end in several ways. The simplest way to render a template or send a JSON response. JSON stands for JavaScript Object Notation and is widely used for data transfer between front-end and back-end and is supported by different languages and frameworks. In Django, we can easily give a JSON response to the
4 min read
Creating child process using fork() in Python
Create a child process and display process id of both parent and child process. Fork system call use for creates a new process, which is called child process, which runs concurrently with process (which process called system call fork) and this process is called parent process. After a new child process created, both processes will execute the next
2 min read
Creating NotePad using PyQt5 - Python
In this article we will see how we can create notepad using PyQt5.Notepad is a generic text editor that allows you to create, open, edit, and read plaintext files.PyQt5 is cross-platform GUI toolkit, a set of python bindings for Qt v5. One can develop an interactive desktop application with so much ease because of the tools and simplicity provided
7 min read
Creating Files in HDFS using Python Snakebite
Hadoop is a popular big data framework written in Java. But it is not necessary to use Java for working on Hadoop. Some other programming languages like Python, C++ can also be used. We can write C++ code for Hadoop using pipes API or Hadoop pipes. Hadoop pipes enable task-tracker with the help of sockets. Python can also be used to write code for
3 min read
Creating a radar sweep animation using arcade in Python
The Radar Sweep are used for displays of single level sweeps of radar data, and their display appears in the Main Display window. With the help of arcade module of Python, it is possible to perform a radar sweep animation. Before starting, it is highly recommended to revise concepts of arcade library. To perform a radar sweep animation, follow the
3 min read
Creating payment receipts using Python
Creating payment receipts is a pretty common task, be it an e-commerce website or any local store for that matter. Here, we will see how to create our own transaction receipts just by using python. We would be using reportlab to generate the PDFs. Generally, it comes as a built-in package but sometimes it might not be present too. If it's not prese
3 min read
Creating a Receipt Calculator using Python
A receipt calculator is generally a slip in which the total invoice along with their names is mentioned. We will use the class PrettyTable inside the prettytable library for making our receipt calculator. What is PrettyTable ? It is a class present inside prettytable library which help us to make relational tables in Python. Installation of library
2 min read
Creating first web application using Bottle Framework - Python
There are many frameworks in python which allow you to create a webpage like bottle, flask, django. In this article, you will learn how to create a simple app using bottle web framework. Bottle is a fast, simple and lightweight WSGI micro web-framework for Python. It is distributed as a single file module and has no dependencies other than the Pyth
2 min read
Creating a Path Graph Using Networkx in Python
A path graph is a connected graph denoted by Pn if it contains n nodes. Nodes are connected in form of a straight line in a path graph. Here we will discuss how networkx module can be used to generate one using its inbuilt path_graph() function. Properties of Path Graph:The number of nodes in a path graph(Pn) is N.The number of edges in a path grap
2 min read
Creating a Slow Motion Video Using OpenCV - Python
In this article, we will try to create a slow-motion video using OpenCV( Open Source Computer Vision) library in Python. OpenCV ( is an open-source computer vision and machine learning software library. Now a video is basically a set of moving pictures moving at a rate of about 24 frames per second. If this frame rate is decreased then we get a slo
4 min read
Creating a Contour Map Using Python PyVista
Contour maps are essential for visualizing three-dimensional data on a two-dimensional plane, often used in fields like geography, meteorology, and various scientific disciplines. PyVista, a powerful Python library built on top of the Visualization Toolkit (VTK), offers an intuitive interface for creating and visualizing such maps. In this article,
5 min read
Creating and Using Serializers - Django REST Framework
In Django REST Framework the very concept of Serializing is to convert DB data to a datatype that can be used by javascript. Serializers allow complex data such as querysets and model instances to be converted to native Python datatypes that can then be easily rendered into JSON, XML or other content types. Serializers also provide deserialization,
3 min read
Creating a dataframe using Excel files
Let's see how to read excel files to Pandas dataframe objects using Pandas.Code #1 : Read an excel file using read_excel() method of pandas. C/C++ Code # import pandas lib as pd import pandas as pd # read by default 1st sheet of an excel file dataframe1 = pd.read_excel('SampleWork.xlsx') print(dataframe1) Output : Name Age Stream Percentage 0 Ankit
4 min read
Creating a multiple Selection using Tkinter
Prerequisites: Python Tkinter – ListBox Widget, Scrollable ListBox in Python-tkinter Tkinter is a GUI library in python which is easy to read and understand. In Tkinter, multiple selections can be done using the List box widget. Generally, a Listbox displays different items in the form of a list. A list box widget provides one or more selections of
3 min read
Creating a tabbed browser using PyQt5
In this article, we will see how we can create a tabbed browser using PyQt5. Web browser is a software application for accessing information on the World Wide Web. When a user requests a web page from a particular website, the web browser retrieves the necessary content from a web server and then displays the page on the screen.Tabbing : Adding tab
6 min read
Creating a simple browser using PyQt5
In this article we will see how we can create a simple browser using PyQt5. Web browser is a software application for accessing information on the World Wide Web. When a user requests a web page from a particular website, the web browser retrieves the necessary content from a web server and then displays the page on the screen. PyQt5 is cross-platf
4 min read
Creating a Camera Application using Pyqt5
Prerequisite: Introduction to pyqt-5 PyQt5 is a cross-platform GUI toolkit, a set of python bindings for Qt v5. One can develop an interactive desktop application with so much ease because of the tools and simplicity provided by this library. In this article, we will see how we can create a simple camera application using PyQt5, which will capture
5 min read
Creating Golden Ratio Calculator using PyQt5
In this article we will see how we can create a golden ratio calculator using PyQt5. In mathematics, two quantities are in the golden ratio if their ratio is the same as the ratio of their sum to the larger of the two quantities. The value of golden ratio is 1.61803398875. Below is how the golden ratio calculator will look like PyQt5 is cross-platf
6 min read
Practice Tags :
three90RightbarBannerImg