{"id":2038,"date":"2026-05-02T15:10:25","date_gmt":"2026-05-02T07:10:25","guid":{"rendered":"https:\/\/xlwings.net\/blog\/?p=2038"},"modified":"2026-03-28T05:31:42","modified_gmt":"2026-03-28T05:31:42","slug":"how-to-use-applicationautomationsecurity-in-the-xlwings-api-way","status":"publish","type":"post","link":"https:\/\/xlwings.net\/blog\/how-to-use-applicationautomationsecurity-in-the-xlwings-api-way\/","title":{"rendered":"How to use Application.AutomationSecurity in the xlwings API way"},"content":{"rendered":"\n<p>The <strong>AutomationSecurity<\/strong> property of the <strong>Application<\/strong> object in Excel is a crucial setting for controlling macro security when automating Excel through external applications like Python using the xlwings library. It determines the security level that Excel uses when opening files programmatically, which can affect whether macros are enabled or disabled automatically. This property is particularly important in scenarios where automation scripts need to ensure consistent security behavior, especially in environments with varying macro settings. By setting <strong>AutomationSecurity<\/strong>, developers can programmatically override the default security settings of Excel, providing more control over macro execution during automated processes. This helps in maintaining security protocols while allowing necessary macros to run in controlled automation tasks.<\/p>\n\n\n\n<p>In xlwings, the <strong>AutomationSecurity<\/strong> property is accessed through the <code>app<\/code> object, which represents the Excel application. The syntax for setting or getting this property is straightforward, as it corresponds directly to the Excel Object Model. The property accepts integer values that correspond to specific security levels defined by Excel. The primary values are:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>1 (msoAutomationSecurityLow)<\/strong>: This setting enables all macros to run without prompting. It is useful in trusted environments but poses security risks if used with untrusted files.<\/li>\n\n\n\n<li><strong>2 (msoAutomationSecurityByUI)<\/strong>: Excel uses the macro security level set in the user interface (via Trust Center settings). This is the default behavior when automation is initiated.<\/li>\n\n\n\n<li><strong>3 (msoAutomationSecurityForceDisable)<\/strong>: This setting disables all macros automatically, regardless of the file&#8217;s trust settings. It is the most secure option, preventing any macro execution.<\/li>\n<\/ul>\n\n\n\n<p>To use this property in xlwings, you first need to instantiate an Excel application object. The property can be set before opening a workbook to influence how Excel handles macros in that file. For example, if you want to ensure macros are disabled during an automated data processing task, you can set <strong>AutomationSecurity<\/strong> to 3. Conversely, if you trust the source and need macros to run, set it to 1. It&#8217;s essential to note that changing this property affects all subsequent workbooks opened in that instance of Excel until it is changed again or the application is closed.<\/p>\n\n\n\n<p>Here is a code example demonstrating the use of <strong>AutomationSecurity<\/strong> in xlwings:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import xlwings as xw\n\n# Start a new Excel application instance\napp = xw.App(visible=False) # Run Excel in the background\n\n# Get the current AutomationSecurity setting\ncurrent_security = app.api.AutomationSecurity\nprint(f\"Current AutomationSecurity setting: {current_security}\")\n\n# Set AutomationSecurity to disable all macros (msoAutomationSecurityForceDisable)\napp.api.AutomationSecurity = 3\nprint(\"AutomationSecurity set to disable all macros.\")\n\n# Open a workbook that contains macros\nwb = app.books.open('example_with_macros.xlsx')\n\n# Perform some operations, such as reading data\ndata = wb.sheets&#91;'Sheet1'].range('A1').value\nprint(f\"Data from A1: {data}\")\n\n# Set AutomationSecurity back to use UI settings (msoAutomationSecurityByUI)\napp.api.AutomationSecurity = 2\nprint(\"AutomationSecurity reset to UI default.\")\n\n# Close the workbook without saving\nwb.close()\n\n# Quit the Excel application\napp.quit()<\/code><\/pre>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The **AutomationSecurity** property of the **Application** object in Excel is a crucial setting for &#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[25],"tags":[],"class_list":["post-2038","post","type-post","status-publish","format-standard","hentry","category-xlwings-api-reference"],"_links":{"self":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2038","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/comments?post=2038"}],"version-history":[{"count":1,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2038\/revisions"}],"predecessor-version":[{"id":3115,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2038\/revisions\/3115"}],"wp:attachment":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/media?parent=2038"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/categories?post=2038"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/tags?post=2038"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}