Character formatting

Bold, color, size โ€” via scopes and ParameterSets

Sample output โ€” generated by tests/generate_v2_doc_artifacts.py running real HWP

Ported from examples/charshape_bold.py.

1. Scope-based โ€” the ordinary case

from hwpapi import App
from hwpapi.context import charshape_scope
from hwpapi import units as U

with App() as app:
    app.new()

    with charshape_scope(app, bold=True, size=U.pt(14), color="#C0392B"):
        app.doc.insert_text("This line is bold 14pt red\n")
    # Cursor style restored here
    app.doc.insert_text("Back to default\n")

2. Element-based โ€” for a specific paragraph

from hwpapi import App
from hwpapi.low.parametersets import CharShape

with App() as app:
    app.open("report.hwp")

    para0 = app.doc.paragraphs[0]
    para0.charshape = CharShape(Bold=True, Height=1800, TextColor="#000080")

3. Tweak a single attribute

from hwpapi import App

with App() as app:
    app.open("report.hwp")

    # Read current shape, mutate one key, write back
    cs = app.doc.paragraphs[0].charshape
    cs.Italic = True
    app.doc.paragraphs[0].charshape = cs

4. Snapshot / restore via clone

from hwpapi import App

with App() as app:
    app.new()
    app.doc.insert_text("Snapshot test.\n")
    app.doc.select_all()

    action = app.actions.CharShape
    ps = action.pset

    # Snapshot โ€” native pset.Clone()
    original = ps.clone()
    print(f"Snapshot: Bold={original.Bold}, Italic={original.Italic}")

    # Apply temporary changes
    ps.Bold = True
    ps.Italic = True
    ps.TextColor = "#00FF00"
    action.run()
    print("Applied temporary formatting.")

    # Restore from snapshot
    ps.update_from(original)
    action.run()
    print("Restored original formatting.")

5. Introspect without running

from hwpapi import App

with App() as app:
    cls = app.actions.get_pset_class("CharShape")
    desc = app.actions.get_description("CharShape")
    print(f"CharShape โ†’ {cls.__name__}: {desc}")
    print(f"Has {len(cls._property_registry)} properties")
    print(f"First 5: {list(cls._property_registry.keys())[:5]}")

See also

Back to top