Add watermarks to spreadsheet documents
Leave feedback
This sample adds either a text or image watermark to specific worksheets by index within a workbook.
import groupdocs.watermark as gw
import groupdocs.watermark.watermarks as gww
import groupdocs.watermark.options.spreadsheet as gwo_xls
load_options = gw.SpreadsheetLoadOptions()
with gw.Watermarker("spreadsheet.xlsx", load_options) as watermarker:
text_watermark = gww.TextWatermark("Test watermark", gww.Font("Arial", 8.0))
text_opts = gwo_xls.SpreadsheetWatermarkShapeOptions()
text_opts.worksheet_index = 0
watermarker.add(text_watermark, text_opts)
with gww.ImageWatermark("logo.jpg") as image_watermark:
img_opts = gwo_xls.SpreadsheetWatermarkShapeOptions()
img_opts.worksheet_index = 1
watermarker.add(image_watermark, img_opts)
watermarker.save("spreadsheet.xlsx")
This sample retrieves worksheet content dimensions and row/column sizes for precise watermark sizing.
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)
print(content.worksheets[0].content_area_height)
print(content.worksheets[0].content_area_width)
print(content.worksheets[0].get_column_width(0))
print(content.worksheets[0].get_row_height(0))
This sample finds all embedded images in a worksheet and overlays a centered, rotated text watermark on each.
import groupdocs.watermark as gw
import groupdocs.watermark.watermarks as gww
import groupdocs.watermark.common as gwc
import groupdocs.watermark.contents.spreadsheet as gwc_xls
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)
images = content.worksheets[0].find_images()
for image in images:
image.add(watermark)
watermarker.save("spreadsheet.xlsx")
These samples cover multiple watermark types and options available for spreadsheets, including shapes, WordArt, backgrounds, and headers/footers.
This sample demonstrates adding shape-based text watermarks using modern WordArt options to a specific worksheet.
Use SpreadsheetWatermarkShapeOptions
or SpreadsheetWatermarkModernWordArtOptions
when adding shape-based text watermarks.
import groupdocs.watermark as gw
import groupdocs.watermark.watermarks as gww
import groupdocs.watermark.options.spreadsheet as gwo_xls
load_options = gw.SpreadsheetLoadOptions()
with gw.Watermarker("spreadsheet.xlsx", load_options) as watermarker:
text_watermark = gww.TextWatermark("Test watermark", gww.Font("Arial", 8.0))
options = gwo_xls.SpreadsheetWatermarkModernWordArtOptions()
options.worksheet_index = 0
watermarker.add(text_watermark, options)
watermarker.save("spreadsheet.xlsx")
This sample sets extra shape properties (name, alt text, lock state) when adding a text watermark shape.
import groupdocs.watermark as gw
import groupdocs.watermark.watermarks as gww
import groupdocs.watermark.options.spreadsheet as gwo_xls
load_options = gw.SpreadsheetLoadOptions()
with gw.Watermarker("spreadsheet.xlsx", load_options) as watermarker:
watermark = gww.TextWatermark("Test watermark", gww.Font("Segoe UI", 19.0))
options = gwo_xls.SpreadsheetWatermarkShapeOptions()
options.name = "Shape 1"
options.alternative_text = "Test watermark"
options.is_locked = True
watermarker.add(watermark, options)
watermarker.save("spreadsheet.xlsx")
This sample configures line and outline text effects for shape-based text watermarks using spreadsheet-specific effects.
import groupdocs.watermark as gw
import groupdocs.watermark.watermarks as gww
import groupdocs.watermark.options.spreadsheet as gwo_xls
import groupdocs.watermark.common as gwc
load_options = gw.SpreadsheetLoadOptions()
with gw.Watermarker("spreadsheet.xlsx", load_options) as watermarker:
watermark = gww.TextWatermark("Test watermark", gww.Font("Segoe UI", 19.0))
effects = gwo_xls.SpreadsheetTextEffects()
effects.line_format.enabled = True
effects.line_format.color = gww.Color.red
effects.line_format.dash_style = gwc.OfficeDashStyle.DASH_DOT_DOT
effects.line_format.line_style = gwc.OfficeLineStyle.TRIPLE
effects.line_format.weight = 1
options = gwo_xls.SpreadsheetWatermarkShapeOptions()
options.effects = effects
watermarker.add(watermark, options)
watermarker.save("spreadsheet.xlsx")
This sample applies image processing effects (brightness, contrast, chroma key, border) to an image watermark.
import groupdocs.watermark as gw
import groupdocs.watermark.watermarks as gww
import groupdocs.watermark.options.spreadsheet as gwo_xls
load_options = gw.SpreadsheetLoadOptions()
with gw.Watermarker("spreadsheet.xlsx", load_options) as watermarker:
with gww.ImageWatermark("logo.png") as watermark:
effects = gwo_xls.SpreadsheetImageEffects()
effects.brightness = 0.7
effects.contrast = 0.6
effects.chroma_key = gww.Color.red
effects.border_line_format.enabled = True
effects.border_line_format.weight = 1
options = gwo_xls.SpreadsheetWatermarkShapeOptions()
options.effects = effects
watermarker.add(watermark, options)
watermarker.save("spreadsheet.xlsx")
This sample adds an image as a worksheet background watermark using background-specific options.
import groupdocs.watermark as gw
import groupdocs.watermark.watermarks as gww
import groupdocs.watermark.options.spreadsheet as gwo_xls
load_options = gw.SpreadsheetLoadOptions()
with gw.Watermarker("spreadsheet.xlsx", load_options) as watermarker:
with gww.ImageWatermark("logo.gif") as watermark:
options = gwo_xls.SpreadsheetBackgroundWatermarkOptions()
watermarker.add(watermark, options)
watermarker.save("spreadsheet.xlsx")
This sample sizes a watermark to fit the worksheet content area using pixel-based background dimensions.
import groupdocs.watermark as gw
import groupdocs.watermark.watermarks as gww
import groupdocs.watermark.options.spreadsheet as gwo_xls
import groupdocs.watermark.contents.spreadsheet as gwc_xls
import groupdocs.watermark.common as gwc
load_options = gw.SpreadsheetLoadOptions()
with gw.Watermarker("spreadsheet.xlsx", load_options) as watermarker:
watermark = gww.TextWatermark("Test watermark", gww.Font("Segoe UI", 19.0))
watermark.horizontal_alignment = gwc.HorizontalAlignment.CENTER
watermark.vertical_alignment = gwc.VerticalAlignment.CENTER
watermark.rotate_angle = 90
watermark.sizing_type = gww.SizingType.SCALE_TO_PARENT_DIMENSIONS
watermark.scale_factor = 0.5
watermark.opacity = 0.5
content = watermarker.get_content(gwc_xls.SpreadsheetContent)
options = gwo_xls.SpreadsheetBackgroundWatermarkOptions()
options.background_width = content.worksheets[0].content_area_width_px
options.background_height = content.worksheets[0].content_area_height_px
watermarker.add(watermark, options)
watermarker.save("spreadsheet.xlsx")
This sample adds an image watermark into worksheet headers or footers with alignment and scaling options.
import groupdocs.watermark as gw
import groupdocs.watermark.watermarks as gww
import groupdocs.watermark.options.spreadsheet as gwo_xls
import groupdocs.watermark.common as gwc
load_options = gw.SpreadsheetLoadOptions()
with gw.Watermarker("spreadsheet.xlsx", load_options) as watermarker:
with gww.ImageWatermark("logo.png") as watermark:
watermark.vertical_alignment = gwc.VerticalAlignment.TOP
watermark.horizontal_alignment = gwc.HorizontalAlignment.CENTER
watermark.sizing_type = gww.SizingType.SCALE_TO_PARENT_DIMENSIONS
watermark.scale_factor = 1.0
options = gwo_xls.SpreadsheetWatermarkHeaderFooterOptions()
options.worksheet_index = 0
watermarker.add(watermark, options)
watermarker.save("spreadsheet.xlsx")
This sample inserts a formatted text watermark into worksheet headers or footers with alignment controls.
import groupdocs.watermark as gw
import groupdocs.watermark.watermarks as gww
import groupdocs.watermark.options.spreadsheet as gwo_xls
import groupdocs.watermark.common as gwc
load_options = gw.SpreadsheetLoadOptions()
with gw.Watermarker("spreadsheet.xlsx", load_options) as watermarker:
watermark = gww.TextWatermark("Test watermark", gww.Font("Segoe UI", 19.0, gww.FontStyle.BOLD))
watermark.foreground_color = gww.Color.red
watermark.background_color = gww.Color.aqua
watermark.vertical_alignment = gwc.VerticalAlignment.TOP
watermark.horizontal_alignment = gwc.HorizontalAlignment.CENTER
options = gwo_xls.SpreadsheetWatermarkHeaderFooterOptions()
options.worksheet_index = 0
watermarker.add(watermark, options)
watermarker.save("spreadsheet.xlsx")
- Shapes in spreadsheet document
- Working with spreadsheet document attachments
- Working with worksheet backgrounds
- Working with worksheet headers and footers
Was this page helpful?
Any additional feedback you'd like to share with us?
Please tell us how we can improve this page.
Thank you for your feedback!
We value your opinion. Your feedback will help us improve our documentation.