On This Page (6)
Examples Showcase / schdoc_add_text_string
Add text strings to an existing SchDoc
Load a blank schematic, create several detached text strings with different justifications, orientations, and fonts, add them to the document, and save the modified SchDoc.
Open a blank schematic, add several text strings, and save the modified .SchDoc.
This example shows the canonical public create-then-insert path for standalone schematic text strings:
- load an existing
SchDoc - describe each text anchor with
SchPointMils - call
make_sch_text_string(...) - insert each object with
AltiumSchDoc.add_object(...) - persist the change with
AltiumSchDoc.save(...)
What It Shows
- creating detached text strings with
make_sch_text_string(...) - using
SchPointMilsfor absolute placement - using
SchFontSpecinstead of rawfont_id - using
TextOrientationfor 90-degree text rotation - using
TextJustificationfor anchor alignment - using
ColorValuehelpers instead of raw Win32 color integers - reopening the written file and inspecting the created text strings
Run
From the project root:
uv run python examples\schdoc_add_text_string\schdoc_add_text_string.pyInput
This sample uses:
examples/schdoc_add_text_string/input/blank.SchDocOutput
The script writes:
examples/schdoc_add_text_string/output/blank_with_text_strings.SchDocExpected Result
The output file should contain three visible text strings:
- bottom-left justified,
Arial 10 - centered and rotated 90 degrees,
Courier New 12 - top-right justified and mirrored,
Times New Roman 14
Each text string uses a different anchor location so it is easy to verify the placement visually in Altium. The script also prints the reopened text, font, orientation, justification, mirror state, location, and color for a quick terminal sanity check.
Text strings are single-line objects. If you need multiline text, use make_sch_text_frame(...) or make_sch_note(...) instead.
Source Code
from __future__ import annotations
from pathlib import Path
from altium_monkey import (
AltiumSchDoc,
ColorValue,
SchFontSpec,
SchPointMils,
TextJustification,
TextOrientation,
make_sch_text_string,
)
SAMPLE_DIR = Path(__file__).resolve().parent
INPUT_SCHDOC = SAMPLE_DIR / "input" / "blank.SchDoc"
OUTPUT_DIR = SAMPLE_DIR / "output"
OUTPUT_SCHDOC = OUTPUT_DIR / "blank_with_text_strings.SchDoc"
def main() -> None:
OUTPUT_DIR.mkdir(parents=True, exist_ok=True)
schdoc = AltiumSchDoc(INPUT_SCHDOC)
text_specs = [
{
"location_mils": SchPointMils.from_mils(1200, 6200),
"text": "Bottom left Arial 10",
"font": SchFontSpec(name="Arial", size=10),
"color": ColorValue.from_hex("#000000"),
"orientation": TextOrientation.DEGREES_0,
"justification": TextJustification.BOTTOM_LEFT,
"mirrored": False,
"url": "",
},
{
"location_mils": SchPointMils.from_mils(4200, 6200),
"text": "Center 90 Courier 12",
"font": SchFontSpec(name="Courier New", size=12),
"color": ColorValue.from_hex("#003366"),
"orientation": TextOrientation.DEGREES_90,
"justification": TextJustification.CENTER_CENTER,
"mirrored": False,
"url": "https://example.com/text-string",
},
{
"location_mils": SchPointMils.from_mils(7600, 6200),
"text": "Top right mirrored",
"font": SchFontSpec(name="Times New Roman", size=14),
"color": ColorValue.from_hex("#660000"),
"orientation": TextOrientation.DEGREES_0,
"justification": TextJustification.TOP_RIGHT,
"mirrored": True,
"url": "",
},
]
for spec in text_specs:
schdoc.add_object(
make_sch_text_string(
location_mils=spec["location_mils"],
text=spec["text"],
font=spec["font"],
color=spec["color"],
orientation=spec["orientation"],
justification=spec["justification"],
mirrored=spec["mirrored"],
url=spec["url"],
)
)
schdoc.save(OUTPUT_SCHDOC)
reopened = AltiumSchDoc(OUTPUT_SCHDOC)
print(f"Loaded: {INPUT_SCHDOC.name}")
print(f"Text strings written: {len(reopened.text_strings)}")
for index, text_string in enumerate(reopened.text_strings, start=1):
font_info = reopened.font_manager.get_font_info(text_string.font_id)
color_text = ColorValue.from_win32(text_string.color or 0).hex
print(
f"Text string {index}: {text_string.text!r}, "
f"{font_info['name']} {font_info['size']}, "
f"{text_string.orientation.name}, "
f"{text_string.justification.name}, "
f"mirrored={text_string.is_mirrored}, "
f"location=({text_string.location_mils.x_mils:.0f}, "
f"{text_string.location_mils.y_mils:.0f}), "
f"color={color_text}"
)
print(f"Wrote: {OUTPUT_SCHDOC.relative_to(SAMPLE_DIR)}")
if __name__ == "__main__":
main()