Python: Remove Backgrounds from PowerPoint Slide or Slide Masters
A 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.
Python: Copy Slides in PowerPoint Presentations
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.
Python: Rearrange Slides in a PowerPoint Document
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
Slide 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.
Python: Add, Modify, or Remove Footers from PowerPoint Documents
In a PowerPoint document, the footer is an area located at the bottom of each slide, typically containing textual information such as page numbers, dates, authors, and more. By adding a footer, you can give your slides a professional look and provide important information. Modifying the footer allows you to adjust the displayed content, style, and position to meet specific needs or styles. Removing the footer can clear the bottom content when extra information is not needed or to maintain a clean appearance. This article will introduce how to use Spire.Presentation for Python to add, modify, or remove footers in PowerPoint documents within a Python project.
- Python Add Footers in PowerPoint Documents
- Python Modify Footers in PowerPoint Documents
- Python Remove Footers in PowerPoint Documents
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
Python Add Footers in PowerPoint Documents
Using Spire.Presentation, you can add footers, page numbers, and time information to the bottom of each page in a PowerPoint document, ensuring consistent footer content across all pages. Here are the detailed steps:
- Create a Presentation object.
- Load a PowerPoint document using the Presentation.LoadFromFile() method.
- Set the footer visible using Presentation.FooterVisible = true and set the footer text.
- Set the slide number visible using Presentation.SlideNumberVisible = true, iterate through each slide, check for the lisence of a page number placeholder, and modify the text to the "Page X" format if found.
- Set the date visible using Presentation.DateTimeVisible = true.
- Set the format of the date using the Presentation.SetDateTime() method.
- Save the document using the Presentation.SaveToFile() method.
- Python
from spire.presentation.common import * from spire.presentation import * # Create a Presentation object presentation = Presentation() # Load the presentation from a file presentation.LoadFromFile("Sample1.pptx") # Set the footer visible presentation.FooterVisible = True # Set the footer text to "Spire.Presentation" presentation.SetFooterText("Spire.Presentation") # Set the slide number visible presentation.SlideNumberVisible = True # Iterate through each slide in the presentation for slide in presentation.Slides: for shape in slide.Shapes: if shape.IsPlaceholder: # If it is a slide number placeholder if shape.Placeholder.Type == PlaceholderType.SlideNumber: autoShape = shape if isinstance(shape, IAutoShape) else None if autoShape is not None: text = autoShape.TextFrame.TextRange.Paragraph.Text # Modify the slide number text to "Page X" autoShape.TextFrame.TextRange.Paragraph.Text = "Page " + text # Set the date and time visible presentation.DateTimeVisible = True # Set the date and time format presentation.SetDateTime(DateTime.get_Now(), "MM/dd/yyyy") # Save the modified presentation to a file presentation.SaveToFile("AddFooter.pptx", FileFormat.Pptx2016) # Dispose of the Presentation object resources presentation.Dispose()
Python Modify Footers in PowerPoint Documents
To modify the footer in a PowerPoint document, you first need to inspect the elements of each slide to locate footer and page number placeholders. Then, for each type of placeholder, set the desired content and format to ensure consistent and compliant footers throughout the document. Here are the detailed steps:
- Create a Presentation object.
- Load a PowerPoint document using the Presentation.LoadFromFile() method.
- Use the Presentation.Slides[index] property to retrieve a slide.
- Iterate through the shapes in the slide using a for loop, check each shape to determine if it is a placeholder such as a footer or page number placeholder, and then modify its content or format accordingly.
- Save the document using the Presentation.SaveToFile() method.
- Python
from spire.presentation.common import * from spire.presentation import * def change_font(paragraph): for textRange in paragraph.TextRanges: # Set the text style to italic textRange.IsItalic = TriState.TTrue # Set the text font textRange.EastAsianFont = TextFont("Times New Roman") # Set the text font size to 12 textRange.FontHeight = 34 # Set the text color textRange.Fill.FillType = FillFormatType.Solid textRange.Fill.SolidColor.Color = Color.get_SkyBlue() # Create a Presentation object presentation = Presentation() # Load a presentation from a file presentation.LoadFromFile("Sample2.pptx") # Get the first slide slide = presentation.Slides[0] # Iterate through the shapes on the slide for shape in slide.Shapes: # Check if the shape is a placeholder if shape.Placeholder is not None: # Get the placeholder type type = shape.Placeholder.Type # If it is a footer placeholder if type == PlaceholderType.Footer: # Convert the shape to IAutoShape type autoShape = shape if isinstance(shape, IAutoShape) else None if autoShape is not None: # Set the text content to "E-ICEBLUE" autoShape.TextFrame.Text = "E-ICEBLUE" # Modify the text font change_font(autoShape.TextFrame.Paragraphs[0]) # If it is a slide number placeholder if type == PlaceholderType.SlideNumber: # Convert the shape to IAutoShape type autoShape = shape if isinstance(shape, IAutoShape) else None if autoShape is not None: # Modify the text font change_font(autoShape.TextFrame.Paragraphs[0]) # Save the modified presentation to a file presentation.SaveToFile("ModifiedFooter.pptx", FileFormat.Pptx2016) # Release the resources of the Presentation object presentation.Dispose()
Python Remove Footers in PowerPoint Documents
To delete footers in a PowerPoint document, you first need to locate placeholders such as footers, page numbers, and time in the slides, and then remove them from the collection of shapes in the slide to ensure complete removal of footer content. Here are the detailed steps:
- Create a Presentation object.
- Load a PowerPoint document using the Presentation.LoadFromFile() method.
- Use the Presentation.Slides[index] property to retrieve a slide.
- Iterate through the shapes in the slide using a for loop, check if they are placeholders, and if they are footer placeholders, page number placeholders, or time placeholders, remove them from the slide.
- Save the document using the Presentation.SaveToFile() method.
- Python
from spire.presentation.common import * from spire.presentation import * # Create a Presentation object presentation = Presentation() # Load a presentation from a file presentation.LoadFromFile("Sample2.pptx") # Get the first slide slide = presentation.Slides[0] # Iterate through the shapes on the slide for i in range(len(slide.Shapes) - 1, -1, -1): # Check if the shape is a placeholder if slide.Shapes[i].Placeholder is not None: # Get the placeholder type type = slide.Shapes[i].Placeholder.Type # If it is a footer placeholder if type == PlaceholderType.Footer: # Remove it from the slide slide.Shapes.RemoveAt(i) # If it is a slide number placeholder if type == PlaceholderType.SlideNumber: # Remove it from the slide slide.Shapes.RemoveAt(i) # If it is a date and time placeholder if type == PlaceholderType.DateAndTime: # Remove it from the slide slide.Shapes.RemoveAt(i) # Save the modified presentation to a file presentation.SaveToFile("RemovedFooter.pptx", FileFormat.Pptx2016) # Release the resources 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.
Python: Update or Extract Slide Titles in PowerPoint
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.
Python: Merge PowerPoint Presentations
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.
Python: Change Slide Size in PowerPoint
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
Managing 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.
Python: Add or Remove Sections in PowerPoint
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.