Working with worksheet backgrounds

Extracting information about all worksheet backgrounds in an Excel document

This sample iterates worksheets and prints size and byte-length details for each background image.

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:
        if worksheet.background_image is not None:
            print(worksheet.background_image.width)
            print(worksheet.background_image.height)
            print(len(worksheet.background_image.get_bytes()))

Removing a particular background

This sample clears the background image for a selected worksheet and saves the updated 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].background_image = None
    watermarker.save("spreadsheet.xlsx")

Adding watermark to all backgrounds in an Excel worksheet

This sample applies a centered, rotated text watermark to each worksheet’s background image.

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

load_options = gw.SpreadsheetLoadOptions()
with gw.Watermarker("spreadsheet.xlsx", load_options) as watermarker:
    watermark = gww.TextWatermark("Protected image", gww.Font("Arial", 8.0))
    watermark.horizontal_alignment = gwc.HorizontalAlignment.CENTER
    watermark.vertical_alignment = gwc.VerticalAlignment.CENTER
    watermark.rotate_angle = 45
    watermark.sizing_type = gww.SizingType.SCALE_TO_PARENT_DIMENSIONS
    watermark.scale_factor = 1.0

    content = watermarker.get_content(gwc_xls.SpreadsheetContent)
    for worksheet in content.worksheets:
        if worksheet.background_image is not None:
            worksheet.background_image.add(watermark)
    watermarker.save("spreadsheet.xlsx")

Settings background image for charts

This sample sets a chart’s background image and configures its transparency and tiling.

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)
    with open("test.png", "rb") as f:
        content.worksheets[0].charts[0].image_fill_format.background_image = \
            gwc_xls.SpreadsheetWatermarkableImage(f.read())
    content.worksheets[0].charts[0].image_fill_format.transparency = 0.5
    content.worksheets[0].charts[0].image_fill_format.tile_as_texture = True
    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.