Spire.Office Knowledgebase Page 36 | E-iceblue

PowerPoint presentations often contain hyperlinks that guide audiences to additional resources or locations within the presentations. While these links can be useful for providing further information and easy navigation, there are instances where they may detract from the presentation's flow or compromise its professional appearance. Those invalid or unnecessary links in slides can be easily removed using Python, enhancing the overall quality of the presentations.

This article will show how Spire.Presentation for Python can be utilized to remove hyperlinks from PowerPoint presentations efficiently.

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

Remove Hyperlinks from Text in PowerPoint Slides

The normal text in a PowerPoint presentation is contained in auto shapes. Developers can access the text ranges within these shapes using the IAutoShape.TextFrame.Paragraphs[].TextRanges[] property and read or set the hyperlinks on them using the TextRange.ClickAction property. Hyperlinks on text can be removed by setting the TextRange.ClickAction property to None.

The detailed steps are as follows:

  • Create an instance of Presentation class and load a PowerPoint presentation using Presentation.LoadFromFile() method.
  • Iterate through the slides in the presentation and then iterate through the shapes in the slides.
  • Check if the shape is an instance of IAutoShape. If it is, iterate through the paragraphs in the shape, and then the text ranges in the paragraphs.
  • Check if the TextRange.ClickAction property of a text range is None. If it is not, remove the hyperlink by setting TextRange.ClickAction property to None.
  • Save the presentation using Presentation.SaveToFile() method.
  • Python
from spire.presentation import Presentation, IAutoShape, FileFormat

# Create an instance of Presentation
presentation = Presentation()

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

# Iterate through the slides in the presentation
for slide in presentation.Slides:
    # Iterate through the shapes
    for shape in slide.Shapes:
        # Check if the shape is an AutoShape instance
        if isinstance(shape, IAutoShape):
            # Iterate through the paragraphs in the shape
            for paragraph in shape.TextFrame.Paragraphs:
                # Iterate through the text ranges in the paragraph
                for textRange in paragraph.TextRanges:
                    # Check if the text range has a hyperlink
                    if textRange.ClickAction is not None:
                        # Remove the hyperlink
                        textRange.ClickAction = None

# Save the presentation
presentation.SaveToFile("output/RemoveSlideTextHyperlink.pptx", FileFormat.Pptx2013)
presentation.Dispose()

Python: Remove Hyperlinks from PowerPoint Presentations

Remove Hyperlinks from All Shapes in PowerPoint Slides

The IShape class represents all types of shapes in a presentation slide, such as auto shapes, images, tables, and more. The hyperlink on all these shapes can be removed by setting the value obtained from the IShape.Click.get_NoAction() method as the value of the shapes’ IShape.Click property. The detailed steps are as follows:

  • Create an instance of Presentation class and load a PowerPoint presentation using Presentation.LoadFromFile() method.
  • Iterate through the slides in the presentation and then iterate through the shapes in the slides.
  • Check if the IShape.Click property is None. If it is not, remove the hyperlink by setting the property to the result of IShape.Click.get_NoAction() method.
  • Save the presentation using Presentation.SaveToFile() method.
  • Python
from spire.presentation import Presentation, FileFormat

# Create an instance of Presentation
presentation = Presentation()

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

# Iterate through the slides in the presentation
for slide in presentation.Slides:
    # Iterate through the shapes in the slide
    for shape in slide.Shapes:
        # Check if the shape has a hyperlink
        if shape.Click is not None:
            # Remove the click action
            shape.Click = shape.Click.get_NoAction()

# Save the presentation
presentation.SaveToFile("output/RemoveSlideShapeHyperlink.pptx", FileFormat.Pptx2013)
presentation.Dispose()

Python: Remove Hyperlinks from PowerPoint Presentations

Remove Hyperlinks from Specific Types of Shapes in PowerPoint Slides

In addition to directly removing hyperlinks for all shapes, we can also determine the shape type before removing the hyperlinks to find and remove hyperlinks from shapes of the specified type. The detailed steps are as follows:

  • Create an instance of Presentation class and load a PowerPoint presentation using Presentation.LoadFromFile() method.
  • Iterate through the slides in the presentation and then iterate through the shapes in the slides.
  • Check if the shape is an instance of IEmbedImage, ITable, or IChart. If it is, check if the IShape.Click property of the shape is None. If it is not, remove the hyperlink by setting the property to the result of IShape.Click.get_NoAction() method.
  • Save the presentation using Presentation.SaveToFile() method.
  • Python
