watermark
watermark
¶
Watermark support — the faint "DRAFT" or "CONFIDENTIAL" behind a document's content.
Word implements a watermark as a VML shape inside a header, not as DrawingML. VML is
deprecated in the specification, but this is what current versions of Word write and
what they render correctly; a DrawingML equivalent does not display the same way, and a
watermark that looks wrong is worse than none. So VML it is, and docx.oxml.ns carries
the v:, o: and w10: namespaces for it.
A watermark is a header artefact, so which pages show one follows from which header applies to them. Both API entry points therefore write into all three header types — default, first-page and even-page — because a watermark that vanishes on page 1 of a document with a distinct first-page header reads as a bug rather than as a setting.
Scope:
- Document.add_text_watermark applies the watermark to the whole document.
- Section.add_text_watermark applies it to one section. Where that section's headers are inherited from an earlier one, the watermark goes into the header actually in force, which the earlier section shares — a header that is inherited is the same header, and there is no way to mark up one section's copy of it alone without first breaking the link.
Watermark
¶
A watermark in a header — the faint text or image behind the document content.
Not constructed directly; reached through Section.watermark or returned by
Section.add_text_watermark and Section.add_image_watermark.
Source code in src/docx/watermark.py
remove
¶
Remove this watermark from the document.
The whole w:pict is removed, and the run holding it too when that leaves the
run empty, so nothing is left behind that Word would render as a stray space.
Source code in src/docx/watermark.py
iter_watermarks
¶
iter_watermarks(
hdrftr: _BaseHeaderFooter,
) -> Iterator[Watermark]
Generate a Watermark for each watermark shape in hdrftr.
Source code in src/docx/watermark.py
iter_watermark_headers
¶
iter_watermark_headers(
sections: Iterable[Section],
) -> Iterator[_BaseHeaderFooter]
Generate the header objects a watermark should be written into for sections.
All three header types of each section, skipping any whose definition has already been generated. A header inherited from an earlier section is that earlier section's header, so writing to both would give it two watermarks.
Source code in src/docx/watermark.py
add_text_watermark
¶
add_text_watermark(
headers: Iterable[_BaseHeaderFooter],
text: str,
font: str = "Calibri",
font_size: Length | int | None = None,
color: str = "C0C0C0",
opacity: float | None = None,
angle: float = 315,
width: Length | int = _DEFAULT_WIDTH,
height: Length | int = _DEFAULT_HEIGHT,
bold: bool = False,
italic: bool = False,
) -> List[Watermark]
Add a text watermark to each of headers, returning the watermarks added.
Source code in src/docx/watermark.py
add_image_watermark
¶
add_image_watermark(
headers: Iterable[_BaseHeaderFooter],
image_descriptor: str | PathLike[str] | IO[bytes],
width: Length | int | None = None,
height: Length | int | None = None,
washout: bool = True,
scale: float = 1.0,
) -> List[Watermark]
Add an image watermark to each of headers, returning the watermarks added.
The image is related to each header part separately, since a relationship belongs to the part that refers to it.
Source code in src/docx/watermark.py
remove_watermarks
¶
remove_watermarks(
headers: Iterable[_BaseHeaderFooter],
) -> int
Remove every watermark from each of headers, returning how many were removed.