Home >System Tutorial >LINUX >How To Create PowerPoint Presentations With Python From Command Line
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.
Table of Contents
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:
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.
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.
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
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.
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:
Saving the Presentation:
Finally, the script saves the PowerPoint presentation as Linux_Security_Presentation.pptx in the current directory.
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.
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:
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:
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:
The above is the detailed content of How To Create PowerPoint Presentations With Python From Command Line. For more information, please follow other related articles on the PHP Chinese website!