Document Operation (13)
Splitting a PowerPoint presentation into smaller files or individual sections can be useful in various situations. For instance, when collaborating with a team, each member may only need a specific section of the presentation to work on. Additionally, breaking a large presentation into smaller parts can simplify sharing over email or uploading to platforms with file size restrictions. In this article, we'll show you how to split PowerPoint presentations by slides, slide ranges, and sections in Python using Spire.Presentation for Python.
- Split PowerPoint Presentations by Slides in Python
- Split PowerPoint Presentations by Slide Ranges in Python
- Split PowerPoint Presentations by Sections in Python
Install Spire.Presentation 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
Split PowerPoint Presentations by Slides in Python
Developers can use Spire.Presentation for Python to split a PowerPoint presentation into individual slides by iterating through the slides in the presentation and adding each slide to a new presentation. The detailed steps are as follows.
- Create an instance of the Presentation class.
- Load a PowerPoint presentation using the Presentation.LoadFromFile() method.
- Iterate through all slides in the presentation:
- Access the current slide through the Presentation.Slides[index] property.
- Create a new PowerPoint presentation using the Presentation class and remove its default slide using the Presentation.Slides.RemoveAt(0) method.
- Append the current slide to the new presentation using the Presentation.Slides.AppendBySlide() method.
- Save the new presentation as a file using the ISlide.SaveToFile() method.
- Python
from spire.presentation.common import * from spire.presentation import * # Create an instance of the Presentation class presentation = Presentation() # Load a PowerPoint presentation presentation.LoadFromFile("Sample.pptx") # Iterate through all slides in the presentation for i in range(presentation.Slides.Count): # Get the current slide slide = presentation.Slides[i] # Create a new PowerPoint presentation and remove its default slide newPresentation = Presentation() newPresentation.Slides.RemoveAt(0) # Append the current slide to the new presentation newPresentation.Slides.AppendBySlide(slide) # Save the new presentation as a file newPresentation.SaveToFile(f"output/Presentations/Slide-{i + 1}.pptx", FileFormat.Pptx2013) newPresentation.Dispose() presentation.Dispose()
Split PowerPoint Presentations by Slide Ranges in Python
Apart from splitting a PowerPoint presentation into individual slides, developers can also divide it into specific ranges of slides by adding the desired slides to new presentations. The detailed steps are as follows.
- Create an instance of the Presentation class.
- Load a PowerPoint presentation using the Presentation.LoadFromFile() method.
- Create new PowerPoint presentations using the Presentation class and remove the default slides within them using the Presentation.Slides.RemoveAt(0) method.
- Append specified ranges of slides to the new presentations using the Presentation.Slides.AppendBySlide() method.
- Save the new presentations as files using the Presentation.SaveToFile() method.
- Python
from spire.presentation.common import * from spire.presentation import * # Create an instance of the Presentation class presentation = Presentation() # Load a PowerPoint presentation presentation.LoadFromFile("Sample.pptx") # Create two new PowerPoint presentations and remove their default slides presentation1 = Presentation() presentation2 = Presentation() presentation1.Slides.RemoveAt(0) presentation2.Slides.RemoveAt(0) # Append slides 1-3 to the first new presentation for i in range(3): presentation1.Slides.AppendBySlide(presentation.Slides[i]) # Append the remaining slides to the second new presentation for i in range(3, presentation.Slides.Count): presentation2.Slides.AppendBySlide(presentation.Slides[i]) # Save the new presentations as files presentation1.SaveToFile("output/Presentations/SlideRange1.pptx", FileFormat.Pptx2013) presentation2.SaveToFile("output/Presentations/SlideRange2.pptx", FileFormat.Pptx2013) presentation1.Dispose() presentation2.Dispose() presentation.Dispose()
Split PowerPoint Presentations by Sections in Python
Sections in PowerPoint are often used to organize slides into manageable groups. With Spire.Presentation for Python, developers can split a PowerPoint presentation into sections by iterating through the sections in the presentation and adding the slides within each section to a new presentation. The detailed steps are as follows.
- Create an instance of the Presentation class.
- Load a PowerPoint presentation using the Presentation.LoadFromFile() method.
- Iterate through all sections in the presentation:
- Access the current section through the Presentation.SectionList[] property.
- Create a new PowerPoint presentation using the Presentation class and remove its default slide using the Presentation.Slides.RemoveAt(0) method.
- Add a section to the new presentation with the same name using the Presentation.SectionList.Append() method.
- Retrieve the slides of the current section using the Section.GetSlides() method.
- Iterate through the retrieved slides and add them to the section of the new presentation using the Section.Insert() method.
- Save the new presentation as a file using the Presentation.SaveToFile() method.
- Python
from spire.presentation.common import * from spire.presentation import * # Create an instance of the Presentation class presentation = Presentation() # Load a PowerPoint presentation presentation.LoadFromFile("Sample.pptx") # Iterate through all sections for i in range(presentation.SectionList.Count): # Get the current section section = presentation.SectionList[i] # Create a new PowerPoint presentation and remove its default slide newPresentation = Presentation() newPresentation.Slides.RemoveAt(0) # Add a section to the new presentation newSection = newPresentation.SectionList.Append(section.Name) # Retrieve the slides of the current section slides = section.GetSlides() # Insert each retrieved slide into the section of the new presentation for slide_index, slide in enumerate(slides): newSection.Insert(slide_index, slide) # Save the new presentation as a file newPresentation.SaveToFile(f"output/Presentations/Section-{i + 1}.pptx", FileFormat.Pptx2019) newPresentation.Dispose() presentation.Dispose()
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.
Python: Remove Backgrounds from PowerPoint Slide or Slide Masters
2024-11-06 01:03:12 Written by support iceblueA well-chosen background can enhance a presentation's appeal, but overly elaborate colors or images may distract viewers and obscure the main message. Additionally, when reusing templates, the original background may not suit the new content. In these cases, removing the background becomes essential to keep your slides clear and focused. This article will show you how to remove backgrounds from PowerPoint slides or slide masters in Python with Spire.Presentation for Python, giving you the flexibility to create clean, professional presentations that keep the audience's attention on what matters.
- Remove Backgrounds from the Specified Slide
- Remove Backgrounds from All Slides
- Remove Backgrounds from PowerPoint Slide Masters
Install Spire.Presentation 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
Remove Backgrounds from the Specified Slide
There are typically two types of backgrounds in PowerPoint: background colors and background images. Although these backgrounds differ in their setup, the method to clear them is the same - using the BackgroundType property provided by Spire.Presentation for Python. Let's take a closer look at how to remove backgrounds from a PowerPoint slide with it.
Steps to remove background from a specified slide:
- Create an object for the Presentation class.
- Load a PowerPoint presentation from the local storage using Presentation.LoadFromFile() method.
- Get a certain slide with Presentation.Slides[] method.
- Remove the background by configuring BackgroundType property to none.
- Save the modified PowerPoint presentation using Presentation.SaveToFile() method, and release the memory.
Here is the code example of removing the background on the fourth slide:
- Python
from spire.presentation import * # Create a Presentation document object presentation = Presentation() # Read the presentation document from file presentation.LoadFromFile("imagebackground.pptx") # Get the fourth slide slide = presentation.Slides[3] # Remove the background by setting the background type slide.SlideBackground.Type = BackgroundType.none # Save the modified presentation presentation.SaveToFile("RemoveBackground.pptx", FileFormat.Pptx2010) # Release resource presentation.Dispose()
Remove Backgrounds from All Slides
Batch-deleting all slide backgrounds follows nearly the same steps as deleting a single slide background. The main difference is that you'll need to loop through each slide before setting the background type to ensure no slides are missed.
Steps to remove backgrounds from PowerPoint slides in a batch:
- Instantiate a Presentation class.
- Specify the file path to read a PowerPoint presentation using Presentation.LoadFromFile() method.
- Loop through each slide in the presentation.
- Remove all backgournds by applying BackgroundType.none property to each slide.
- Save the updated PowerPoint presentation as a new file with Presentation.SaveToFile() method, and release the resource.
Below is the code example for removing each background from PowerPoint slides:
- Python
from spire.presentation import * # Create a Presentation document object presentation = Presentation() # Read the presentation document from file presentation.LoadFromFile("presentation.pptx") # Loop through each slide for slide in presentation.Slides: # Remove the background image or color by setting the background type slide.SlideBackground.Type = BackgroundType.none # Save the modified presentation presentation.SaveToFile("RemoveBackground_allSlides.pptx", FileFormat.Pptx2010) # Release resource presentation.Dispose()
How to Remove Backgrounds from PowerPoint Slide Masters
If the slide background still exists after using the above method, you may need to remove the slide master's background instead. Unlike individual slides, setting the background of a slide master applies changes across all slides, so removing the slide master background can efficiently clear all backgrounds at once.
Steps to remove backgrounds from PowerPoint slide masters:
- Create an instance of the Presentation class.
- Load a presentation from the disk with Presentation.LoadFromFile() method.
- Retrieve a specified slide master using Presentation.Masters[] method.
- Access the background of the slide master with Masters.SlideBackground property.
- Remove the background by setting BackgroundType property to none.
- Save the newly modified PowerPoint presentation with Presentation.SaveToFile() method.
Note: Since the process of batch-removing slide master backgrounds is almost similar to deleting background from a slide master, this section will show the steps in the code comments rather than listing them separately.
Here is an example of removing the background from the third slide master:
- Python
from spire.presentation import * # Create a Presentation object presentation = Presentation() # Load the sample file from the disk presentation.LoadFromFile("presentation.pptx") # Get the third slide master master = presentation.Masters[2] # Access the background of the slide master SlideBackground = master.SlideBackground # Clear the background by setting the slide master background style to none master.SlideBackground.Type = BackgroundType.none # Loop through each slide master #for master in presentation.Masters: # Set the background type to none to remove it #master.SlideBackground.Type = BackgroundType.none # Save the result presentation presentation.SaveToFile("remove_background.pptx", FileFormat.Pptx2013) # Release resources presentation.Dispose()
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.
When preparing multiple PowerPoint presentations with similar themes, copying slides helps to maintain consistency in terms of design, layout and content. This ensures that all presentations have a uniform appearance, which can enhance the aesthetics of your document. In this article, you will learn how to copy or clone slides in PowerPoint presentations in Python using Spire.Presentation for Python.
- Copy Slides Within the Same Presentation with Python
- Copy Slides to Another Presentation with Python
Install Spire.Presentation 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
Copy Slides Within the Same Presentation with Python
You can clone a slide either at a specified location or at the end of a PowerPoint presentation through the Presentation.Slides.Insert(Index: int, slide: ISlide) or Presentation.Slides.AppendBySlide(slide: ISlide) methods. The following are the detailed steps.
- Create a Presentation instance.
- Load a PowerPoint presentation using Presentation.LoadFromFile() method.
- Get a specified slide using Prenstion.Slides[] property.
- Clone the slide to the end of the same presentation using Presentation.Slides.AppendBySlide() method.
- Clone the slide to a specific position within the same presentation using Presentation.Slides.Insert() method.
- Save the result file using Presentation.SaveToFile() method.
- Python
from spire.presentation.common import * from spire.presentation import * inputFile ="Input1.pptx" outputFile ="CloneSlidesWithinTheSame.pptx" # Create a Presentation instance ppt = Presentation() #Load a PowerPoint presentation ppt.LoadFromFile(inputFile) # Get the first slide in the presentation slide = ppt.Slides[0] # Clone the slide to the end of the presentation ppt.Slides.AppendBySlide(slide) # Clone the slide to the third position within the presentation ppt.Slides.Insert(2, slide) # Save the result file ppt.SaveToFile(outputFile, FileFormat.Pptx2016) ppt.Dispose()
Copy Slides to Another Presentation with Python
Spire.Presentation for Python also allows you to load two PowerPoint files and then clone the slides from one presentation to another presentation. The following are the detailed steps.
- Create a Presentation instance.
- Load two PowerPoint presentations using Presentation.LoadFromFile() method.
- Get two slides in the first presentation using Prenstion.Slides[] property.
- Clone the first slide to a specific position in the second presentation using Presentation.Slides.Insert() method.
- Clone the second slide to the end of the second presentation using Presentation.Slides.AppendBySlide() method.
- Save the result file using Presentation.SaveToFile() method.
- Python
from spire.presentation.common import * from spire.presentation import * inputFile_1 = "Input1.pptx" inputFile_2 = "Input2.pptx" outputFile ="CloneSlidesToAnother.pptx" # Load the first PowerPoint presentation sourcePPT = Presentation() sourcePPT.LoadFromFile(inputFile_1) # Load the second PowerPoint presentation destPPT = Presentation() destPPT.LoadFromFile(inputFile_2) # Get two slides in the first presentation slide1 =sourcePPT.Slides[1] slide2 =sourcePPT.Slides[2] # Clone slide1 to the second position in the second presentation destPPT.Slides.Insert(1, slide1) # Clone slide2 to the end of the second presentation destPPT.Slides.AppendBySlide(slide2) # Save the second presentation destPPT.SaveToFile(outputFile, FileFormat.Pptx2016) destPPT.Dispose()
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.
Rearranging slides in a PowerPoint presentation is a simple but essential skill. Whether you need to change the order of your points, group related slides together, or move a slide to a different location, the ability to efficiently reorganize your slides can help you create a more coherent and impactful presentation.
In this article, you will learn how to rearrange slides in a PowerPoint document in Python using Spire.Presentation for Python.
Install Spire.Presentation for Python
This scenario requires Spire.Presentation for Python and plum-dispatch v1.7.4. They can be easily installed in your system 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
Rearrange Slides in a PowerPoint Document in Python
To reorder the slides in PowerPoint, two Presentation objects were created - one for loading the original document, and one for creating a new document. By copying the slides from the original document to the new one in the desired sequence, the slide order could be easily rearranged.
The following are the steps to rearrange slides in a PowerPoint document using Python.
- Create a Presentation object.
- Load a PowerPoint document using Presentation.LoadFromFile() method.
- Specify the slide order within a list.
- Create another Presentation object for creating a new presentation.
- Add the slides from the original document to the new presentation in the specified order using Presentation.Slides.AppendBySlide() method.
- Save the new presentation to a PPTX file using Presentation.SaveToFile() method.
- Python
from spire.presentation.common import * from spire.presentation import * # Create a Presentation object presentation = Presentation() # Load a PowerPoint file presentation.LoadFromFile("C:\\Users\\Administrator\\Desktop\\input.pptx") # Specify the new slide order within a list newSlideOrder = [4,2,1,3] # Create another Presentation object new_presentation = Presentation() # Remove the default slide new_presentation.Slides.RemoveAt(0) # Iterate through the list for i in range(len(newSlideOrder)): # Add the slides from the original PowerPoint file to the new PowerPoint document in the new order new_presentation.Slides.AppendBySlide(presentation.Slides[newSlideOrder[i] - 1]) # Save the new presentation to file new_presentation.SaveToFile("output/NewOrder.pptx", FileFormat.Pptx2019) # Dispose resources presentation.Dispose() new_presentation.Dispose()
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.
Python: Create, Modify, and Copy Slide Master in PowerPoint Presentations
2024-05-13 01:14:23 Written by support iceblueSlide Master in PowerPoint presentations is a powerful feature that lies at the heart of designing consistent and professional-looking slideshows. It's essentially a blueprint or a template that controls the overall design and layout of the slides, allowing users to establish uniformity across presentations without having to manually format each slide individually. In this article, we will explore how to harness the power of Spire.Presentation for Python to create, modify, and apply slide masters in PowerPoint presentations within Python programs.
- Create and Apply Slide Masters in PowerPoint Presentations
- Modify Slide Masters in PowerPoint Presentations
- Copy Slide Masters Between PowerPoint Presentations
Install Spire.Presentation 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: How to Install Spire.Presentation for Python on Windows
Create and Apply Slide Masters in PowerPoint Presentations
Every PowerPoint presentation in PowerPoint, regardless of whether it is newly created or not, will have at least one slide master. Developers can modify the default master or create new ones and apply them to slides with Spire.Presentation for Python to achieve a consistent style and content layout across the presentation.
The detailed steps for creating new slide masters and applying them to the slides in a presentation file are as follows:
- Create an object of Presentation class and load a PowerPoint presentation using Presentation.LoadFromFile() method.
- Create slide masters using Presentation.Masters.AppendSlide() method.
- Use the methods under IMasterSlide class to set the backgrounds, customize color schemes, insert images, shapes, and text, etc.
- Apply the slide masters to specific slides through ISlide.Layout property.
- Save the presentation using Presentation.SaveToFile() method.
- Python
from spire.presentation.common import * from spire.presentation import * # Create an instance of Presentation class pres = Presentation() # Load a Presentation file pres.LoadFromFile("Sample.pptx") # Add a cover slide master and a body slide master master1 = pres.Masters.AppendSlide(pres.Masters.get_Item(0)) coverMaster = pres.Masters.get_Item(master1) master2 = pres.Masters.AppendSlide(pres.Masters.get_Item(0)) bodyMaster = pres.Masters.get_Item(master2) # Set background images for the two slide masters pic1 = "Background1.jpg" pic2 = "Background2.jpg" rect = RectangleF.FromLTRB (0, 0, pres.SlideSize.Size.Width, pres.SlideSize.Size.Height) coverMaster.SlideBackground.Fill.FillType = FillFormatType.Picture image1 = coverMaster.Shapes.AppendEmbedImageByPath (ShapeType.Rectangle, pic1, rect) coverMaster.SlideBackground.Fill.PictureFill.Picture.EmbedImage = image1.PictureFill.Picture.EmbedImage bodyMaster.SlideBackground.Fill.FillType = FillFormatType.Picture image2 = bodyMaster.Shapes.AppendEmbedImageByPath (ShapeType.Rectangle, pic2, rect) bodyMaster.SlideBackground.Fill.PictureFill.Picture.EmbedImage = image2.PictureFill.Picture.EmbedImage # Insert a logo to the body slide master logo = "Logo.png" bodyMaster.Shapes.AppendEmbedImageByPath(ShapeType.Rectangle, logo, RectangleF.FromLTRB(pres.SlideSize.Size.Width - 110, 10, pres.SlideSize.Size.Width - 10, 110)) # Insert text to the body slide master shape = bodyMaster.Shapes.AppendShape(ShapeType.Rectangle, RectangleF.FromLTRB(pres.SlideSize.Size.Width - 210, 110, pres.SlideSize.Size.Width - 10, 150)) shape.Fill.FillType = FillFormatType.none shape.Line.FillType = FillFormatType.none shape.TextFrame.Text = "Spire.Presentation" # Set the color scheme for the two slide masters coverMaster.Theme.ColorScheme.Accent1.Color = Color.get_Red() coverMaster.Theme.ColorScheme.Accent2.Color = Color.get_Blue() bodyMaster.Theme.ColorScheme.Accent1.Color = Color.get_Brown() coverMaster.Theme.ColorScheme.Accent2.Color = Color.get_Green() # Apply the first master with layout to the first slide pres.Slides.get_Item(0).Layout = coverMaster.Layouts.GetByType(SlideLayoutType.Title) # Apply the second master with layout to other slides for i in range(1, pres.Slides.Count): pres.Slides.get_Item(i).Layout = bodyMaster.Layouts.GetByType(SlideLayoutType.TitleAndObject) # Save the document pres.SaveToFile("output/CreateAndApplySlideMaster.pptx", FileFormat.Pptx2016) pres.Dispose()
Modify Slide Masters in PowerPoint Presentations
A presentation can have multiple slide masters, which can be applied to different slides to achieve a unified style application and modification for different types of slides.
The Presentation.Masters.get_Item() method in Spire.Presentation for Python allows developers to retrieve the specified slide master in the presentation by index and modify the master. The following step-by-step example demonstrates how to retrieve a slide master and modify its background, color scheme, and embedded images:
- Create an object of Presentation class and load a PowerPoint presentation using Presentation.LoadFromFile() method.
- Get a slide master through Presentation.Masters property.
- Use the methods under IMasterSlide class to change the background, set the color scheme, delete and insert text and images, etc.
- Save the presentation using Presentation.SaveToFile() method.
- Python
from spire.presentation.common import * from spire.presentation import * # Create an object of Presentation pres = Presentation() # Load a PowerPoint presentation pres.LoadFromFile("output/CreateAndApplySlideMaster.pptx") # Get the third slide master master = pres.Masters[2] # Change the background master.SlideBackground.Type = BackgroundType.Custom master.SlideBackground.Fill.FillType = FillFormatType.Solid master.SlideBackground.Fill.SolidColor.Color = Color.get_LightBlue() # Change the color sheme master.Theme.ColorScheme.Accent1.Color = Color.get_Red() master.Theme.ColorScheme.Accent2.Color = Color.get_Green() # Remove the pictures in the slide master pictures = [shape for shape in master.Shapes if isinstance(shape, SlidePicture)] for picture in pictures: master.Shapes.Remove(picture) # Change the text in the slide master texts = [shape for shape in master.Shapes if isinstance(shape, IAutoShape)] for text in texts: if len(text.TextFrame.Text) != 0: text.TextFrame.Text = "Spire.Presentation for Python" # Save the presentation pres.SaveToFile("output/ModifySlideMaster.pptx", FileFormat.Pptx2016) pres.Dispose()
Copy Slide Masters Between PowerPoint Presentations
Applying the slide style of a presentation to another presentation can be achieved by copying the slide master between presentations and applying the master style to the specified slides. The following are the steps to copy the slide master between presentations and apply it to the specified slides:
- Create two objects of Presentation class and load two presentation documents using Presentation.LoadFromFile() method.
- Get the slide master of the second presentation using Presentation.Masters.get_Item() method.
- Add the slide master to the first presentation using Presentation.Masters.AppendSlide() method.
- Apply the slide master to the slides in the second presentation through ISlide.Layout property.
- Save the first presentation using Presentation.SaveToFile() method.
- Python
from spire.presentation import * from spire.presentation.common import * # Create two objects of Presentation pres1 = Presentation() pres2 = Presentation() # Load two PowerPoint documents pres1.LoadFromFile("Sample.pptx") pres2.LoadFromFile("Template.pptx") # Get the slide master of the second presentation master = pres2.Masters.get_Item(0) # Add the slide master to the first presentation index = pres1.Masters.AppendSlide(master) # Apply the slide master to the first presentation pres1.Slides.get_Item(0).Layout = pres1.Masters.get_Item(index).Layouts.GetByType(SlideLayoutType.Title) for i in range(1, pres1.Slides.Count): pres1.Slides.get_Item(i).Layout = pres1.Masters.get_Item(index).Layouts.GetByType(SlideLayoutType.TitleAndObject) # Save the first presentation pres1.SaveToFile("output/CopySlideMaster.pptx", FileFormat.Pptx2013) pres1.Dispose() pres2.Dispose()
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.
Slide titles play a crucial role in PowerPoint presentations because they can assist the audience in quickly grasping the topics or key points of each slide. When working with PowerPoint documents, users often encounter the need to update or extract slide titles for various purposes. For example, they may need to modify titles to reflect new content or extract titles to perform tasks such as summarizing or analyzing presentation content. Knowing how to programmatically update or extract slide titles can greatly save time and effort, particularly when dealing with extensive presentations. In this article, we will demonstrate how to update and extract slide titles in PowerPoint PPTX or PPT documents in Python using Spire.Presentation for Python.
Install Spire.Presentation 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
Update Slide Titles in PowerPoint in Python
The title of a slide can be updated using the ISlide.Title property. The detailed steps are as follows.
- Create a Presentation instance.
- Load a PowerPoint PPTX or PPT document using Presentation.LoadFromFile() method.
- Get a specific slide of the document using Presentation.Slides[index] property.
- Update the title of the slide using ISlide.Title property.
- Save the result document using Presentation.SaveToFile() method.
- Python
from spire.presentation.common import * from spire.presentation import * # Create a Presentation object ppt = Presentation() # Load a PowerPoint document ppt.LoadFromFile("Water Of Life.pptx") # Get the second slide slide = ppt.Slides[1] # Update the title of the second slide slide.Title = "Updated Title" # Save the result document ppt.SaveToFile("UpdateSlideTitle.pptx", FileFormat.Pptx2016) ppt.Dispose()
Extract All Slide Titles from PowerPoint in Python
To extract all slide titles from a PowerPoint document, you first need to iterate through all slides in the document and all shapes on each slide. Then identify shapes with placeholder types like Title, CenteredTitle, or Subtitle. After that retrieve the text content from the identified shapes using the IAutoShape.TextFrame.Text property. The detailed steps are as follows.
- Create a Presentation instance.
- Load a PowerPoint PPTX or PPT document using Presentation.LoadFromFile() method.
- Create a list to store the extracted titles.
- Iterate through all slides in the document.
- For each slide, iterate through all shapes on it.
- Identify shapes with placeholder types such as Title, CenteredTitle, or Subtitle.
- Typecast the identified shapes to IAutoShape object.
- Retrieve the text content of the identified shapes using the IAutoShape.TextFrame.Text property and append them to the list.
- Save the content of the list to a text file.
- Python
from spire.presentation.common import * from spire.presentation import * # Load a PowerPoint document ppt = Presentation() ppt.LoadFromFile("Water of Life.pptx") # Create a list to store the extracted slide titles titles = [] # Iterate through all slides in the document for slide in ppt.Slides: # Iterate through all shapes on each slide for shape in slide.Shapes: # Find the shapes with placeholder types such as Title, CenteredTitle, or Subtitle if shape.Placeholder is not None and shape.Placeholder.Type in [PlaceholderType.Title, PlaceholderType.CenteredTitle, PlaceholderType.Subtitle]: # Typecast the shape to IautoShape object auto_shape = shape if isinstance(shape, IAutoShape) else None if auto_shape is not None: # Add the text of the shape to the titles list titles.append(auto_shape.TextFrame.Text) # Save the extracted slide titles to a text file with open("AllTitles.txt", "w") as file: file.write("Extracted titles:\n") file.write("\n".join(titles)) ppt.Dispose()
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.
Working with multiple PowerPoint presentations at the same time can be a daunting task, often resulting in a less-than-ideal presentation experience. However, there is a solution that can streamline this process and ensure seamless transitions throughout the presentation. By combining multiple PowerPoint files into a single cohesive presentation, presenters can eliminate the need to repeatedly open different files, saving time and effort. While manually copying slides can be arduous and time-consuming, Python offers a swift and efficient solution. This article is going to show how to leverage Spire.Presentation for Python to merge PowerPoint presentations effortlessly through Python programs.
- Merging PowerPoint Presentations and Retain Their Designs
- Merging PowerPoint Presentations with Consistent Design
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: How to Install Spire.Presentation for Python on Windows
Merging PowerPoint Presentations and Retain Their Designs
Merging PowerPoint presentations can be accomplished by reading slides from one presentation and adding them to another presentation. During the process of adding to the target presentation, developers can use Presentation.Slides.AppendBySlide(ISlide) method to add slides and retain the original design of the slides.
The detailed steps are as follows:
- Create two instances of Presentation class.
- Load two PowerPoint presentations using Presentation.LoadFromFile() method.
- Iterate through each slide in the second presentation and add them to the first presentation while keeping their design using Presentation.Slides.AppendBySlide() method.
- Save the first presentation using Presentation.SaveToFile() method.
- Python
from spire.presentation import * from spire.presentation.common import * # Create two instances of Presentation class pres1 = Presentation() pres2 = Presentation() # Load two presentation files pres1.LoadFromFile("Sample1.pptx") pres2.LoadFromFile("Sample2.pptx") # Iterate through the slides of the second presentation for slide in pres2.Slides: # Add each slides to the first presentation and keep the original design pres1.Slides.AppendBySlide(slide) # Save the first presentation pres1.SaveToFile("output/MergePresentations.pptx", FileFormat.Pptx2016) pres1.Dispose() pres2.Dispose()
Merging PowerPoint Presentations with Consistent Design
Developers can also use Presentation.Slides.AppendByMaster(slide Islide, master IMasterSlide) method to insert slides into the target presentation and change the design of the slides to the design of the target presentation. This allows for merging presentations and ensuring a consistent design.
The detailed stops are as follows:
- Create two instances of Presentation class.
- Load two PowerPoint presentations using Presentation.LoadFromFile() method.
- Iterate through each slide in the second presentation and add them to the first presentation while changing their design to the design of the first presentation using Presentation.Slides.AppendByMaster() method.
- Save the first presentation using Presentation.SaveToFile() method.
- Python
from spire.presentation import * from spire.presentation.common import * # Create two instances of Presentation class pres1 = Presentation() pres2 = Presentation() # Load two presentation files pres1.LoadFromFile("Sample1.pptx") pres2.LoadFromFile("Sample2.pptx") # Iterate through each slide in the second presentation for slide in pres2.Slides: # Add each slide to the first presentation pres1.Slides.AppendByMaster(slide, pres1.Masters[0]) # Save the first presentation pres1.SaveToFile("output/MergePresentationsDesign.pptx", FileFormat.Pptx2016) pres1.Dispose() pres2.Dispose()
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.
In PowerPoint, properly sized slides help make the document look professional. When giving presentations in different scenarios, adjusting slide sizes to match the aspect ratio of the projector or screen ensures an optimal viewing experience for all audience members, thus increasing engagement. In this article, you will learn how to change the slide size of a PowerPoint presentation in Python using Spire.Presentation for Python.
Install Spire.Presentation 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
Change the Slide Size to a Preset Size in Python
Spire.Presentation for Python provides the Presentation.SlideSize.Type property to set or change the slide size to a preset size. The following are the detailed steps.
- Create a Presentation instance.
- Load a PowerPoint presentation using Presentation.LoadFromFile() method.
- Change the slide size of the presentation using Presentation.SlideSize.Type property.
- Save the result document using Presentation.SaveToFile() method.
- Python
from spire.presentation import * # Create a Presentation instance presentation = Presentation() # Load a PowerPoint document presentation.LoadFromFile("sample.pptx") # Set or change the slide size presentation.SlideSize.Type = SlideSizeType.Screen4x3 # Save the result document presentation.SaveToFile("ChangeSlideSize.pptx", FileFormat.Pptx2016) presentation.Dispose()
Change the Slide Size to a Custom Size in Python
Customizing the size of slides requires changing the slide size type to Custom first, and then you can set a desired size through the Presentation.SlideSize.Size property. The following are the detailed steps.
- Create a Presentation instance.
- Load a PowerPoint presentation using Presentation.LoadFromFile() method.
- Change the slide size type to custom using Presentation.SlideSize.Type property.
- Customize the slide size using Presentation.SlideSize.Size property.
- Save the result document using Presentation.SaveToFile() method.
- Python
from spire.presentation import * # Create a Presentation instance presentation = Presentation() # Load a PowerPoint document presentation.LoadFromFile("sample.pptx") # Change the slide size type to custom presentation.SlideSize.Type = SlideSizeType.Custom # Set the slide size presentation.SlideSize.Size = SizeF(900.0,600.0) # Save the result document presentation.SaveToFile("CustomSlideSize.pptx", FileFormat.Pptx2016) presentation.Dispose()
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.
Python: Add, Read, or Remove Document Properties in PowerPoint
2024-02-23 07:10:20 Written by support iceblueManaging document properties in PowerPoint is an essential aspect of presentation creation. These properties serve as metadata that provides important information about the file, such as the author, subject, and keywords. By being able to add, retrieve, or remove document properties, users gain control over the organization and customization of their presentations. Whether it's adding relevant tags for easy categorization, accessing authorship details, or removing sensitive data, effectively managing document properties in PowerPoint ensures seamless collaboration and professionalism in your slide decks.
In this article, you will learn how to add, read, and remove document properties in a PowerPoint file in Python by using the Spire.Presentation for Python library.
- Add Document Properties to a PowerPoint File
- Read Document Properties of a PowerPoint File
- Remove Document Properties from a PowerPoint File
Install Spire.Presentation 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 commands.
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
Prerequisite Knowledge
Document properties can be divided into two types: standard document properties and custom document properties.
- Standard document properties are pre-defined properties that are commonly used across various PowerPoint presentations. Some examples of standard document properties include title, author, subject, keywords and company. Standard document properties are useful for providing general information and metadata about the presentation.
- Custom document properties are user-defined properties that allow you to add specific information to a PowerPoint presentation. Unlike standard document properties, custom properties are not predefined and can be tailored to suit your specific needs. Custom properties usually provide information relevant to your presentation that may not be covered by the default properties.
Spire.Presentation for Python offers the DocumentProperty class to work with both standard document properties and custom document properties. The standard document properties can be accessed using the properties like Title, Subject, Author, Manager, Company, etc. of the DocumentProperty class. To add or retrieve custom properties, you can use the set_Item() method and the GetPropertyName() method of the DocumentProperty class.
Add Document Properties to a PowerPoint File in Python
To add or change the standard document properties, you can assign values to the DocumentProperty.Title proerpty, DocumentProperty.Subject property and other similar properties. To add custom properties to a presentation, use the DocumentProperty.set_Item(name: str, value: SpireObject) method. The detailed steps are as follows.
- Create a Presentation object.
- Load a PowerPoint document using Presentation.LoadFromFile() method.
- Get the DocumentProperty object.
- Add standard document properties to the presentation by assigning values to the Title, Subject, Author, Manager, Company and Keywords properties of the object.
- Add custom properties to the presentation using set_Item() of the object.
- Save the presentation to a PPTX file using Presentation.SaveToFile() method.
- Python
from spire.presentation.common import * from spire.presentation import * # Create a Presentation object presentation = Presentation() # Load a PowerPoint document presentation.LoadFromFile("C:\\Users\\Administrator\\Desktop\\input.pptx") # Get the DocumentProperty object documentProperty = presentation.DocumentProperty # Set built-in document properties documentProperty.Title = "Annual Sales Presentation" documentProperty.Subject = "Company performance and sales strategy" documentProperty.Author = "John Smith" documentProperty.Manager = "Sarah Johnson" documentProperty.Company = "E-iceblue Corporation" documentProperty.Category = "Business" documentProperty.Keywords = "sales, strategy, performance" documentProperty.Comments = "Please review and provide feedback by Friday" # Add custom document properties documentProperty.set_Item("Document ID", Int32(12)) documentProperty.set_Item("Authorized by", String("Product Manager")) documentProperty.set_Item("Authorized Date", DateTime(2024, 1, 10, 0, 0, 0, 0)) # Save to file presentation.SaveToFile("output/Properties.pptx", FileFormat.Pptx2019) presentation.Dispose()
Read Document Properties of a PowerPoint File in Python
The DocumentProperty.Title and the similar properties are not only used to set standard properties but can return the values of standard properties as well. Since the name of a custom property is not constant, we need to get the name using the DocumentProperty.GetPropertyName(index: int) method. And then, we're able to get the property's value using the DocumentProperty.get_Item(name: str) method.
The steps to read document properties of a PowerPoint file are as follows.
- Create a Presentation object.
- Load a PowerPoint document using Presentation.LoadFromFile() method.
- Get the DocumentProperty object.
- Get the standard document properties by using the Title, Subject, Author, Manager, Company, and Keywords properties of the object.
- Get the count of the custom properties, and iterate through the custom properties.
- Get the name of a specific custom property by its index using DocumentProperty.GetPropertyName() method.
- Get the value of the property using DocumentProperty.get_Item() method.
- Python
from spire.presentation.common import * from spire.presentation import * # Create a Presentation object presentation = Presentation() # Load a PowerPoint document presentation.LoadFromFile("C:\\Users\\Administrator\\Desktop\\Properties.pptx") # Get the DocumentProperty object documentProperty = presentation.DocumentProperty # Get the built-in document properties print("Title: " + documentProperty.Title) print("Subject: " + documentProperty.Subject) print("Author: " + documentProperty.Author) print("Manager : " + documentProperty.Manager) print("Company: " + documentProperty.Company) print("Category: " + documentProperty.Category) print("Keywords: " + documentProperty.Keywords) print("Comments: " + documentProperty.Comments) # Get the count of the custom document properties count = documentProperty.Count # Iterate through the custom properties for i in range(count): # Get the name of a specific custom property customPropertyName = documentProperty.GetPropertyName(i) # Get the value of the custom property customPropertyValue = documentProperty.get_Item(customPropertyName) # Print the result print(customPropertyName + ": " + str(customPropertyValue))
Remove Document Properties from a PowerPoint File in Python
Removing a standard property means assigning an empty string to a property like DocumentProperty.Title. To remove the custom properties, Spire.Presentation provides the DocumentProperty.Remove(name: str) method. The following are the steps to remove document properties from a PowerPoint file in Python.
- Create a Presentation object.
- Load a PowerPoint document using Presentation.LoadFromFile() method.
- Get the DocumentProperty object.
- Set the Title, Subject, Author, Manager, Company, and Keywords properties of the object to empty strings.
- Get the count of the custom properties.
- Get the name of a specific custom property by its index using DocumentProperty.GetPropertyName() method.
- Remove the custom property using DocumentProperty.Remove() method.
- Save the presentation to a PPTX file using Presentation.SaveToFile() method.
- Python
from spire.presentation.common import * from spire.presentation import * # Create a Presentation object presentation = Presentation() # Load a PowerPoint document presentation.LoadFromFile("C:\\Users\\Administrator\\Desktop\\Properties.pptx") # Get the DocumentProperty object documentProperty = presentation.DocumentProperty # Set built-in document properties to empty strings documentProperty.Title = "" documentProperty.Subject = "" documentProperty.Author = "" documentProperty.Manager = "" documentProperty.Company = "" documentProperty.Category = "" documentProperty.Keywords = "" documentProperty.Comments = "" # Get the count of the custom document properties i = documentProperty.Count while i > 0: # Get the name of a specific custom property customPropertyName = documentProperty.GetPropertyName(i - 1) # Remove the custom property documentProperty.Remove(customPropertyName) i = i - 1 # Save the presentation to a different pptx file presentation.SaveToFile("Output/RemoveProperties.pptx",FileFormat.Pptx2019)
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.
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.
- Add a Section at the End of a PowerPoint
- Insert a Section Before a Specified Section
- Add a Section Before a Specified Slide in PowerPoint
- Remove a Section from a PowerPoint
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()
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()
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()
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()
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.