Pip install python pptx

How to create powerpoint files using Python

We’ve all had to make PowerPoint presentations at some point in our lives. Most often we’ve used Microsoft’s PowerPoint or Google Slides.

But what if you don’t have membership or access to the internet? Or what if you just wanted to do it the “programmers” way?

Well, worry not for Python’s got your back!

In this article you’ll learn how to create a PowerPoint file and add some content to it with the help of Python. So let’s get started!

Getting Started

Throughout this walkthrough, we’ll be using the python-pptx package. This package supports different python versions ranging from 2.6 to 3.6.

So, make sure you have the right version of Python installed on your computer first.

Next, open your terminal and type −

Once the module is successfully installed, you are all set to start coding!

Importing the Modules

Before we get into the main aspects of it, we must first import the right modules to utilise the various features of the package.

So, let’s import the presentation class that contains all the required methods to create a PowerPoint.

from pptx import Presentation

Now, we are all set to create a presentation.

Creating a Presentation

Let us now create an object of the Presentation class to access its various methods.

Next, we need to select a layout for the presentation.

As you can see, there are nine different layouts. In the pptx module, each layout is numbered from 0 to 8. So, “Title Slide” is 0 and the “Picture with Caption” is 8.

So, let us first add a title slide.

Layout = X.slide_layouts[0] first_slide = X.slides.add_slide(Layout) # Adding first slide

Now, we have created a layout and added a slide to our presentation.

Let us now add some content to the first slide.

first_slide.shapes.title.text = "Creating a powerpoint using Python" first_slide.placeholders[1].text = "Created by Tutorialpoints"

In the above lines, we first add a title to the “first slide” and a subtitle using the placeholder.

Now, let us save the presentation. We can do this using the save command.

X.save("First_presentation.pptx")

If you run the program, it will save the PowerPoint presentation in the directory where your program is saved.

Output

You have successfully created your PowerPoint presentation.

Creating a second slide and adding some content

Firstly, you’ll need to import additional methods to add content.

from pptx import Presentation from pptx.util import Inches

Let us first create and add the second slide.

Second_Layout = X.slide_layouts[5] second_slide = X.slides.add_slide(Second_Layout)

Adding title for the next slide,

second_slide.shapes.title.text = "Second slide"

Now, we must create a textbox and move its layout to suit our needs.

Let us position it and adjust its margins in inches.

textbox = second_slide.shapes.add_textbox(Inches(3), Inches(1.5),Inches(3), Inches(1))

The above line of code will place a textbox 3 Inches from left and 1.5 Inches from the top with a width of 3 Inches and height of 1 Inch.

Once we have the layout and position fixed, time to create a textframe to add content to.

textframe = textbox.text_frame

Now to add a paragraph of content,

paragraph = textframe.add_paragraph() paragraph.text = "This is a paragraph in the second slide!"

Finally, save the presentation again using the save method.

X.save("First_presentation.pptx")

Output

Example

# Creating powerpoint presentations using the python-pptx package from pptx import Presentation from pptx.util import Inches X = Presentation() Layout = X.slide_layouts[0] first_slide = X.slides.add_slide(Layout) first_slide.shapes.title.text = "Creating a powerpoint using Python" first_slide.placeholders[1].text = "Created by Tutorialpoints" X.save("First_presentation.pptx") Second_Layout = X.slide_layouts[5] second_slide = X.slides.add_slide(Second_Layout) second_slide.shapes.title.text = "Second slide" textbox = second_slide.shapes.add_textbox(Inches(3), Inches(1.5),Inches(3), Inches(1)) textframe = textbox.text_frame paragraph = textframe.add_paragraph() paragraph.text = "This is a paragraph in the second slide!" X.save("First_presentation.pptx")

Conclusion

That’s it! You can now create your own presentation with the help of Python.

And there are a lot more features within the pptx package that allows you to completely customise your presentation from A-Z just the way you do it in GUI.

You can add images, build charts, display statistics and a lot more.

You can go through python-pptx official documentation for more syntaxes and features.

Источник

Python-PPTX Library

Python-PPTX Library

  1. What Is the python-pptx Library in Python
  2. How to Install the python-pptx Library
  3. Create and Edit a PowerPoint File in Python

This tutorial will discuss the python-pptx library and implement it in Python.

What Is the python-pptx Library in Python

PowerPoint is widely recognized as the most popular software for creating and editing presentations. Python provides a library named python-pptx utilized to create or edit PowerPoint files.

These files have a pptx extension. The python-pptx library can function only on newer versions released after Microsoft Office 2003.

Inserting paragraphs, shapes, slides, and plenty more to a PowerPoint presentation can be done through this library.

How to Install the python-pptx Library

The python pptx library can be installed by simply utilizing the pip command. The following command must be written on the command prompt to install the python-pptx library.

We should note that this package works on Python 2.6 and releases after that.

The first half of the article explains what the python-pptx library was. The other half of the article will demonstrate the various functions of the python-pptx library to create and edit a PowerPoint presentation.

Create and Edit a PowerPoint File in Python

Create a New PowerPoint File and Add Title/Subtitles

Firstly, we will import the pptx library to the Python code to ensure no errors while utilizing the pptx library functions. Then, we will create a presentation object and apply the necessary functions to it.

The following code shows how to create a presentation object and add a title and subtitles.

from pptx import Presentation X = Presentation() # Presentation object created slide1_layout = X.slide_layouts[0] #Slide layout process slide = X.slides.add_slide(slide1_layout) #Then, we create title slide.shapes.title.text = " PPT TITLE (PYTHON) " X.save("delft.pptx") # File saved print("done") 

The above code provides the following output.

python pptx

Convert .pptx File to a .txt File

Another essential step to understanding Python’s pptx library is converting a presentation with the (.pptx) extension to a text file with the (.txt) extension.

The following code converts a file with the (.pptx) to the (.txt) extension.

from pptx import Presentation X = Presentation("abc.pptx") # Presentation object created # Then file is opened in write mode ftw_data = open("fte_ppt.txt", "w") # write text from powerpoint # file into .txt file for slide in X.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:  ftw_data.write(run.text) ftw_data.close() # The file is closed print("Done") 

Insert an Image Into a PowerPoint Presentation

Another one of the essentials of editing a PowerPoint presentation through Python is to learn how to add an image to a PowerPoint presentation.

The following code inserts an image into a PowerPoint presentation.

from pptx import Presentation from pptx.util import Inches  img_path = 'vk.png' #specify image path  X = Presentation() # presentation object created  bs_layout = X.slide_layouts[6] #select blank slide layout  slide = X.slides.add_slide(bs_layout)  left = top = Inches(1) # add margins  pic = slide.shapes.add_picture(img_path,  left, top) #add images left = Inches(1) height = Inches(1)  pic = slide.shapes.add_picture(img_path, left,  top, height = height) X.save('test_4.pptx') # file is saved print("Done") 

The above code provides the following output.

Here, we have covered some essentials of creating and editing PowerPoint presentations in Python.

Additionally, we can utilize several functions in the pptx library to customize more things like adding charts, tables, shapes, etc.

Vaibhhav is an IT professional who has a strong-hold in Python programming and various projects under his belt. He has an eagerness to discover new things and is a quick learner.

Источник

Читайте также:  Java method is accessible
Оцените статью