Spire.Office Knowledgebase Page 29 | E-iceblue

Python: Create a Radar Chart in Excel

2024-12-09 01:10:17 Written by Koohji

A radar chart, also known as a spider chart, is a graphical method of displaying multivariate data in two dimensions. Each spoke on the chart represents a different variable, and data points are plotted along these spokes. Radar charts are particularly useful for comparing the performance of different entities across several criteria. This article will demonstrate how to create a radar chart 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

Create a Simple Radar Chart in Excel in Python

Spire.XLS for Python provides the Worksheet.Charts.Add(ExcelChartType.Radar) method to add a standard radar chart to an Excel worksheet. The following are the detailed steps:

  • Create a Workbook instance.
  • Get a specified worksheet using Workbook.Worksheets[] property.
  • Add the chart data to specified cells and set the cell styles.
  • Add a simple radar chart to the worksheet using Worksheet.Charts.Add(ExcelChartType.Radar) method.
  • Set data range for the chart using Chart.DataRange property.
  • Set the position, legend and title of the chart.
  • Save the result file using Workbook.SaveToFile() method.
  • Python
*from spire.xls import *
from spire.xls.common import *

# Create a Workbook instance
workbook = Workbook()

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

# Add chart data to specified cells
sheet.Range["A1"].Value = "Rating"
sheet.Range["A2"].Value = "Communication"
sheet.Range["A3"].Value = "Experience"
sheet.Range["A4"].Value = "Work Efficiency"
sheet.Range["A5"].Value = "Leadership"
sheet.Range["A6"].Value = "Problem-solving"
sheet.Range["A7"].Value = "Teamwork"

sheet.Range["B1"].Value = "Jonathan"
sheet.Range["B2"].NumberValue = 4
sheet.Range["B3"].NumberValue = 3
sheet.Range["B4"].NumberValue = 4
sheet.Range["B5"].NumberValue = 3
sheet.Range["B6"].NumberValue = 5
sheet.Range["B7"].NumberValue = 5

sheet.Range["C1"].Value = "Ryan"
sheet.Range["C2"].NumberValue = 2
sheet.Range["C3"].NumberValue = 5
sheet.Range["C4"].NumberValue = 4
sheet.Range["C5"].NumberValue = 4
sheet.Range["C6"].NumberValue = 3
sheet.Range["C7"].NumberValue = 3

# Set font styles
sheet.Range["A1:C1"].Style.Font.IsBold = True
sheet.Range["A1:C1"].Style.Font.Size = 11
sheet.Range["A1:C1"].Style.Font.Color = Color.get_White()

# Set row height and column width 
sheet.Rows[0].RowHeight = 20
sheet.Range["A1:C7"].Columns[0].ColumnWidth = 15

# Set cell styles
sheet.Range["A1:C1"].Style.Color = Color.get_DarkBlue()
sheet.Range["A2:C7"].Borders[BordersLineType.EdgeBottom].LineStyle = LineStyleType.Thin
sheet.Range["A2:C7"].Style.Borders[BordersLineType.EdgeBottom].Color = Color.get_DarkBlue()
sheet.Range["B1:C7"].HorizontalAlignment = HorizontalAlignType.Center
sheet.Range["A1:C7"].VerticalAlignment = VerticalAlignType.Center

# Add a radar chart to the worksheet
chart = sheet.Charts.Add(ExcelChartType.Radar)

# Set position of chart
chart.LeftColumn = 4
chart.TopRow = 4
chart.RightColumn = 14
chart.BottomRow = 29

# Set data range for the chart
chart.DataRange = sheet.Range["A1:C7"]
chart.SeriesDataFromRange = False

# Set and format chart title
chart.ChartTitle = "Employee Performance Appraisal"
chart.ChartTitleArea.IsBold = True
chart.ChartTitleArea.Size = 14
chart.PlotArea.Fill.Visible = False
chart.Legend.Position = LegendPositionType.Corner

