Existing objects in word processing document

Watermarker.get_content() returns a WordProcessingContent whose sections each expose a shapes collection. Watermarks added to a Word document are shapes, so this is how you find, modify, and remove watermarks that already exist in a document.

Extract information about shapes

from groupdocs.watermark import Watermarker
from groupdocs.watermark.options.word_processing import WordProcessingLoadOptions

def extract_shapes():
    with Watermarker("./document.docx", WordProcessingLoadOptions()) as watermarker:
        content = watermarker.get_content()
        for i, section in enumerate(content.sections):
            print(f"Section {i}: shapes={len(section.shapes)}")
            for shape in section.shapes:
                text = (shape.text or "").strip()
                print(f"  shape text={text!r} size={round(shape.width)}x{round(shape.height)} name={shape.name!r}")

if __name__ == "__main__":
    extract_shapes()

document.docx is the sample file used in this example. Click here to download it.

Section 0: shapes=3
  shape text='' size=230x69 name=''
  shape text='' size=90x3 name='Rectangle 100004'
  shape text='' size=460x287 name=''

Download full output

Each shape exposes text, image, name, alternative_text, x, y, width, height, rotate_angle, is_word_art, and behind_text.

Remove and modify shapes

The shapes collection supports remove_at(index) and remove(shape). Iterate in reverse when removing by index:

from groupdocs.watermark import Watermarker
from groupdocs.watermark.options.word_processing import WordProcessingLoadOptions

def remove_and_modify_shapes():
    with Watermarker("./document.docx", WordProcessingLoadOptions()) as watermarker:
        content = watermarker.get_content()
        for section in content.sections:
            for i in range(len(section.shapes) - 1, -1, -1):
                if section.shapes[i].text == "CONFIDENTIAL":
                    section.shapes.remove_at(i)
        watermarker.save("./output.docx")

if __name__ == "__main__":
    remove_and_modify_shapes()

document.docx is the sample file used in this example. Click here to download it.

Binary file (DOCX, 118 KB)

Download full output

You can replace a shape’s text by assigning to shape.text, replace its image by assigning a WordProcessingWatermarkableImage to shape.image, set or clear a hyperlink via shape.hyperlink, and modify its position, size, and rotate_angle. Headers and footers are available through section.headers_footers.

Close
Loading

Analyzing your prompt, please hold on...

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