On This Page (1)
Examples Showcase / schdoc_apply_template
Apply a SchDot template to an existing SchDoc
Open a schematic, clear its existing template graphics, apply a SchDot template from shared assets, and save the modified SchDoc.
This example opens an existing schematic, removes the embedded title-block template graphics, applies a .SchDot template, and saves the result as a new .SchDoc.
Run from the package root:
uv run python examples\schdoc_apply_template\schdoc_apply_template.pyThe example uses:
examples/assets/projects/bunny_brain/bunny_brain_D.SchDocexamples/assets/templates/Wavenumber__ANSI_D.SchDot
The output is written to:
examples/schdoc_apply_template/output/bunny_brain_D_with_applied_template.SchDocThe important API calls are:
schdoc = AltiumSchDoc(INPUT_SCHDOC)
schdoc.clear_template()
schdoc.apply_template(
TEMPLATE_SCHDOT,
clear_existing=False,
template_filename=TEMPLATE_REF,
)
schdoc.save(OUTPUT_SCHDOC)apply_template() can also clear the existing template itself by using the default clear_existing=True. This example calls clear_template() explicitly first so the two operations are easy to inspect independently. It passes template_filename=TEMPLATE_REF so the saved schematic stores a relocatable template reference instead of the absolute asset path used to load the file.
Source Code
from pathlib import Path
from altium_monkey import AltiumSchDoc
SAMPLE_DIR = Path(__file__).resolve().parent
EXAMPLES_DIR = SAMPLE_DIR.parent
ASSETS_DIR = EXAMPLES_DIR / "assets"
INPUT_SCHDOC = ASSETS_DIR / "projects" / "bunny_brain" / "bunny_brain_D.SchDoc"
TEMPLATE_SCHDOT = ASSETS_DIR / "templates" / "Wavenumber__ANSI_D.SchDot"
TEMPLATE_REF = TEMPLATE_SCHDOT.relative_to(EXAMPLES_DIR)
OUTPUT_DIR = SAMPLE_DIR / "output"
OUTPUT_SCHDOC = OUTPUT_DIR / "bunny_brain_D_with_applied_template.SchDoc"
def main() -> None:
OUTPUT_DIR.mkdir(parents=True, exist_ok=True)
schdoc = AltiumSchDoc(INPUT_SCHDOC)
template_before = schdoc.get_template()
removed_count = schdoc.clear_template()
inserted_count = schdoc.apply_template(
TEMPLATE_SCHDOT,
clear_existing=False,
template_filename=TEMPLATE_REF,
)
template_after = schdoc.get_template()
schdoc.save(OUTPUT_SCHDOC)
print(
f"Input template: {template_before.filename if template_before else '<none>'}"
)
print(f"Removed template records: {removed_count}")
print(f"Applied template: {TEMPLATE_REF}")
print(f"Inserted template records: {inserted_count}")
print(f"Output template: {template_after.filename if template_after else '<none>'}")
print(f"Show template graphics: {schdoc.sheet.show_template_graphics}")
print(f"Template file name: {schdoc.sheet.template_filename!r}")
print(f"Wrote SchDoc: {OUTPUT_SCHDOC.relative_to(SAMPLE_DIR)}")
if __name__ == "__main__":
main()