How to use Application.IsSandboxed in the xlwings API way

The IsSandboxed property of the Application object in Excel’s object model is a read-only Boolean property that indicates whether the current instance of Excel is running in a sandboxed environment. This is particularly relevant for security contexts, such as when Excel is embedded within a web browser or running under certain restricted permissions, like in Office Online or protected view scenarios. In a sandboxed environment, certain operations may be limited or disabled to enhance security, such as accessing external data sources or executing macros. Understanding this property can help developers write more robust and secure code by conditionally enabling or disabling features based on the runtime environment.

In xlwings, you can access this property through the Application object, which is part of the xlwings.App class when interacting with Excel instances. The syntax for accessing the IsSandboxed property in xlwings is straightforward, as it mirrors the Excel object model. Here’s how you can call it:

  • Syntax: app.api.IsSandboxed
  • app: This is an instance of xlwings.App, representing the Excel application.
  • api: This attribute provides direct access to the underlying Excel object model, allowing you to call properties and methods that are not directly wrapped by xlwings.
  • IsSandboxed: The property name, which returns a Boolean value (True if Excel is sandboxed, False otherwise).

No parameters are required for this property, as it is a simple property getter. The return value is a Python Boolean, which you can use in conditional statements. It’s important to note that this property may not be available in all versions of Excel; typically, it is supported in newer versions (e.g., Excel 2013 and later) and specific environments. If you try to access it in an unsupported version, you might encounter an AttributeError. To handle this gracefully, you can use error handling or check the Excel version beforehand.

Here’s a code example that demonstrates how to use the IsSandboxed property in xlwings to check the sandbox status of an Excel instance:

import xlwings as xw

# Connect to the active Excel instance or create a new one
app = xw.apps.active if xw.apps.active else xw.App()

try:
    # Access the IsSandboxed property via the api attribute
is_sandboxed = app.api.IsSandboxed
    print(f"Excel is running in a sandboxed environment: {is_sandboxed}")

    # Use the property in a conditional statement
    if is_sandboxed:
        print("Restricted mode: Some features may be disabled.")
    else:
        print("Full mode: All features are available.")
except AttributeError:
    print("The IsSandboxed property is not supported in this version of Excel.")
finally:
    # Clean up: close the app if it was created in this script
    if not xw.apps.active:
        app.quit()

June 17, 2026 (0)


Leave a Reply

Your email address will not be published. Required fields are marked *