from spire.presentation import Presentation, FileFormat, IEmbedImage, ITable, IChart

# Create an instance of Presentation
presentation = Presentation()

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

# Iterate through the slides in the presentation
for slide in presentation.Slides:
    # Iterate through the shapes in the slide
    for shape in slide.Shapes:
        # Check if the shape is an embedded image
        if isinstance(shape, (IEmbedImage, ITable, IChart)):
            # check if the click action is not None
            if shape.Click is not None:
                # Remove the click action
                shape.Click = shape.Click.get_NoAction()

# Save the presentation
presentation.SaveToFile("output/RemoveSlideShapeTypeHyperlink.pptx", FileFormat.Pptx2013)
presentation.Dispose()

Python: Remove Hyperlinks from PowerPoint Presentations

Remove Hyperlinks from Table Text in PowerPoint Slides

To remove hyperlinks from text within a table, it is necessary to iterate through the table's cells and the text ranges within each cell. Afterward, the hyperlinks on the text ranges in each cell can be removed by setting the TextRange.ClickAction property to None. The detailed steps are as follows:

  • Create an instance of Presentation class and load a PowerPoint presentation using Presentation.LoadFromFile() method.
  • Iterate through the slides in the presentation and then iterate through the shapes in the slides.
  • Check if a shape is an instance of ITable class. If it is, iterate through the rows in the table and then the cells in the rows.
  • Iterate through the paragraphs in the cells and then the text ranges in the paragraphs.
  • Check if the TextRange.ClickAction property of a text range is None. If it is not, remove the hyperlink by setting the value of the property to None.
  • Save the presentation using Presentation.SaveToFile() method.
  • Python
from spire.presentation import Presentation, ITable, FileFormat

# Create an instance of Presentation
presentation = Presentation()

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

# Iterate through the slides in the presentation
for slide in presentation.Slides:
    # Iterate through the shapes in the slide
    for shape in slide.Shapes:
        # Check if the shape is a table
        if isinstance(shape, ITable):
            # Get the table
            table = ITable(shape)
            # Iterate through the rows in the table
            for row in table.TableRows:
                # Iterate through the cells in the row
                for cell in row:
                    # Iterate through the paragraphs in the cell
                    for para in cell.TextFrame.Paragraphs:
                        # Iterate through the text ranges in the paragraph
                        for range in para.TextRanges:
                            # Check if the text run contains a hyperlink
                            if range.ClickAction is not None:
                                # Remove the hyperlink
                                range.ClickAction = None

presentation.SaveToFile("output/RemoveSlideTableTextHyperlink.pptx", FileFormat.Pptx2013)
presentation.Dispose()

Python: Remove Hyperlinks 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.

Java: Edit a Word Document

2024-08-20 01:15:58 Written by Koohji

Editing a Word document is a common task that many people encounter in their daily lives, whether it's for work, school, or personal projects. From correcting spelling and grammar errors to rearranging content and formatting the document, the ability to edit a Word document efficiently is a valuable skill.

In this article, you will learn how to programmatically edit or modify a Word document using Spire.Doc for Java.

Install Spire.Doc for Java

First of all, you're required to add the Spire.Doc.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.doc</artifactId>
        <version>14.1.3</version>
    </dependency>
</dependencies>

Modify Text in a Word Document in Java

To retrieve the paragraph from a particular section, you can use the Section.getParagraphs().get() method. Once you have the target paragraph, you can then update its text content by calling the Paragraph.setText() method and passing in the new text you want to assign.

The following are the steps modify text in a Word document using Java:

  • Create a Document object.
  • Load a Word file from the given file path.
  • Get a specific section using Document.getSections().get() method.
  • Get a specific paragraph using Section.getParagraphs().get() method.
  • Reset the text of the paragraph using Paragraph.setText() method.
  • Save the updated document to a different Word file.
  • Java
import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.Section;
import com.spire.doc.documents.Paragraph;

public class ModifyText {

    public static void main(String[] args) {

        // Create a new document object
        Document document = new Document();

        // Load an existing Word file
        document.loadFromFile("C:\\Users\\Administrator\\Desktop\\input.docx");

        // Get a specific section
        Section section = document.getSections().get(0);

        // Get a specific paragraph
        Paragraph paragraph = section.getParagraphs().get(0);

        // Modify the text of the paragraph
        paragraph.setText("The title has been modified");

        // Save the document to a different Word file
        document.saveToFile("ModifyText.docx", FileFormat.Docx);

        // Dispose resource
        document.dispose();
    }
}