# Save the result file
workbook.SaveToFile("CreateRadarChart.xlsx", ExcelVersion.Version2016)
workbook.Dispose()

A radar chart is added to Excel to show the employee performance appraisal

Create a Filled Radar Chart in Excel in Python

A filled radar chart is a variation of a standard radar chart, with the difference that the area between each data point is filled with color. The following are the steps to create a filled radar chart using Python:

  • Create a Workbook instance.
  • Get a specified worksheet using Workbook.Worksheets[] property.
  • Add the chart data to specified cells and set the cell styles.
  • Add a filled radar chart to the worksheet using Worksheet.Charts.Add(ExcelChartType.RadarFilled) method.
  • Set data range for the chart using Chart.DataRange property.
  • Set the position, legend and title of the chart.
  • Save the result file using Workbook.SaveToFile() method.
  • Python
from spire.xls import *
from spire.xls.common import *

# Create a Workbook instance
workbook = Workbook()

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

# Add chart data to specified cells
sheet.Range["A1"].Value = "Rating"
sheet.Range["A2"].Value = "Communication"
sheet.Range["A3"].Value = "Experience"
sheet.Range["A4"].Value = "Work Efficiency"
sheet.Range["A5"].Value = "Leadership"
sheet.Range["A6"].Value = "Problem-solving"
sheet.Range["A7"].Value = "Teamwork"

sheet.Range["B1"].Value = "Jonathan"
sheet.Range["B2"].NumberValue = 4
sheet.Range["B3"].NumberValue = 3
sheet.Range["B4"].NumberValue = 4
sheet.Range["B5"].NumberValue = 3
sheet.Range["B6"].NumberValue = 5
sheet.Range["B7"].NumberValue = 5

sheet.Range["C1"].Value = "Ryan"
sheet.Range["C2"].NumberValue = 2
sheet.Range["C3"].NumberValue = 5
sheet.Range["C4"].NumberValue = 4
sheet.Range["C5"].NumberValue = 4
sheet.Range["C6"].NumberValue = 3
sheet.Range["C7"].NumberValue = 3

# Set font styles
sheet.Range["A1:C1"].Style.Font.IsBold = True
sheet.Range["A1:C1"].Style.Font.Size = 11
sheet.Range["A1:C1"].Style.Font.Color = Color.get_White()

# Set row height and column width 
sheet.Rows[0].RowHeight = 20
sheet.Range["A1:C7"].Columns[0].ColumnWidth = 15

# Set cell styles
sheet.Range["A1:C1"].Style.Color = Color.get_DarkBlue()
sheet.Range["A2:C7"].Borders[BordersLineType.EdgeBottom].LineStyle = LineStyleType.Thin
sheet.Range["A2:C7"].Style.Borders[BordersLineType.EdgeBottom].Color = Color.get_DarkBlue()
sheet.Range["B1:C7"].HorizontalAlignment = HorizontalAlignType.Center
sheet.Range["A1:C7"].VerticalAlignment = VerticalAlignType.Center

# Add a filled radar chart to the worksheet
chart = sheet.Charts.Add(ExcelChartType.RadarFilled)

# Set position of chart
chart.LeftColumn = 4
chart.TopRow = 4
chart.RightColumn = 14
chart.BottomRow = 29

# Set data range for the chart
chart.DataRange = sheet.Range["A1:C7"]
chart.SeriesDataFromRange = False

# Set and format chart title
chart.ChartTitle = "Employee Performance Appraisal"
chart.ChartTitleArea.IsBold = True
chart.ChartTitleArea.Size = 14
chart.PlotArea.Fill.Visible = False
chart.Legend.Position = LegendPositionType.Corner

# Save the result file
workbook.SaveToFile("FilledRadarChart.xlsx", ExcelVersion.Version2016)
workbook.Dispose()

Create a filled radar chart in Excel to show the employee performance appraisal

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.

