Extracting images from a PowerPoint presentation is necessary when you need to reuse them elsewhere. By doing so, you gain the flexibility to use these images outside the confines of the original presentation, thus maximizing their value in different projects. This article will demonstrate how to extract images from 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 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

Extract Images from a PowerPoint Document in Python

To extract images from an entire PowerPoint presentation, you need to use the Presentation.Images property to get the collection of all the images in the presentation, then iterate through the elements in the collection and call IImageData.Image.Save() method to save each element to an image file. The following are the detailed steps:

  • Create a Presentation instance.
  • Load a PowerPoint document using Presentation.LoadFromFile() method.
  • Get the collection of all the images in the document using Presentation.Images property.
  • Iterate through the elements in the collection, and save each element as an image file using the IImageData.Image.Save() method.
  • Python
from spire.presentation.common import *
from spire.presentation import *

# Create a Presentation instance
ppt = Presentation()

# Load a PowerPoint document
ppt.LoadFromFile("sample.pptx")

# Iterate through all images in the document
for i, image in enumerate(ppt.Images):

    # Extract the images
    ImageName = "ExtractImage/Images_"+str(i)+".png"
    image.Image.Save(ImageName)

ppt.Dispose()

Python: Extract Images from PowerPoint Presentations

Extract Images from a Presentation Slide in Python

To extract images from a specific slide, you need to iterate through all shapes on the slide and find the shapes that are of SlidePicture or PictureShape type, then use the SlidePicture.PictureFill.Picture.EmbedImage.Image.Save() or PictureShape.EmbedImage.Image.Save() method to save the images to image files. The following are the detailed steps:

  • Create a Presentation instance.
  • Load a PowerPoint document using Presentation.LoadFromFile() method.
  • Get a specified slide using Presentation.Slides[int] property.
  • Iterate through all shapes on the slide.
  • Determine whether the shapes are of SlidePicture or PictureShape type. If so, save the images to image files using SlidePicture.PictureFill.Picture.EmbedImage.Image.Save() or PictureShape.EmbedImage.Image.Save() method.
  • Python
from spire.presentation.common import *
from spire.presentation import *

# Create a Presentation instance
ppt = Presentation()

# Load a PowerPoint document
ppt.LoadFromFile("sample.pptx")

# Get a specified slide
slide = ppt.Slides[2]

i = 0
#Traverse all shapes in the slide
for s in slide.Shapes:

    # Determine if the shape is of SlidePicture type
    if isinstance(s, SlidePicture):

        # If yes, then extract the image
        ps = s if isinstance(s, SlidePicture) else None
        ps.PictureFill.Picture.EmbedImage.Image.Save("Output/SlidePic_"+str(i)+".png")
        i += 1

    # Determine if the shape is of PictureShape type
    if isinstance(s, PictureShape):

        # If yes, then extract the image
        ps = s if isinstance(s, PictureShape) else None
        ps.EmbedImage.Image.Save("Output/SlidePic_"+str(i)+".png")
        i += 1

ppt.Dispose()

Python: Extract Images from PowerPoint Presentations

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.

Proper row height and column width are essential for optimizing data readability and ensuring that all content is fully visible in Excel. While the default row height and column width settings may be suitable in some cases, they may not be sufficient when dealing with lengthy text, large numbers, or complex formulas. In such cases, it becomes necessary to set appropriate row heights and column widths. In this article, we will explain how to set row height and column width in Excel in Python using Spire.XLS for Python.

Install Spire.XLS for Python

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

pip install Spire.XLS

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

Set Row Height in Excel in Python

Spire.XLS for Python provides the Worksheet.SetRowHeight() method to set the height for a specific row.  The detailed steps are as follows:

  • Create a Workbook object.
  • Load an Excel file using Workbook.LoadFromFile() method.
  • Get a specific worksheet using Workbook.Worksheets[] property.
  • Set the height of a specific row using Worksheet.SetRowHeight() method.
  • Save the resulting file using Workbook.SaveToFile() method.
  • Python
from spire.xls import *
from spire.xls.common import *

# Create a Workbook object
workbook = Workbook()
# Load an Excel file
workbook.LoadFromFile("Sample.xlsx")

# Get the first worksheet
worksheet = workbook.Worksheets[0]

# Set the height of the first row
worksheet.SetRowHeight(1, 25)

# Save the resulting file
workbook.SaveToFile("SetRowHeight.xlsx", ExcelVersion.Version2016)
workbook.Dispose()

Python: Set Row Height and Column Width in Excel

Set Column Width in Excel in Python

To set the width of a specific column, you can use the Worksheet.SetColumnWidth() method. The detailed steps are as follows:

  • Create a Workbook object.
  • Load an Excel file using Workbook.LoadFromFile() method.
  • Get a specific worksheet using Workbook.Worksheets[] property.
  • Set the width of a specific column using Worksheet.SetColumnWidth() method.
  • Save the resulting file using Workbook.SaveToFile() method.
  • Python
from spire.xls import *
from spire.xls.common import *

# Create a Workbook object
workbook = Workbook()
# Load an Excel file
workbook.LoadFromFile("Sample.xlsx")

# Get the first worksheet
worksheet = workbook.Worksheets[0]

# Set the width of the third column
worksheet.SetColumnWidth(3, 15)