Java: Edit a Word Document

Change Formatting of Text in a Word Document in Java

To modify the formatting of specific text within a paragraph, you first need to access the target paragraph object. Once you have the paragraph, you can then iterate through its child elements to locate the individual text ranges.

For each text range found, you can update the formatting by using the methods under the CharacterFormat object. This allows you to set properties like font name, size, color, and other text-level formatting options for the selected text.

The steps to change text formatting in a Word document are as follows:

  • Create a Document object.
  • Load a Word file from the given file path.
  • Get a specific section using Document.getSections().get() method.
  • Get a specific paragraph using Section.getParagraphs().get() method.
  • Iterate through the child objects in the paragraph.
    • Determine if a child object is a text range.
    • Get a specific text range.
    • Reset the text formatting using the methods under the CharacterFormat object.
  • Save the updated document to a different Word file.
  • Java
import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.Section;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.fields.TextRange;

import java.awt.*;

public class ChangeTextFormatting {

    public static void main(String[] args) {

        // Create a new document object
        Document document = new Document();

        // Load an existing Word file
        document.loadFromFile("C:\\Users\\Administrator\\Desktop\\input.docx");

        // Get a specific section
        Section section = document.getSections().get(0);

        // Get a specific paragraph
        Paragraph paragraph = section.getParagraphs().get(1);

        // Iterate through the child objects in the paragraph
        for (int i = 0; i < paragraph.getChildObjects().getCount(); i++)
        {
            // Determine if a child object is text range
            if (paragraph.getChildObjects().get(i) instanceof TextRange)
            {
                // Get a specific text range
                TextRange textRange = (TextRange)paragraph.getChildObjects().get(i);

                // Reset font name for it
                textRange.getCharacterFormat().setFontName("Corbel Light");

                // Reset font size for it
                textRange.getCharacterFormat().setFontSize(11);

                // Reset text color for it
                textRange.getCharacterFormat().setTextColor(Color.blue);

                // Apply italic to the text range
                textRange.getCharacterFormat().setItalic(true);
            }
        }

        // Save the document to a different Word file
        document.saveToFile("ChangeFont.docx", FileFormat.Docx);

        // Dispose resource
        document.dispose();
    }
}

Java: Edit a Word Document

Add New Elements to a Word Document in Java

When working with Word documents, the paragraph serves as the foundational unit for incorporating diverse elements like text, images, lists, and charts. To introduce a new paragraph within a specific section, you can leverage the Section.addParagraph() method.

Once the paragraph has been added, you can then proceed to add various other elements to it by utilizing the methods available within the Paragraph object.

The following are the steps to add new elements (text and images) to a Word document using Java:

  • Create a Document object.
  • Load a Word file from the given file path.
  • Get a specific section using Document.getSections() method.
  • Add a paragraph to the section using Section.addParagraph() method.
  • Add text to the paragraph using Paragraph.appendText() method.
  • Add an image to the paragraph using Paragraph.appendPicture() method.
  • Save the updated document to a different Word file.
  • Java
import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.Section;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.documents.ParagraphStyle;

public class AddNewElements {

    public static void main(String[] args) {

        // Create a new document object
        Document document = new Document();

        // Load an existing Word file
        document.loadFromFile("C:\\Users\\Administrator\\Desktop\\input.docx");

        // Get the last section
        Section lastSection = document.getLastSection();

        // Add a paragraph to the section
        Paragraph paragraph = lastSection.addParagraph();

        // Add text to the paragraph
        paragraph.appendText("This text and the image shown below are added programmatically using Spire.Doc for Java.");

        // Add an image to the paragraph
        paragraph.appendPicture("C:\\Users\\Administrator\\Desktop\\logo.png");

        // Create a paragraph style
        ParagraphStyle style = new ParagraphStyle(document);
        style.setName("FontStyle");
        style.getCharacterFormat().setFontName("Times New Roman");
        style.getCharacterFormat().setFontSize(12);
        document.getStyles().add(style);

        // Apply the style to the paragraph
        paragraph.applyStyle(style.getName());

        // Save the document to a different Word file
        document.saveToFile("AddNewElements.docx", FileFormat.Docx);

        // Dispose resource
        document.dispose();
    }
}