Java: Set Page Setup Options in Excel

2024-12-06 03:22:51 Written by Koohji

Setting up the page layout in Excel is an important step to make your worksheets look polished and professional. Whether you’re printing a report or sharing it digitally, customizing options like margins, orientation, paper size, and scaling helps ensure your data is presented clearly and effectively. In this article, you will learn how to programmatically set page setup options in Excel in Java using Spire.XLS for Java.

Install Spire.XLS for Java

First of all, you're required to add the Spire.Xls.jar file as a dependency in your Java program. The JAR file can be downloaded from this link. If you use Maven, you can easily import the JAR file in your application by adding the following code to your project's pom.xml file.

<repositories>
    <repository>
        <id>com.e-iceblue</id>
        <name>e-iceblue</name>
        <url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
    </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>e-iceblue</groupId>
        <artifactId>spire.xls</artifactId>
        <version>16.4.1</version>
    </dependency>
</dependencies>

Set Page Margins in Excel in Java

The PageSetup class in Spire.XLS for Java allows you to customize page setup options for Excel worksheets. It provides methods like setTopMargin(), setBottomMargin(), setLeftMargin(), setRightMargin(), setHeaderMarginInch(), and setFooterMarginInch(), enabling you to adjust the top, bottom, left, right, header, and footer margins of a worksheet. The detailed steps are as follows.

  • Create an object of the Workbook class.
  • Load an Excel file using the Workbook.loadFromFile() method.
  • Get a specific worksheet using the Workbook.getWorksheets().get(index) method.
  • Access the PageSetup object of the worksheet using the Worksheet.getPageSetup() method.
  • Set the top, bottom, left, right, header, and footer margins using the PageSetup.setTopMargin(), PageSetup.setBottomMargin(), PageSetup.setLeftMargin(), PageSetup.setRightMargin(), PageSetup.setHeaderMarginInch(), and PageSetup.setFooterMarginInch() methods.
  • Save the modified workbook to a new file using the Workbook.saveToFile() method.
  • Java
import com.spire.xls.ExcelVersion;
import com.spire.xls.PageSetup;
import com.spire.xls.Workbook;
import com.spire.xls.Worksheet;

public class PageMargins {
    public static void main(String[] args) {
        // Create a Workbook object
        Workbook workbook = new Workbook();
        // Load an Excel file
        workbook.loadFromFile("Sample.xlsx");

        // Get the first worksheet
        Worksheet sheet = workbook.getWorksheets().get(0);
        
        // Get the PageSetup object of the worksheet
        PageSetup pageSetup = sheet.getPageSetup();

        // Set top, bottom, left, and right margins for the worksheet
        // The measure of the unit is Inch (1 inch = 2.54 cm)
        pageSetup.setTopMargin(1);
        pageSetup.setBottomMargin(1);
        pageSetup.setLeftMargin(1);
        pageSetup.setRightMargin(1);
        pageSetup.setHeaderMarginInch(1);
        pageSetup.setFooterMarginInch(1);

        // Save the modified workbook to a new file
        workbook.saveToFile("SetPageMargins.xlsx", ExcelVersion.Version2016);
        workbook.dispose();
    }
}

Set Page Margins in Excel in Java

Set Page Orientation in Excel in Java

The PageSetup.setOrientation() method allows you to specify the page orientation for printing. You can choose between two options: portrait mode or landscape mode. The detailed steps are as follows.

  • Create an object of the Workbook class.
  • Load an Excel file using the Workbook.loadFromFile() method.
  • Get a specific worksheet using the Workbook.getWorksheets().get(index) method.
  • Access the PageSetup object of the worksheet using the Worksheet.getPageSetup() method.
  • Set the page orientation using the PageSetup.setOrientation() method.
  • Save the modified workbook to a new file using the Workbook.saveToFile() method.
  • Java
import com.spire.xls.*;