# Save the resulting file
workbook.SaveToFile("SetColumnWidth.xlsx", ExcelVersion.Version2016)
workbook.Dispose()

Python: Set Row Height and Column Width in Excel

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: Find and Replace Text in Word

2023-09-22 02:55:09 Written by Koohji

The Find and Replace feature in Word offers a reliable and efficient solution for updating text within your documents. It eliminates the need for exhaustive manual searching and editing by automatically locating and replacing the desired text throughout the entire document. This not only saves time but also guarantees that every instance of the targeted text is updated consistently. In this article, we will demonstrate how to find and replace text in a Word document in Python using Spire.Doc for Python.

Install Spire.Doc for Python

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

pip install Spire.Doc

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

Find Text and Replace All Its Instances with New Text

You can find a text and replace all its instances with another text easily using the Document.Replace() method. The detailed steps are as follows:

  • Create a Document object.
  • Load a Word document using Document.LoadFromFile() method.
  • Find a specific text and replace all its instances with another text using Document.Replace() method.
  • Save the resulting document using Document.SaveToFile() method.
  • Python
from spire.doc import *
from spire.doc.common import *

# Create a Document object
document = Document()
# Load a Word document
document.LoadFromFile("Sample.docx")

# Find a specific text and replace all its instances with another text
document.Replace("Spire.Doc", "Eiceblue", False, True)

# Save the resulting document
document.SaveToFile("ReplaceAllOccurrencesOfText.docx", FileFormat.Docx2016)
document.Close()

Python: Find and Replace Text in Word

Find Text and Replace Its First Instance with New Text

Spire.Doc for Python provides the Document.ReplaceFirst property which enables you to change the replacement mode from replacing all instances to replacing the first instance. The following steps explain how to find a text and replace its first instance in a Word document:

  • Create a Document object.
  • Load a Word document using Document.LoadFromFile() method.
  • Change the replacement mode to replace the first instance by setting the Document.ReplaceFirst property as True.
  • Replace the first instance of a text with another text using Document.Replace() method.
  • Save the resulting document using Document.SaveToFile() method.
  • Python
from spire.doc import *
from spire.doc.common import *

# Create a Document object
document = Document()
# Load a Word document
document.LoadFromFile("Sample.docx")

# Change the replacement mode to replace the first match
document.ReplaceFirst = True

# Replace the first instance of a text with another text
document.Replace("Spire.Doc", "Eiceblue", False, True)

# Save the resulting document
document.SaveToFile("ReplaceFirstOccurrenceOfText.docx", FileFormat.Docx2016)
document.Close()

Python: Find and Replace Text in Word

Find and Replace Text Using a Regular Expression

You can replace a text matching a regular expression with new text by passing a Regex object and the new text to the Document.Replace() method as parameters. The detailed steps are as follows:

  • Create a Document object.
  • Load a Word document using Document.LoadFromFile() method.
  • Create a Regex object to match the specific text.
  • Replace the text matching the regex with another text using Document.Replace() method.
  • Save the resulting document using Document.SaveToFile() method.
  • Python
from spire.doc import *
from spire.doc.common import *

# Create a Document object
document = Document()
# Load a Word document
document.LoadFromFile("Sample1.docx")

# Create a regex to match the text that starts with #
regex = Regex("""\\#\\w+\\b""")

# Find the text matching the regex and replace it with another text
document.Replace(regex, "Spire.Doc for Python")

#save the document
document.SaveToFile("ReplaceTextUsingRegex.docx", FileFormat.Docx2016)
document.Close()

Python: Find and Replace Text in Word

Find and Replace Text with an Image

Spire.Doc for Python doesn't offer a direct method to replace text with image, but you can achieve this by inserting the image at the position of the text and then removing the text from the document. The detailed steps are as follows:

  • Create a Document object.
  • Load a Word document using Document.LoadFromFile() method.
  • Find a specific text in the document using Document.FindAllString() method.
  • Loop through the found results.
  • Create a DocPicture object and load an image using DocPicture.LoadImage() method.
  • Get the found text as a single text range and then get the index of the text range in its owner paragraph.
  • Insert an image at the position of the text range and then remove the text range from the document.
  • Save the resulting document using Document.SaveToFile() method.
  • Python
from spire.doc import *
from spire.doc.common import *

# Create a Document object
document = Document()
# Load a Word document
document.LoadFromFile("Sample.docx")

# Find a specific text in the document
selections = document.FindAllString("Spire.Doc", True, True)
index = 0
testRange = None

# Loop through the found results
for selection in selections:
    # Load an image
    pic = DocPicture(document)
    pic.LoadImage("logo.png")
    # Get the found text as a single text range
    testRange = selection.GetAsOneRange()
    # Get the index of the text range in its owner paragraph
    index = testRange.OwnerParagraph.ChildObjects.IndexOf(testRange)
    # Insert an image at the index
    testRange.OwnerParagraph.ChildObjects.Insert(index, pic)
    # Remove the text range
    testRange.OwnerParagraph.ChildObjects.Remove(testRange)

# Save the resulting document
document.SaveToFile("ReplaceTextWithImage.docx", FileFormat.Docx2016)
document.Close()

Python: Find and Replace Text in Word

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.

page 73