Создание powerpoint в python

Creating Presentations with Python

Create and update Powerpoint (.pptx) files with Python

Time is the most precious treasure Data Scientists have and it must be used efficiently. If you frequently prepare a presentation then it means you are consuming your time with busywork. But why should you do something like grunt work if you can automate it?

This is my second article about the automation of time-consuming works. You can find the other one that is about creating a pdf file with python by clicking here.

This article consists of five parts.

Now I’ll show you the basics of creating PowerPoint slides with Python.

Getting Started

python-pptx is a Python library for creating and updating PowerPoint files. This article is going to be a basic introduction to this package. If you want to learn much more about it, this is the official documentation page that you should check. Now let’s install the package if you don’t have.

pip install python-pptx

I suggest you use the pip install code line because this package depends on Ixml , Pillow and XlsxWriter packages. If you use setup.py installation method you will need to install these dependencies yourself.

It is always easy to explain something with examples. For this purpose, I am going to use official examples in the first place. At the end of the article, I am going to build a basic PowerPoint presentation page by page. Let’s start with the adding slide. To do this first we need to import the pptx package.

from pptx import Presentation

We are going to use Presentation() to open or create a presentation. Let’s assign it to a variable.

Now we are ready to build our first slide. This slide will contain a title and a subtitle.

lyt=prs.slide_layouts[0] # choosing a slide layout
slide=prs.slides.add_slide(lyt) # adding a slide
title=slide.shapes.title # assigning a title
subtitle=slide.placeholders[1]

Источник

python-pptx¶

python-pptx is a Python library for creating and updating PowerPoint (.pptx) files.

Читайте также:  Программа для создания css дизайна

A typical use would be generating a customized PowerPoint presentation from database content, downloadable by clicking a link in a web application. Several developers have used it to automate production of presentation-ready engineering status reports based on information held in their work management system. It could also be used for making bulk updates to a library of presentations or simply to automate the production of a slide or two that would be tedious to get right by hand.

More information is available in the python-pptx documentation.

Browse examples with screenshots to get a quick idea what you can do with python-pptx.

Feature Support¶

python-pptx has the following capabilities, with many more on the roadmap:

  • Round-trip any Open XML presentation (.pptx file) including all its elements
  • Add slides
  • Populate text placeholders, for example to create a bullet slide
  • Add image to slide at arbitrary position and size
  • Add textbox to a slide; manipulate text font size and bold
  • Add table to a slide
  • Add auto shapes (e.g. polygons, flowchart shapes, etc.) to a slide
  • Add and manipulate column, bar, line, and pie charts
  • Access and change core document properties such as title and subject

Additional capabilities are actively being developed and added on a release cadence of roughly once per month. If you find a feature you need that python-pptx doesn’t yet have, reach out via the mailing list or issue tracker and we’ll see if we can jump the queue for you to pop it in there 🙂

User Guide¶

  • Introduction
  • Installing
  • Getting Started
  • Working with Presentations
  • Working with Slides
  • Understanding Shapes
  • Working with AutoShapes
  • Understanding placeholders
  • Working with placeholders
  • Working with text
  • Working with charts
  • Working with tables
  • Working with Notes Slides
  • Use cases
  • Concepts

Community Guide¶

API Documentation¶

  • Presentations
    • Presentation function
    • Presentation objects
    • CoreProperties objects
    • Slides objects
    • Slide objects
    • SlideLayouts objects
    • SlideLayout objects
    • SlideMasters objects
    • SlideMaster objects
    • SlidePlaceholders objects
    • NotesSlide objects
    • SlideShapes objects
    • GroupShapes objects
    • Shape objects in general
    • Shape objects (AutoShapes)
    • Connector objects
    • FreeformBuilder objects
    • Picture objects
    • GraphicFrame objects
    • GroupShape objects
    • MasterPlaceholder objects
    • LayoutPlaceholder objects
    • ChartPlaceholder objects
    • PicturePlaceholder objects
    • TablePlaceholder objects
    • PlaceholderGraphicFrame objects
    • PlaceholderPicture objects
    • _PlaceholderFormat objects
    • Table objects
    • _Column objects
    • _Row objects
    • _Cell objects
    • Chart objects
    • Legend objects
    • Axis objects
    • MajorGridlines objects
    • TickLabels objects
    • _BasePlot objects
    • DataLabels objects
    • Series objects
    • Point objects
    • TextFrame objects
    • Font objects
    • _Paragraph objects
    • _Run objects
    • ActionSetting objects
    • Hyperlink objects
    • ChartFormat objects
    • FillFormat objects
    • LineFormat objects
    • ColorFormat objects
    • RGBColor objects
    • ShadowFormat objects
    • Image objects
    • MSO_AUTO_SHAPE_TYPE
    • MSO_AUTO_SIZE
    • MSO_COLOR_TYPE
    • MSO_CONNECTOR_TYPE
    • MSO_FILL_TYPE
    • MSO_LANGUAGE_ID
    • MSO_LINE_DASH_STYLE
    • MSO_PATTERN_TYPE
    • MSO_SHAPE_TYPE
    • MSO_TEXT_UNDERLINE_TYPE
    • MSO_THEME_COLOR_INDEX
    • MSO_VERTICAL_ANCHOR
    • PP_ACTION_TYPE
    • PP_MEDIA_TYPE
    • PP_PARAGRAPH_ALIGNMENT
    • PP_PLACEHOLDER_TYPE
    • XL_AXIS_CROSSES
    • XL_CATEGORY_TYPE
    • XL_CHART_TYPE
    • XL_DATA_LABEL_POSITION
    • XL_LEGEND_POSITION
    • XL_MARKER_STYLE
    • XL_TICK_LABEL_POSITION
    • XL_TICK_MARK
    • Excel Number Formats

    Contributor Guide¶

    Источник

    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.

    Источник

Оцените статью