Skip to main content

Sapwright

A typed, object-oriented Python wrapper around the SAP GUI Scripting API. Get IDE autocomplete, static type checking, and readable code instead of loosely-typed COM objects.

from sapwright import Sapwright

with Sapwright(username="jdoe", password="secret", connection_name="PRD") as session:
    session.start_transaction("va01")
    session.find_by_id("wnd[0]/usr/ctxtVBAK-AUART").text = "ZWEO"
    session.press_enter()

Features

  • Typed API - full autocomplete and static typing
  • Hands-off connection management - launches SAP Logon, reuses an open connection, logs in, handles multi-logon and forced password-change prompts, and closes cleanly on exit.
  • Smart elements - .text handles dates, comboboxes and max length; .click() does the right thing for buttons, tabs, checkboxes and radios.
  • Tables made easy - header lookup, iterable rows, absolute row indexing, row/column selection, scrolling.
  • Specific exceptions - a clear SAPGuiError hierarchy instead of raw COM errors.
  • Recorder-friendly - PascalCase aliases and COM attribute delegation keep recorded SAP scripts working.

Requirements

  • Windows (SAP GUI Scripting is COM-based)
  • Python 3.11+
  • SAP Logon 770+ with scripting enabled

Installation

pip install sapwright
# or
uv add sapwright

SAP GUI setup

In SAP GUI Options:

  1. Accessibility & Scripting → Scripting
    • Check Enable Scripting
    • Uncheck Notify when a script attaches to SAP GUI
    • Uncheck Notify when a script opens a connection
  2. Security → Security Configuration
    • Set Status to Disabled

Usage

Connecting

The context manager connects on enter and closes the session on exit either using connection_name or connection_string (click here to read docs)

with Sapwright(
    username="jdoe",
    password="secret",
    connection_name="PRD",  # or connection_string="..."
    client=100,
    language="EN",
) as session:
    ...

For finer control, call connect() and close_connection() yourself:

sap = Sapwright(username="jdoe", password="secret", connection_name="PRD")
session = sap.connect()
try:
    session.start_transaction("va01")
finally:
    session.close_connection()

If a password change is forced at login, pass a password_generator. Make sure your password_generator returns a valid string. This handles automatic password management/reset.

Sapwright(..., password_generator=lambda: date.today().strftime("%b@%Y"))

Finding elements

from sapwright.objects import GuiTableControl

field = session.find_by_id("wnd[0]/usr/ctxtVBAK-AUART")
field.text = "ZWEO"

# Typed lookup: validates the SAP element type and returns a typed wrapper
table = session.find_by_id("wnd[0]/usr/tblITEMS", expected_type=GuiTableControl)

# Optional lookup: returns None instead of raising
ok = session.find_by_id("wnd[1]/usr/btnSPOP-OPTION1", raise_error=False)
if ok:
    ok.click()

Tables

print(table.headers)  # {"material": 0, "quantity": 1, ...}
print(table.row_count, table.visible_row_count)

print(
    table.vertical_scrollbar.maximum
)  # You can also use this to find number of rows in the table, Often find this more accurate than table.row_count

for row in table.rows:
    for cell in row:
        print(cell.name, cell.text)

col = table.headers["material"]  # header keys are lowercase
print(table.get_cell(0, col).text)
table.set_cell_text(0, col, "MAT-100")
row = table.get_absolute_row(250)  # scrolls if the row isn't visible

Status bar, keys and popups

from sapwright.exceptions import SAPTransactionError
from sapwright.types import VKey

try:
    session.start_transaction("bad_tcode")
except SAPTransactionError as e:
    print(e)

msg = session.statusbar_msg()
if msg.type == "E":
    print(f"Error {msg.number}: {msg.text}")

session.raise_for_status(message="Order creation failed")  # raises on type "E"

session.send_vkey(VKey.CTRL_S)  # save
session.dismiss_popups(limit=5)  # clear popup dialogs

API reference

Sapwright

from sapwright import Sapwright

