Home Python How To Create PowerPoint Presentations With Python From Command Line

How To Create PowerPoint Presentations With Python From Command Line

Automating the Process of Creating Simple PowerPoint Slides with Python-pptx Library.

By sk
595 views

This Step-by-Step guide demonstrates how to use Python and the python-pptx library to create PowerPoint presentations from command line.

The steps and code provided in this guide are not specific to any particular operating system and should work on all major platforms, including Linux, macOS, and Windows.

What is Python-pptx, by the way?

The python-pptx library is an open-source Python library that allows you to create, read, and update PowerPoint (.pptx) files.

The python-pptx library is particularly well-suited for generating PowerPoint presentations dynamically from various data sources as listed below:

  1. Database Queries: By connecting to a database and executing queries, you can fetch data and use the python-pptx library to generate PowerPoint slides with visualizations, charts, or tables based on the queried data. This approach can be useful for creating data-driven presentations or reports.
  2. Analytics Outputs: If you have analytics software or scripts that generate structured data or insights, you can use the python-pptx library to transform that data into visually appealing PowerPoint slides, complete with charts, graphs, and other visual elements.
  3. JSON Payloads: With the increasing prevalence of APIs and web services, it's common to receive data in JSON format. The python-pptx library can be used to parse JSON payloads and dynamically create PowerPoint slides based on the structured data contained within them.
  4. HTTP Requests: By integrating the python-pptx library into a web application or API, you can generate PowerPoint presentations on-the-fly in response to HTTP requests. This allows for the creation of dynamic, customized presentations tailored to specific user inputs or data sources.

The python-pptx library runs on any Python-capable platform, including Linux, macOS, and Windows.

Another significant benefit of the python-pptx library is that it does not require the PowerPoint application to be installed on the system where the code is running.

Now let us discuss how to automate the process of creating simple PowerPoint presentations using python-pptx library.

Create PowerPoint Presentations using Python

The process is divided into three straightforward steps: installing the required software, creating the Python script, and running the script to produce the presentation file.

Step 1 - Installing Prerequisites

Before proceeding, ensure that you have Python installed on your system.

Python is pre-installed in most Linux operating systems. If not, you can install it using the following command on Debian-based systems:

$ sudo apt install python3

On Red-hat based systems, run:

$ sudo dnf install python3

Next, install the python-pptx library, which provides the necessary functionality to create PowerPoint presentations programmatically. You can install it using pip, Python's package installer:

$ pip install python-pptx

Step 2 - Creating the Python Script

Create a new Python script (e.g., create_ppt.py):

$ nano create_ppt.py

and copy the following code into it:

from pptx import Presentation
from pptx.util import Inches

# Create a new presentation object
presentation = Presentation()

# Title Slide
title_slide_layout = presentation.slide_layouts[0]  # Layout for title slides
slide = presentation.slides.add_slide(title_slide_layout)
title = slide.shapes.title
subtitle = slide.placeholders[1]
title.text = "Linux Security Automation"
subtitle.text = "An overview of securing Linux systems"

# Slide 1: Importance of Linux Security
content_slide_layout = presentation.slide_layouts[1]  # Layout for content slides
slide = presentation.slides.add_slide(content_slide_layout)
title = slide.shapes.title
content = slide.placeholders[1]
title.text = "Importance of Linux Security"
content.text = (
    "1. Protects against unauthorized access.\n"
    "2. Ensures data integrity.\n"
    "3. Maintains system availability.\n"
    "4. Protects sensitive information.\n"
)

# Slide 2: Common Security Practices
slide = presentation.slides.add_slide(content_slide_layout)
title = slide.shapes.title
content = slide.placeholders[1]
title.text = "Common Security Practices"
content.text = (
    "1. Regularly update and patch the system.\n"
    "2. Use strong passwords and change them regularly.\n"
    "3. Enable and configure firewalls.\n"
    "4. Use antivirus software.\n"
    "5. Monitor system logs for suspicious activities.\n"
)

# Slide 3: Security Tools
slide = presentation.slides.add_slide(content_slide_layout)
title = slide.shapes.title
content = slide.placeholders[1]
title.text = "Security Tools"
content.text = (
    "1. SELinux/AppArmor - Mandatory Access Control.\n"
    "2. ClamAV - Antivirus software.\n"
    "3. Fail2Ban - Prevent brute force attacks.\n"
    "4. UFW - Uncomplicated Firewall.\n"
)

# Save the presentation
presentation.save('Linux_Security_Presentation.pptx')

Edit the script and update the title, content of the slides and the output file as you wish. Once done, save the file and close it.

Explanation of the Script:

This script creates a new PowerPoint presentation and adds four slides: a title slide, and three content slides covering the importance of Linux security, common security practices, and security tools.

Title Slide:

The script initializes a new Presentation object and adds a title slide with the main title "Linux Security Automation" and the subtitle "An overview of securing Linux systems".

Content Slides:

The script then adds three content slides, each with a title and bullet points covering different aspects of Linux security:

  • Slide 1: Importance of Linux Security (e.g., protecting against unauthorized access, ensuring data integrity, maintaining system availability, and protecting sensitive information).
  • Slide 2: Common Security Practices (e.g., regularly updating and patching the system, using strong passwords, enabling firewalls, using antivirus software, and monitoring system logs).
  • Slide 3: Security Tools (e.g., SELinux/AppArmor for Mandatory Access Control, ClamAV for antivirus protection, Fail2Ban for preventing brute force attacks, and UFW for an uncomplicated firewall).

Saving the Presentation:

Finally, the script saves the PowerPoint presentation as Linux_Security_Presentation.pptx in the current directory.

Step 3 - Generate PowerPoint Slides

Navigate to the directory containing the script in your terminal and run the following command:

$ python3 create_ppt.py

This command will execute the script, and generate a new PowerPoint file named "Linux_Security_Presentation.pptx" in the same directory.

Step 4 - Run or Customize PPTs

As I mentioned, this script will only create simple PowerPoint presentations. They are plain with white background.

Here is a sample PPT slide that I created using this script:

A Powerpoint Slide Created with Python
A Powerpoint Slide Created with Python

You can open it with any PowerPoint application (For example LibreOffice Impress or MS PowerPoint) and customize the look of the slides as per your own liking.

Here's how the slide looks like after I changed its background and added our blog's logo on the top of the slide:

A Powerpoint Slide Created with Python-pptx Library
A PowerPoint Slide Created with Python-pptx Library

This script serves as a basic example, and you can further enhance it by adding more slides, customizing the content, or incorporating additional features, such as adding images, charts, or formatting options.

You get the idea. Use our script as a starting point. Customize it and be creative. You can make the slides more elegant and professional.

Resource:

You May Also Like

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

This website uses cookies to improve your experience. By using this site, we will assume that you're OK with it. Accept Read More