Shapes in spreadsheet document

Extracting information about all shapes in an Excel document

This sample iterates all worksheets and prints shape metadata, text, positioning, size, rotation, and image details.

import groupdocs.watermark as gw
import groupdocs.watermark.contents.spreadsheet as gwc_xls

load_options = gw.SpreadsheetLoadOptions()
with gw.Watermarker("spreadsheet.xlsx", load_options) as watermarker:
    content = watermarker.get_content(gwc_xls.SpreadsheetContent)
    for worksheet in content.worksheets:
        for shape in worksheet.shapes:
            print(shape.auto_shape_type)
            print(shape.mso_drawing_type)
            print(shape.text)
            if shape.image is not None:
                print(shape.image.width)
                print(shape.image.height)
                print(len(shape.image.get_bytes())))

            print(shape.id)
            print(shape.alternative_text)
            print(shape.x)
            print(shape.y)
            print(shape.width)
            print(shape.height)
            print(shape.rotate_angle)
            print(shape.is_word_art)
            print(shape.name)

Removing a particular shape

This sample removes shapes from a worksheet by index and by reference, then saves the workbook.

import groupdocs.watermark as gw
import groupdocs.watermark.contents.spreadsheet as gwc_xls

load_options = gw.SpreadsheetLoadOptions()
with gw.Watermarker("spreadsheet.xlsx", load_options) as watermarker:
    content = watermarker.get_content(gwc_xls.SpreadsheetContent)
    content.worksheets[0].shapes.remove_at(0)
    content.worksheets[0].shapes.remove(content.worksheets[0].shapes[0])
    watermarker.save("spreadsheet.xlsx")

Removing shapes with particular text formatting

This sample deletes shapes whose formatted text matches a condition (for example, red Arial fragments).

import groupdocs.watermark as gw
import groupdocs.watermark.contents.spreadsheet as gwc_xls
import groupdocs.watermark.watermarks as gww

load_options = gw.SpreadsheetLoadOptions()
with gw.Watermarker("spreadsheet.xlsx", load_options) as watermarker:
    content = watermarker.get_content(gwc_xls.SpreadsheetContent)
    for worksheet in content.worksheets:
        for i in range(worksheet.shapes.count - 1, -1, -1):
            for fragment in worksheet.shapes[i].formatted_text_fragments:
                if fragment.foreground_color == gww.Color.red and fragment.font.family_name == "Arial":
                    worksheet.shapes.remove_at(i)
                    break
    watermarker.save("spreadsheet.xlsx")

This sample sets or clears hyperlinks on shapes and charts across worksheets.

import groupdocs.watermark as gw
import groupdocs.watermark.contents.spreadsheet as gwc_xls

load_options = gw.SpreadsheetLoadOptions()
with gw.Watermarker("spreadsheet.xlsx", load_options) as watermarker:
    content = watermarker.get_content(gwc_xls.SpreadsheetContent)
    content.worksheets[0].charts[0].hyperlink = "https://www.aspose.com/"
    content.worksheets[0].shapes[0].hyperlink = "https://www.groupdocs.com/"
    content.worksheets[1].charts[0].hyperlink = None
    content.worksheets[1].shapes[0].hyperlink = None
    watermarker.save("spreadsheet.xlsx")

Replacing text for particular shapes

This sample finds shapes by exact text and replaces their text content with new plain text.

import groupdocs.watermark as gw
import groupdocs.watermark.contents.spreadsheet as gwc_xls

load_options = gw.SpreadsheetLoadOptions()
with gw.Watermarker("spreadsheet.xlsx", load_options) as watermarker:
    content = watermarker.get_content(gwc_xls.SpreadsheetContent)
    for shape in content.worksheets[0].shapes:
        if shape.text == "© Aspose 2016":
            shape.text = "© GroupDocs 2017"
    watermarker.save("spreadsheet.xlsx")

Replacing text for particular shapes with formatted text

This sample rebuilds a shape’s text using formatted fragments (font, style, and colors).

import groupdocs.watermark as gw
import groupdocs.watermark.contents.spreadsheet as gwc_xls
import groupdocs.watermark.watermarks as gww

load_options = gw.SpreadsheetLoadOptions()
with gw.Watermarker("spreadsheet.xlsx", load_options) as watermarker:
    content = watermarker.get_content(gwc_xls.SpreadsheetContent)
    for shape in content.worksheets[0].shapes:
        if shape.text == "© Aspose 2016":
            shape.formatted_text_fragments.clear()
            shape.formatted_text_fragments.add(
                "© GroupDocs 2017",
                gww.Font("Calibri", 19.0, gww.FontStyle.BOLD),
                gww.Color.red,
                gww.Color.aqua,
            )
    watermarker.save("spreadsheet.xlsx")

Replacing shape image

This sample replaces the image content of shapes with a new image loaded from disk.

import groupdocs.watermark as gw
import groupdocs.watermark.contents.spreadsheet as gwc_xls

load_options = gw.SpreadsheetLoadOptions()
with gw.Watermarker("spreadsheet.xlsx", load_options) as watermarker:
    content = watermarker.get_content(gwc_xls.SpreadsheetContent)
    for shape in content.worksheets[0].shapes:
        if shape.image is not None:
            with open("test.png", "rb") as f:
                shape.image = gwc_xls.SpreadsheetWatermarkableImage(f.read())
    watermarker.save("spreadsheet.xlsx")

Setting background image for particular shapes

This sample assigns a background image to shapes and configures transparency and tiling options.

import groupdocs.watermark as gw
import groupdocs.watermark.contents.spreadsheet as gwc_xls

load_options = gw.SpreadsheetLoadOptions()
with gw.Watermarker("spreadsheet.xlsx", load_options) as watermarker:
    content = watermarker.get_content(gwc_xls.SpreadsheetContent)
    for shape in content.worksheets[0].shapes:
        if shape.text == "© Aspose 2016":
            with open("test.png", "rb") as f:
                shape.image_fill_format.background_image = gwc_xls.SpreadsheetWatermarkableImage(f.read())
            shape.image_fill_format.transparency = 0.5
            shape.image_fill_format.tile_as_texture = True
    watermarker.save("spreadsheet.xlsx")

Updating shape properties

This sample updates several shape properties such as alternative text, rotation, position, and size.

import groupdocs.watermark as gw
import groupdocs.watermark.contents.spreadsheet as gwc_xls

load_options = gw.SpreadsheetLoadOptions()
with gw.Watermarker("spreadsheet.xlsx", load_options) as watermarker:
    content = watermarker.get_content(gwc_xls.SpreadsheetContent)
    for shape in content.worksheets[0].shapes:
        if shape.text == "© Aspose 2019":
            shape.alternative_text = "watermark"
            shape.rotate_angle = 30
            shape.x = 200
            shape.y = 200
            shape.width = 400
            shape.height = 100
    watermarker.save("spreadsheet.xlsx")
Close
Loading

Analyzing your prompt, please hold on...

An error occurred while retrieving the results. Please refresh the page and try again.