Sapwright(
    username: str,
    password: str,
    connection_string: str | None = None,
    connection_name: str | None = None,
    client: int | None = None,
    language: str | None = None,
    terminate_other_sessions: bool = True,
    exe_path: str | Path | None = None,
    password_generator: Callable[[], str] | None = None,
)

One of connection_string or connection_name is required (ValueError otherwise). When opening a new connection, connection_string wins. If SAP Logon isn't running it is launched from the registry path, or from exe_path.

Parameter Description
terminate_other_sessions On the "already logged on" prompt, terminate other sessions. IfFalse, raises SAPLoginError.
password_generator Called to produce a new password when SAP forces a change. Defaults to month and year, e.g."Jan@2026".
Method Description
connect() -> GuiSession Attach to the user's existing connection, or open a new one, then log in.
close_connection() Close the SAP connection and all its sessions.
__enter__ / __exit__ Callconnect(), then close the session on exit.

GuiComponent

Wrapper for any SAP GUI element. Unknown attributes are delegated to the underlying COM object.

Properties: id, name, type, children, changeable, modified, tooltip, default_tooltip, icon_name, is_symbol_font, width, height, left, top, screen_left, screen_top, and text (get/set).

Method Description
set_text(value, raise_error=False, date_format="%d.%m.%Y", set_focus=False, strip=False) -> bool Set text. Formats dates, selects combobox entries by visible text, truncates to max length. ReturnsFalse if not changeable, or raises SAPElementNotChangeable when raise_error=True.
get_text(strip=False) -> str Read text, optionally stripped.
click() -> bool Press, select, or toggle based on element type (button, tab, checkbox, radio).
press() / select() Explicit press / select.
send_vkey(vkey) Send a virtual key to the element.
set_focus() Focus the element.
visualize(on=True) Draw or remove a red highlight frame.
GuiComponent.matches(com_object) -> bool Class method; whether a COM object fits this wrapper.

GuiSession

Returned by Sapwright.connect(). Extends GuiComponent.

Elements and navigation

Method Description
find_by_id(id, raise_error=True, *, expected_type=None) Find an element. RaisesSAPElementNotFound or SAPElementTypeMismatch. Returns None if not found and raise_error=False.
start_transaction(tcode) Start a transaction (like/n<tcode>). Raises SAPTransactionError if it doesn't exist.
end_transaction() End the current transaction (like/n).
send_command(command) Run a command as if typed in the command field.
title() -> str Title bar text.
maximize() Maximize the main window.

Keys and popups

Method Description
send_vkey(vkey, window_index=0, repeat_count=1) Send aVKey to a window.
press_enter(window_index=0, repeat_count=1) Send Enter.
dismiss_popups(key=VKey.ENTER, window_index=1, limit=None) -> int Dismiss stacked popups; returns the count dismissed.

Status bar

Method Description
statusbar_msg() -> StatusBarMsg Current status bar message.
raise_for_status(message=None, exception=SAPStatusBarError) -> StatusBarMsg Raise if the message type is"E"; otherwise return the message.

Session lifecycle

Method Description
create_session() Open a new session (like/o).
close(window_depth=10) Close this session, confirming the logoff dialog.
close_connection() / close_all_sessions() Close the connection and all its sessions. Unsaved changes are lost.
lock_session_ui() / unlock_session_ui() Block or allow user interaction.
get_vkey_description(vkey) -> str Readable name of a virtual key.

Properties: info (SessionInfo), busy, is_active, error_list, progress_percent, progress_text, is_listbox_active, listbox_curr_entry, passport_system_id, passport_pre_system_id, passport_transaction_id, show_dropdown_keys (get/set), test_tool_mode (get/set).

GuiTableControl

from sapwright.objects import GuiTableControl

Properties: headers (lowercase title → column index), rows, columns, row_count, visible_row_count, current_row, current_col, horizontal_scrollbar, vertical_scrollbar, table_field_name.

