Creating and Updating PowerPoint Presentation using PythonIn the following tutorial, we will understand how to create and update a PowerPoint Presentation with the help of the Python programming language. In order to accomplish the task, we will be using the python-pptx library. So, let’s get started. Understanding the python-pptx librarypython-pptx is a Python library used to create and edit PowerPoint (.pptx) files. This library is not compatible with Microsoft Office 2003 and earlier versions. We can insert shapes, paragraphs, texts, and slides and perform many other activities with the help of this library. A typical utilization would be generating a customized PowerPoint presentation from database content, downloadable by clicking a link in a web application. Some developers have utilized this library to automate the production of presentation-ready engineering status reports on the basis of information held in their work management system. We can also use it to make bulk updates to a library of presentations or automate the production of a slide or two that would be tiresome when done manually. Some Features of the python-pptx libraryThe python-pptx library has the following features, with many more on the roadmap:
How to install the python-pptx library?In order to install the Python module, we need ‘pip‘, a framework to manage packages required to install the modules from the trusted public repositories. Once we have ‘pip‘, we can install the python-pptx module using the command from a Windows command prompt (CMD) or terminal as shown below: Syntax: Verifying the InstallationOnce the module is installed, we can verify it by creating an empty Python program file and writing an import statement as follows: File: verify.py Now, save the above file and execute it using the following command in a terminal: Syntax: If the above Python program file does not return any error, the module is installed properly. However, in the case where an exception is raised, try reinstalling the module, and it is also recommended to refer to the official documentation of the module. Let us now understand the working of the python-pptx library using examples. Creating a new PowerPoint file with the title and subtitle slideWe can create a new PowerPoint file using the Presentation class of the pptx library. We can then create a slide layout using the slide_layouts attribute and create a slide object to add to the presentation file using the add_slide() method. We can add the title and subtitles to the slide using the title and placeholder attributes and specifying the text under the text attribute. Let us consider the following snippet of code demonstrating the same: Example: Output: Explanation: In the above snippet of code, we have imported the Presentation class from the pptx library. We have then created an object of the Presentation class. We have then created the layout for the first slide using the slide_layouts[n] attribute, where n is the index of the slide in the PPT file. We have then used the add_slide() method to create a slide object to add in a PPT file. We then have the title and placeholders[n] attributes to add title and subtitle in the slide. At last, we have added the text to the title and subtitle using the text attribute and saved the PPT file. Creating a Bullet SlideWe can also create a Bullet slide using the Presentation class of the python-pptx library. Let us consider the following example demonstrating the same: Example: Output: Explanation: In the above snippet of code, we have imported the Presentation class from the pptx library. We have then created an object of the Presentation class. We have then created the layout for the first slide using the slide_layouts[n] attribute, where n is the index of the slide in the PPT file. We have then used the add_slide() method to create a slide object to add in a PPT file. We then have the title and placeholders[n] attributes to add title and subtitle in the slide. We have then added the text to the title and subtitle using the text attribute. We have then used the text_frame attribute to add text frame in the slide. We have then used the add_paragraph() to insert a new paragraph to the text frame specifying the levels with the help of the level attribute. At last, we have saved the PPT file. Not all shapes can consist of text; however, those that do always have at least one paragraph, even if that paragraph is empty and no text is visible within the shape. The _BaseShape.has_text_frame attribute allows us to determine whether a shape can consist of text or not (All shapes subclass _BaseShape.) When the value of _BaseShape.has_text_frame is True, _BaseShape.text_frame.paragraphs[0] returns the first paragraph. We can set the text of the first paragraph with the help of the text_frame.paragraphs[0].text attribute. As a shortcut, the library offers some writable properties like _BaseShape.text and _TextFrame.text to accomplish the same thing. Note that these last two calls delete all the paragraphs of the shape except the first one before setting the text it consists of. Adding textbox in PowerPoint PresentationWe can also add textbox in the slide using the add_textbox() method. This method accepts multiple parameters named left, top, width, height. Let us consider the following example demonstrating the same: Example: Output: Explanation: In the above snippet of code, we have imported the required things from the library and created a slide. We have then specified the value of the parameters for the add_textbox() method and used it to create a textbox. We have then created a text frame. We have added some paragraphs to the text frame and included some unique properties like making a paragraph bold and increasing the font size. At last, we have saved the presentation file. Adding Image into the PowerPoint FileWe can use the add_picture() method to insert an image into the PowerPoint Presentation file. Let us consider the following example demonstrating the same: Example: Output: Explanation: In the above snippet of code, we have imported the required things from the library and defined the path to the image file. We have then instantiated the Presentation class and created the layout for the slide. Once we are done creating an object of the slide, we have defined the measurements in inches for left and top. We have then used the add_picture() method that accepts multiple parameters, including the image file path, the value of left, and the top. At last, we have saved the presentation. Adding Tables to the PowerPoint FileThe Presentation class of the python-pptx library also offers the method called add_table() that allows us to add tables to the PowerPoint File. Let us consider the following snippet of code demonstrating the same: Example: Output: Explanation: In the above snippet of code, we have imported the required things from the library and instantiated the Presentation class. We have then created the layout of the slide and a slide object. We have then added the title to the slide. We have then specified the rows and columns and the measurement of the left, top, width, and height. We have used these values as the add_table() method parameters. We have then set the column widths, heading, and body of the cell. At last, we have saved the presentation file. Adding shapes in the PowerPoint FileWe can use the add_shape() method in order to add shapes to the PowerPoint presentation. Let us consider the following example demonstrating the same: Example: Output: Explanation: In the above snippet of code, we have imported the required things from the library and instantiated the Presentation class. We have then created the layout of the slide and a slide object. We have then added the title to the slide. We have then defined the left, top, width, and height measurements and used the add_shape() method. This method defines constant representing the available auto shapes (for example, MSO_SHAPE.PENTAGON, MSO_SHAPE.ROUNDED_RECT, MSO_SHAPE.CHEVRON, and many more) along with the measurements as the parameters. We have then added some text to the image. Later, we changed the value of left and width and used the for-loop to create more shapes and add the text to them. At last, we have saved the PPT file. |
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/263248.html