public class PageOrientation {
    public static void main(String[] args) {
        // Create a Workbook object
        Workbook workbook = new Workbook();
        // Load an Excel file
        workbook.loadFromFile("Sample.xlsx");

        // Get the first worksheet
        Worksheet sheet = workbook.getWorksheets().get(0);

        // Get the PageSetup object of the worksheet
        PageSetup pageSetup = sheet.getPageSetup();

        pageSetup.setOrientation(PageOrientationType.Landscape);

        // Save the modified workbook to a new file
        workbook.saveToFile("SetPageOrientation.xlsx", ExcelVersion.Version2016);
        workbook.dispose();
    }
}

Set Page Orientation in Excel in Java

Set Paper Size in Excel in Java

The PageSetup.setPaperSize() method enables you to select from a variety of paper sizes for printing your worksheet. These options include A3, A4, A5, B4, B5, letter, legal, tabloid, and more. The detailed steps are as follows.

  • Create an object of the Workbook class.
  • Load an Excel file using the Workbook.loadFromFile() method.
  • Get a specific worksheet using the Workbook.getWorksheets().get(index) method.
  • Access the PageSetup object of the worksheet using the Worksheet.getPageSetup() method.
  • Set the paper size using the PageSetup.setPaperSize() method.
  • Save the modified workbook to a new file using the Workbook.saveToFile() method.
  • Java
import com.spire.xls.*;

public class PaperSize {
    public static void main(String[] args) {
        // Create a Workbook object
        Workbook workbook = new Workbook();
        // Load an Excel file
        workbook.loadFromFile("Sample.xlsx");

        // Get the first worksheet
        Worksheet sheet = workbook.getWorksheets().get(0);

        // Get the PageSetup object of the worksheet
        PageSetup pageSetup = sheet.getPageSetup();

        // Set the paper size to A4
        pageSetup.setPaperSize(PaperSizeType.PaperA4);
        
        // Save the modified workbook to a new file
        workbook.saveToFile("SetPaperSize.xlsx", ExcelVersion.Version2016);
        workbook.dispose();
    }
}

Set Paper Size in Excel in Java

Set Print Area in Excel in Java

You can define the specific area to be printed by using the PageSetup.setPrintArea() method. The detailed steps are as follows.

  • Create an object of the Workbook class.
  • Load an Excel file using the Workbook.loadFromFile() method.
  • Get a specific worksheet using the Workbook.getWorksheets().get(index) method.
  • Access the PageSetup object of the worksheet using the Worksheet.getPageSetup() method.
  • Set the print area using the PageSetup.setPringArea() method.
  • Save the modified workbook to a new file using the Workbook.saveToFile() method.
  • Java
import com.spire.xls.ExcelVersion;
import com.spire.xls.PageSetup;
import com.spire.xls.Workbook;
import com.spire.xls.Worksheet;

public class PrintArea {
    public static void main(String[] args) {
        // Create a Workbook object
        Workbook workbook = new Workbook();
        // Load an Excel file
        workbook.loadFromFile("Sample.xlsx");

        // Get the first worksheet
        Worksheet sheet = workbook.getWorksheets().get(0);

        // Get the PageSetup object of the worksheet
        PageSetup pageSetup = sheet.getPageSetup();
        
        // Set the print area of the worksheet to "A1:E5"
        pageSetup.setPrintArea("A1:E5");

        // Save the modified workbook to a new file
        workbook.saveToFile("SetPrintArea.xlsx", ExcelVersion.Version2016);
        workbook.dispose();
    }
}

Set Print Area in Excel in Java

Set Scaling Factor in Excel in Java

To scale the content of your worksheet to a specific percentage of its original size, you can use the PageSetup.setZoom() method. The detailed steps are as follows.

  • Create an object of the Workbook class.
  • Load an Excel file using the Workbook.loadFromFile() method.
  • Get a specific worksheet using the Workbook.getWorksheets().get(index) method.
  • Access the PageSetup object of the worksheet using the Worksheet.getPageSetup() method.
  • Set the scaling factor using the PageSetup.setZoom() method.
  • Save the modified workbook to a new file using the Workbook.saveToFile() method.
  • Java