Method Description
get_cell(row, column, expected_type=None) Get a cell by zero-based indexes, optionally typed.
set_cell_text(row, column, value, strip=False, date_format="%d.%m.%Y", set_focus=False, raise_error=True) Set a cell's text.
get_absolute_row(index) -> GuiTableRow Row by absolute index, independent of scroll position. Scrolls if needed.
select_all_columns() / deselect_all_columns() Column selection.
reorder_table(permutation) Reorder columns (e.g."1 0 2"). Fixed columns can't move.
get_table_headers() -> dict[str, int] Rebuild the header map.
refresh() Re-resolve a stale table reference after scrolling or pagination.

GuiTableRow — iterable and indexable over its cells. Properties: count, length, selectable, selected (get/set). Methods: select_row(), deselect_row(), toggle_select_row().

GuiTableColumn — properties: title, selected (get/set), count, fixed, length.

GuiScrollbar — minimum, maximum, page_size, range, position (get/set).

GuiShell

For shell-based controls (GridView, TextEdit, Picture, ...).

Properties: sub_type, drag_drop_supported, acc_description, handle. Methods: select_context_menu_item(function_code), select_context_menu_item_by_position(position_desc), select_context_menu_item_by_text(text).

Models

from sapwright.models import StatusBarMsg, SessionInfo (dataclasses)

  • StatusBarMsg — id, type, text, number, has_longtext, is_popup.
  • SessionInfo — user, client, language, transaction, program, screen_number, system_name, application_server, message_server, group, session_number, system_session_id, response_time, interpretation_time, round_trips, flushes, codepage, gui_codepage, i18n_mode, is_low_speed_connection, ui_guideline, and scripting-mode flags.

Types

from sapwright.types import VKey, GuiComponentType

  • VKey — IntEnum of SAP virtual keys (Enter, F1–F12, Ctrl/Shift combinations, e.g. VKey.CTRL_S).
  • GuiComponentType — StrEnum of every SAP GUI element type.

Exceptions

from sapwright.exceptions import ... — all inherit from SAPGuiError.

Exception Raised when
SAPLogonError SAP Logon can't be launched or used.
SAPConnectionError A connection can't be found or opened.
SAPScriptingDisabled Scripting is disabled in SAP GUI.
SAPLoginError Login fails, or multi-logon is hit withterminate_other_sessions=False.
SAPElementNotFound An element ID doesn't resolve.
SAPElementTypeMismatch An element's type doesn't matchexpected_type.
SAPElementNotChangeable Writing to a read-only element.
SAPComboBoxOptionNotFound No combobox entry matches the given text.
SAPStatusBarError raise_for_status finds an error message.
SAPTransactionError A transaction code doesn't exist.
SAPTableConfigurationError A table control is misconfigured.

Metadata

Release files for sapwright 0.0.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for sapwright 0.0.4
File Size Uploaded Provenance Actions
sapwright-0.0.4.tar.gz 51.1 kB Without provenance Details for sapwright-0.0.4.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for sapwright 0.0.4
File Interpreter ABI Platform Actions
sapwright-0.0.4-py3-none-any.whl Python 3 none any Details for sapwright-0.0.4-py3-none-any.whl

Total release size: 90.0 kB

Release files / sapwright-0.0.4.tar.gz

Download URL sapwright-0.0.4.tar.gz
Size 51.1 kB
Tags Source
SHA-256 checksum
How to use checksums
654642bcc73381bc6fe923f3fa20f6e5c655ae91faf774ea19b07b404bf8ea50
BLAKE2b-256 checksum
How to use checksums
30c7711ad834e8f16523059189931fd45766f00b595026560df40774124e989c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.13.9 {"installer":{"name":"uv","version":"0.13.9","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / sapwright-0.0.4-py3-none-any.whl

Download URL sapwright-0.0.4-py3-none-any.whl
Size 38.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
2da7347ae32aaba92165e72a5e7deb5a8c14caf11eb59fa3bf880708ee1ca63a
BLAKE2b-256 checksum
How to use checksums
8e4242e48720671fa2de5b1d93a17d22dbfea31010859c41926465396fbc7d7f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.13.9 {"installer":{"name":"uv","version":"0.13.9","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Security for sapwright 0.0.4

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page