Existing objects in diagrams

Watermarker.get_content() returns a DiagramContent whose pages expose the diagram’s shapes and headers/footers. You can iterate the shapes to read their properties, modify them, or remove them.

Extract information about shapes

The example reports the shapes on the first page of a Visio diagram.

from groupdocs.watermark import Watermarker

def extract_shapes():
    with Watermarker("./diagram.vsdx") as watermarker:
        content = watermarker.get_content()
        page = content.pages[0]
        print(f"Pages: {len(content.pages)}; page 1 shapes: {len(page.shapes)}")
        for shape in page.shapes:
            text = (shape.text or "").strip()
            print(f"  shape name={shape.name!r} text={text!r} "
                  f"size={round(shape.width)}x{round(shape.height)}")

if __name__ == "__main__":
    extract_shapes()

diagram.vsdx is the sample file used in this example. Click here to download it.

Pages: 2; page 1 shapes: 12
  shape name='Rectangle' text='' size=108x72
  shape name='Circle' text='' size=108x108
  shape name='Square' text='' size=108x108
  shape name='Octagon' text='' size=108x108
  shape name='Sheet.20' text='' size=216x63
  shape name='Sheet.21' text='' size=216x54
  shape name='Triangle' text='' size=108x94
  shape name='Sheet.33' text='' size=85x0
  shape name='Sheet.34' text='' size=108x0
[TRUNCATED]

Download full output

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

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

def remove_and_modify_shapes():
    with Watermarker("./diagram.vsdx") as watermarker:
        content = watermarker.get_content()
        for page in content.pages:
            for i in range(len(page.shapes) - 1, -1, -1):
                if page.shapes[i].name == "Rectangle":
                    page.shapes.remove_at(i)
        watermarker.save("./output.vsdx")

if __name__ == "__main__":
    remove_and_modify_shapes()

diagram.vsdx is the sample file used in this example. Click here to download it.

Binary file (VSDX, 29 KB)

Download full output

You can replace a shape’s text by assigning to shape.text, replace its image by assigning a DiagramWatermarkableImage to shape.image, and modify its position and size through x, y, width, height, and rotate_angle.

Close
Loading

Analyzing your prompt, please hold on...

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