import com.spire.xls.ExcelVersion;
import com.spire.xls.PageSetup;
import com.spire.xls.Workbook;
import com.spire.xls.Worksheet;

public class ScalingFactor {
    public static void main(String[] args) {
        // Create a Workbook object
        Workbook workbook = new Workbook();
        // Load an Excel file
        workbook.loadFromFile("Sample.xlsx");

        // Get the first worksheet
        Worksheet sheet = workbook.getWorksheets().get(0);

        // Get the PageSetup object of the worksheet
        PageSetup pageSetup = sheet.getPageSetup();
        
        // Set the scaling factor of the worksheet to 90%
        pageSetup.setZoom(90);

        // Save the modified workbook to a new file
        workbook.saveToFile("SetScalingFactor.xlsx", ExcelVersion.Version2016);
        workbook.dispose();
    }
}

Set Scaling Factor in Excel in Java

Set FitToPages Options in Excel in Java

Spire.XLS also provides the ability to adjust your worksheet content to fit a specific number of pages by using the PageSetup.setFitToPagesTall() and PageSetup.setFitToPagesWide() methods. The detailed steps are as follows.

  • Create an object of the Workbook class.
  • Load an Excel file using the Workbook.loadFromFile() method.
  • Get a specific worksheet using the Workbook.getWorksheets().get(index) method.
  • Access the PageSetup object of the worksheet using the Worksheet.getPageSetup() method.
  • Fit the content of the worksheet to one page using the PageSetup.setFitToPagesTall() and PageSetup.setFitToPagesWide() methods.
  • Save the modified workbook to a new file using the Workbook.saveToFile() method.
  • Java
import com.spire.xls.ExcelVersion;
import com.spire.xls.PageSetup;
import com.spire.xls.Workbook;
import com.spire.xls.Worksheet;

public class FitToPages {
    public static void main(String[] args) {
        // Create a Workbook object
        Workbook workbook = new Workbook();
        // Load an Excel file
        workbook.loadFromFile("Sample.xlsx");

        // Get the first worksheet
        Worksheet sheet = workbook.getWorksheets().get(0);

        // Get the PageSetup object of the worksheet
        PageSetup pageSetup = sheet.getPageSetup();

        // Fit the content of the worksheet within one page vertically (i.e., all rows will fit on a single page)
        pageSetup.setFitToPagesTall(1);
        // Fit the content of the worksheet within one page horizontally (i.e., all columns will fit on a single page)
        pageSetup.setFitToPagesWide(1);

        // Save the modified workbook to a new file
        workbook.saveToFile("FitToPages.xlsx", ExcelVersion.Version2016);
        workbook.dispose();
    }
}

Set FitToPages Options in Excel in Java

Set Headers and Footers in Excel in Java

For instructions on setting headers and footers in Excel, please refer to this article: Java: Add Headers and Footers to 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.

Incorporating external content into PowerPoint presentations can significantly enhance their impact and relevance. OLE (Object Linking and Embedding) objects provide an efficient way to embed or link various types of external files, such as Excel spreadsheets, Word documents, and PDF files, directly into PowerPoint slides. This functionality not only allows for seamless integration of dynamic data but also enables users to maintain a live connection to the original files. In this article, we will introduce how to insert, extract, or remove OLE objects in PowerPoint presentations 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

Insert OLE Objects into a PowerPoint Presentation in Python