Java: Edit a Word Document

Remove Paragraphs from a Word Document in Java

To remove a specific paragraph from the collection of paragraphs within a document, you can call the ParagraphCollection.removeAt() method. This method takes the index of the paragraph you wish to remove as an argument, allowing you to selectively delete the desired paragraph from the document.

The steps to remove paragraphs from a Word document using Java are as follows:

  • Create a Document object.
  • Load a Word file from the given file path.
  • Get a specific section using Document.getSections().get() method.
  • Remove a specific paragraph from the section using ParagraphCollection.removeAt() method.
  • Save the updated document to a different Word file.
  • Java
import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.Section;

public class RemoveParagraph {

    public static void main(String[] args) {

        // Create a new document object
        Document document = new Document();

        // Load an existing Word file
        document.loadFromFile("C:\\Users\\Administrator\\Desktop\\input.docx");

        // Get a specific section
        Section section = document.getSections().get(0);

        // Remove a specific paragraph
        section.getParagraphs().removeAt(0);

        // Save the document to a different Word file
        document.saveToFile("RemoveParagraph.docx", FileFormat.Docx);

        // Dispose resource
        document.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.

Creating charts in PowerPoint slides is a powerful way to enhance presentations by visually representing complex information, making it easier for audiences to grasp key insights. By reading Excel data directly to generate charts, you can streamline the data entry process and ensure data accuracy. Or, if you want to use charts from Excel files directly in PowerPoint presentations, you can directly insert them as pictures into PowerPoint slides, thus maximizing the original formatting and appearance. This article will show how to create charts in PowerPoint slides from Excel data using Spire.Office for .NET in .NET programs.

Install Spire.Office for .NET

To begin with, you need to add the DLL files included in the Spire.Office for.NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.

PM> Install-Package Spire.Office

Create Charts in PowerPoint Slides with Excel Data using C#

Developers can read data from Excel worksheets with Spire.XLS for .NET and then create charts in PowerPoint slides with Spire.Presentation for .NET using the read data as the charts' data source. The detailed steps for creating charts in PowerPoint presentations with Excel file data are as follows:

  • Create an instance of Presentation class.
  • Create an instance of Workbook class and load an Excel file using Workbook.LoadFromFile() method.
  • Get the first slide in the presentation through Presentation.Slides[] property and create a chart in the first slide of the presentation using ISlide.Shapes.AppendChart() method.
  • Clear the default dummy data using IChart.ChartData.Clear() method.
  • Get the first worksheet in the workbook through Workbook.Worksheets[] property.
  • Iterate through rows in the worksheet and then the columns in the worksheet:
    • Get the cell values in the worksheet through Worksheet.AllocatedRange[].Value2 property and set them as the values of the chart’s data through IChart.ChartData[].Value property.
  • Set the chart title using properties under IChart.ChartTitle property.
  • Set the chart series labels and category labels through IChart.Series.SeriesLabel and IChart.Categories.CategoryLabels properties.
  • Set the series values through IChart.Series[].Values property.
  • Set the number format of the axis through IChart.PrimaryCategoryAxis.NumberFormat and IChart.PrimaryValueAxis.NumberFormat properties.
  • Set the style of the chart through IChart.ChartStyle property.
  • Save the presentation using Presentation.SaveToFile() method.
  • C#
using Spire.Presentation;
using Spire.Presentation.Charts;
using Spire.Xls;
using System.Drawing;
using FileFormat = Spire.Presentation.FileFormat;
using IChart = Spire.Presentation.Charts.IChart;

namespace PresentationChartExcelData
{
    class Program
    {
        public static void Main(string[] args)
        {
            // Create an instance of Presentation class
            Presentation presentation = new Presentation();

            // Set the slide size
            presentation.SlideSize.Type = SlideSizeType.Screen16x9;

            // Create an instance of Workbook class and load an Excel file
            Workbook workbook = new Workbook();
            workbook.LoadFromFile("Sample.xlsx");

            // Get the first worksheet in the workbook
            Worksheet sheet = workbook.Worksheets[0];

            // Create a chart in the presentation
            RectangleF rect = new RectangleF(50, 100, presentation.SlideSize.Size.Width - 100, presentation.SlideSize.Size.Height - 150);
            ISlide slide = presentation.Slides[0];
            IChart chart = slide.Shapes.AppendChart(ChartType.ColumnClustered, rect);

            // Clear the default dummy data
            chart.ChartData.Clear(0, 0, 5, 5);

            // Iterate through the rows in the worksheet
            for (int i = 0; i < sheet.AllocatedRange.RowCount; i++)
            {
                // Iterate through the columns in the worksheet
                for (int j = 0; j < sheet.AllocatedRange.ColumnCount; j++)
                {
                    // Set the cell values in the worksheet as the values of the chart data
                    chart.ChartData[i, j].Value = sheet.AllocatedRange[i + 1, j + 1].Value2;
                    // Copy number formats
                    chart.ChartData[i, j].NumberFormat = sheet.AllocatedRange[i + 1, j + 1].NumberFormat;
                }
            }

            // Set the chart title
            chart.ChartTitle.TextProperties.Text = sheet.Name;
            chart.ChartTitle.TextProperties.IsCentered = true;
            chart.ChartTitle.Height = 25;
            chart.HasTitle = true;

            // Set the series labels and category labels
            chart.Series.SeriesLabel = chart.ChartData["B1", "C1"];
            chart.Categories.CategoryLabels = chart.ChartData["A2", "A" + sheet.AllocatedRange.RowCount];

            // Set the series values
            chart.Series[0].Values = chart.ChartData["B2", "B" + sheet.AllocatedRange.RowCount];
            chart.Series[1].Values = chart.ChartData["C2", "C" + sheet.AllocatedRange.RowCount];

            // Set the number format of the axis
            chart.PrimaryCategoryAxis.NumberFormat = sheet.AllocatedRange["A2"].NumberFormat;
            chart.PrimaryValueAxis.NumberFormat = sheet.AllocatedRange["B2"].NumberFormat;

            // Set the style of the chart
            chart.ChartStyle = ChartStyle.Style2;

            // Set the overlap and gap width
            chart.OverLap = 50;
            chart.GapWidth = 200;

            // Save the presentation
            presentation.SaveToFile("output/PresentationChartExcelData.pptx", FileFormat.Pptx2019);
            presentation.Dispose();
            workbook.Dispose();
        }
    }
}

C#: Create Charts in PowerPoint Slides with Excel Data

Insert Excel Charts into PowerPoint Slides as Images using C#

To insert an existing chart from an Excel worksheet into a PowerPoint slide while maintaining its appearance and formatting precisely, the Workbook.SaveChartAsImage() method can be employed. This method allows the Excel chart to be saved as an image, which can then be added to the slide. The specific steps are as follows:

  • Create an instance of Presentation class.
  • Create an instance of Workbook class and load an Excel file using Workbook.LoadFromFile() method.
  • Save a chart in a worksheet as an image using Workbook.SaveChartAsImage() method.
  • Embed the image into the presentation using Presentation.Images.Append() method.
  • Add the image to a slide using Presentation.Slides[].AppendEmbedImage() method.
  • Save the presentation using Presentation.SaveToFile() method.
  • C#
using Spire.Presentation;
using Spire.Presentation.Drawing;
using Spire.Xls;
using System.Drawing;
using FileFormat = Spire.Presentation.FileFormat;

namespace PresentationChartExcelChart
{
    class Program
    {
        public static void Main(string[] args)
        {
            // Create an instance of Presentation class
            Presentation presentation = new Presentation();

            // Set the slide size
            presentation.SlideSize.Type = SlideSizeType.Screen16x9;

            // Create an instance of Workbook class and load an Excel file
            Workbook workbook = new Workbook();
            workbook.LoadFromFile("Sample.xlsx");

            // Save the first chart in the first worksheet as an image
            Image image = workbook.SaveChartAsImage(workbook.Worksheets[0], 0);

            // Embed the image into the presentation
            IImageData imageData = presentation.Images.Append(image);

            // Add the image to the first slide
            RectangleF rect = new RectangleF(50, 120, presentation.SlideSize.Size.Width - 100, presentation.SlideSize.Size.Height - 170);
            presentation.Slides[0].Shapes.AppendEmbedImage(ShapeType.Rectangle, imageData, rect);

            // Save the presentation
            presentation.SaveToFile("output/PresentationChartExcelChart.pptx", FileFormat.Pptx2019);
            presentation.Dispose();
            workbook.Dispose();
        }
    }
}

C#: Create Charts in PowerPoint Slides with Excel Data

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 36