Python: Add or Remove Sections in PowerPoint

2024-01-19 01:22:56 Written by  support iceblue
Rate this item
(0 votes)

In PowerPoint, sections are a powerful tool for organizing and managing slides. By dividing slides into different sections, you can better organize content, navigate through your presentation, and present information in a more structured manner. This article will demonstrate how to add and remove sections in a PowerPoint presentation using Spire.Presentation for Python.

Install Spire.PDF for Python

This scenario requires Spire.Presentation for Python and plum-dispatch v1.7.4. They can be easily installed in your Windows through the following pip command.

pip install Spire.Presentation

If you are unsure how to install, please refer to this tutorial: How to Install Spire.Presentation for Python on Windows

Add a Section at the End of a PowerPoint in Python

Spire.Presentation for Python provides the Presentation.SectionList.Append(section_name) method to add a section at the end of a presentation. Here are the specific steps to perform this operation:

  • Create a Presentation class instance.
  • Load a PowerPoint document using Presentation.LoadFromFile() method.
  • Add a section at the end using the Presentation.SectionList.Append() method.
  • Save the document using Presentation.SaveToFile() method.
  • Python
from spire.presentation.common import *
from spire.presentation import *

# Create a new presentation object
presentation = Presentation()

# Load a sample PowerPoint presentation
presentation.LoadFromFile("sample.pptx")

# Append a new section
presentation.SectionList.Append("New Section")

# Save the presentation
presentation.SaveToFile("AddSection.pptx", FileFormat.Pptx2013)

# Dispose of the presentation object
presentation.Dispose()

Python: Add or Remove Sections in PowerPoint

Insert a Section Before a Specified Section in Python

You can also use the Presentation.SectionList.Insert(index, section_name) method to insert a new section before a specific section. Here are the detailed steps:

  • Create a Presentation class instance.
  • Load a PowerPoint document using Presentation.LoadFromFile() method.
  • Insert a new section before a specific section using the Presentation.SectionList.Insert() method, where index is the position of the specific section.
  • Save the document using Presentation.SaveToFile() method.
  • Python
from spire.presentation.common import *
from spire.presentation import *

# Create a new presentation object
presentation = Presentation()

# Load a sample PowerPoint presentation
presentation.LoadFromFile("sample.pptx")

# Insert a new section before the second section
presentation.SectionList.Insert(1," New Section")

# Save the presentation
presentation.SaveToFile("AddSection.pptx", FileFormat.Pptx2013)

# Dispose of the presentation object
presentation.Dispose()

Python: Add or Remove Sections in PowerPoint

Add a Section Before a Specified Slide in Python

You can also use the Presentation.SectionList.Add(section_name, slide) method to insert a new section before a specific slide. Here are the detailed steps:

  • Create a Presentation class instance.
  • Load a PowerPoint document using Presentation.LoadFromFile() method.
  • Insert a new section before a specific slide using the Presentation.SectionList.Add() method
  • Save the document using Presentation.SaveToFile() method.
  • Python
from spire.presentation.common import *
from spire.presentation import *

# Create a new presentation object
presentation = Presentation()

# Load a sample PowerPoint presentation
presentation.LoadFromFile("sample.pptx")

# Get the second slide
slide=presentation.Slides[1]

# Add a new section before the second slide
presentation.SectionList.Add("New Section",slide)

# Save the presentation
presentation.SaveToFile("AddSection.pptx", FileFormat.Pptx2013)

# Dispose of the presentation object
presentation.Dispose()

Python: Add or Remove Sections in PowerPoint

Remove a Section from a PowerPoint in Python

If you don't need a specific section, you can simply remove it using the Presentation.SectionList.RemoveAt(index_to_remove) method. Please note that removing a section does not delete the slides within that section. Here are the steps to delete a specific section while preserving its slides:

  • Create a Presentation class instance.
  • Load a PowerPoint document using Presentation.LoadFromFile() method.
  • Remove a specific section using the Presentation.SectionList.RemoveAt(index_to_remove) method, which takes an integer index as a parameter. You can also remove all sections using the Presentation.Slides.RemoveAll() method.
  • Save the document using Presentation.SaveToFile() method.
  • Python
from spire.presentation.common import *
from spire.presentation import *

# Create a new presentation object
presentation = Presentation()

# Load a sample PowerPoint presentation
presentation.LoadFromFile("sample.pptx")

# Remove the second section
presentation.SectionList.RemoveAt(1);

# # Remove all sections
# presentation.SectionList.RemoveAll();

# Save the presentation
presentation.SaveToFile("RemoveSection.pptx", FileFormat.Pptx2013)

# Dispose of the presentation object
presentation.Dispose()

Python: Add or Remove Sections in PowerPoint

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.

Additional Info

  • tutorial_title:
Last modified on Thursday, 25 April 2024 02:19