Spire.Presentation for Python provides the ISlide.Shapes.AppendOleObject() method, which allows you to insert various external files (such as Word documents, Excel spreadsheets, PDF files, PowerPoint presentations, and ZIP archives) as OLE objects into PowerPoint slides. The detailed steps are as follows.

  • Create an object of the Presentation class.
  • Access the desired slide using the Presentation.Slides[index] property.
  • Add an OLE object to the slide using the ISlide.Shapes.AppendOleObject() method.
  • Set the icon for the OLE object using the IOleObject.SubstituteImagePictureFillFormat.Picture.EmbedImage property.
  • Specify the object type using the IOleObject.ProgId property.
  • Save the presentation using the Presentation.SaveToFile() method.
  • Python
from spire.presentation.common import *
from spire.presentation import *

# Create an object of the Presentation class
ppt = Presentation()

try:
    # Get the first slide
    slide = ppt.Slides[0]

    # Initialize the top position for the first object
    currentTop = 60
    # Spacing between OLE objects
    verticalSpacing = 20  

    # Add an Excel icon to the presentation
    excelImageStream = Stream("icons/excel-icon.png")
    oleImage = ppt.Images.AppendStream(excelImageStream)
    # Define the position of the Excel OLE object
    excelRec = RectangleF.FromLTRB(100, currentTop, oleImage.Width + 100, currentTop + oleImage.Height)
    # Add an Excel file to the slide as an OLE object
    oleStream = Stream("Budget.xlsx")
    oleObject = slide.Shapes.AppendOleObject("excel", oleStream, excelRec)
    oleObject.SubstituteImagePictureFillFormat.Picture.EmbedImage = oleImage
    oleObject.ProgId = "Excel.Sheet.12"

    # Update the top position for the next object
    currentTop += oleImage.Height + verticalSpacing

    # Add a Word icon to the presentation
    wordImageStream = Stream("icons/word-icon.png")
    wordOleImage = ppt.Images.AppendStream(wordImageStream)
    # Define the position of the Word OLE object
    wordRec = RectangleF.FromLTRB(100, currentTop, wordOleImage.Width + 100, currentTop + wordOleImage.Height)
    # Add a Word file to the slide as an OLE object
    wordStream = Stream("Document.docx")
    wordOleObject = slide.Shapes.AppendOleObject("word", wordStream, wordRec)
    wordOleObject.SubstituteImagePictureFillFormat.Picture.EmbedImage = wordOleImage
    wordOleObject.ProgId = "Word.Document.12"

    # Update the top position for the next object
    currentTop += wordOleImage.Height + verticalSpacing

    # Add a PDF icon to the presentation
    pdfImageStream = Stream("icons/pdf-icon.png")
    pdfOleImage = ppt.Images.AppendStream(pdfImageStream)
    # Define the position of the PDF OLE object
    pdfRec = RectangleF.FromLTRB(100, currentTop, pdfOleImage.Width + 100, currentTop + pdfOleImage.Height)
    # Add a PDF file to the slide as an OLE object
    pdfStream = Stream("Report.pdf")
    pdfOleObject = slide.Shapes.AppendOleObject("pdf", pdfStream, pdfRec)
    pdfOleObject.SubstituteImagePictureFillFormat.Picture.EmbedImage = pdfOleImage
    pdfOleObject.ProgId = "Acrobat.Document"

    # Update the top position for the next object
    currentTop += pdfOleImage.Height + verticalSpacing

    # Add a zip package icon to the presentation
    zipImageStream = Stream("icons/zip-icon.png")
    zipOleImage = ppt.Images.AppendStream(zipImageStream)
    # Define the position of the zip package OLE object
    zipRec = RectangleF.FromLTRB(100, currentTop, zipOleImage.Width + 100, currentTop + zipOleImage.Height)
    # Add a zip file to the slide as an OLE object
    zipOleStream = Stream("Example.zip")
    zipOleObject = slide.Shapes.AppendOleObject("zip", zipOleStream, zipRec)
    zipOleObject.ProgId = "Package"
    zipOleObject.SubstituteImagePictureFillFormat.Picture.EmbedImage = zipOleImage

    # Save the PowerPoint presentation
    ppt.SaveToFile("AddOLEObjects.pptx", FileFormat.Pptx2010)

finally:
    excelImageStream.Close()
    oleStream.Close()
    wordImageStream.Close()
    wordStream.Close()
    pdfImageStream.Close()
    pdfStream.Close()
    zipImageStream.Close()
    zipOleStream.Close()
    ppt.Dispose()

Insert OLE Objects into a PowerPoint Presentation in Python

Extract OLE Objects from a PowerPoint Presentation in Python

Spire.Presentation for Python enables you to extract the embedded OLE objects from a PowerPoint presentation and save them for further use. The detailed steps are as follows.

  • Create an object of the Presentation class.
  • Load a PowerPoint presentation using the Presentation.LoadFromFile() method.
  • Iterate through all slides in the presentation and all shapes on each slide.
  • Check if the shape is an OLE object.
  • Get the data of the OLE object using the IOleObject.Data property.
  • Identify the type of the OLE object using the IOleObject.ProgId property and save the OLE object data to its original format.
  • Python
from spire.presentation.common import *
from spire.presentation import *

# Create an object of the Presentation class
presentation = Presentation()

try:
    # Load the PowerPoint presentation
    presentation.LoadFromFile("AddOLEObjects.pptx")

    # Define output file paths for different types of OLE objects
    output_files = {
        "Acrobat.Document": "ExtractedOLEs/ExtractOLEObject.pdf",  
        "Excel.Sheet.12": "ExtractedOLEs/ExtractOLEObject.xlsx",  
        "Word.Document.12": "ExtractedOLEs/ExtractOLEObject.docx",
        "Package": "ExtractedOLEs/ExtractOLEObject.zip"
    }

    # Iterate through each slide in the presentation
    for slide in presentation.Slides:
        # Iterate through each shape in the slide
        for shape in slide.Shapes:
            # Check if the shape is an OLE object
            if isinstance(shape, IOleObject):
                ole_object = shape
                # Retrieve the data of the OLE object
                ole_data_stream = ole_object.Data

                # Determine the appropriate output file based on the OLE object's ProgId
                output_file = output_files.get(ole_object.ProgId)
                if output_file:
                    # Save the OLE object data to the corresponding output file
                    ole_data_stream.Save(output_file)
                    # Close stream
                    ole_data_stream.Close()

finally:
    presentation.Dispose()

Extract OLE Objects from a PowerPoint Presentation in Python

Remove OLE Objects from a PowerPoint Presentation in Python

If you need to remove unwanted OLE objects from a PowerPoint presentation to streamline your slides, you can use the ISlide.Shapes.Remove() method. The detailed steps are as follows.

  • Create an object of the Presentation class.
  • Load a PowerPoint presentation using the Presentation.LoadFromFile() method.
  • Iterate through all slides in the presentation and all shapes on each slide.
  • Check if the shape is an OLE object.
  • Store the OLE objects in a list and then remove them from the slide using the ISlide.Shapes.Remove() method.
  • Save the presentation using the Presentation.SaveToFile() method.
  • Python
from spire.presentation.common import *
from spire.presentation import *

# Create an object of the Presentation class
presentation = Presentation()

try:
    # Load the PowerPoint presentation
    presentation.LoadFromFile("AddOLEObjects.pptx")

    # Iterate through each slide in the presentation
    for slide in presentation.Slides:
        # Create a list to store shapes that are OLE objects
        ole_shapes = []

        # Iterate through each shape in the slide
        for shape in slide.Shapes:
            # Check if the shape is an OLE object
            if isinstance(shape, IOleObject):
                ole_shapes.append(shape)

        # Remove all OLE objects from the slide
        for ole_object in ole_shapes:
            slide.Shapes.Remove(ole_object)

    # Save the modified PowerPoint presentation
    presentation.SaveToFile("RemoveOLEObjects.pptx", FileFormat.Pptx2010)

finally:
    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.

page 29