{"id":2142,"date":"2026-06-23T16:40:41","date_gmt":"2026-06-23T08:40:41","guid":{"rendered":"https:\/\/xlwings.net\/blog\/?p=2142"},"modified":"2026-03-28T08:01:09","modified_gmt":"2026-03-28T08:01:09","slug":"how-to-use-applicationmeasurementunit-in-the-xlwings-api-way","status":"publish","type":"post","link":"https:\/\/xlwings.net\/blog\/how-to-use-applicationmeasurementunit-in-the-xlwings-api-way\/","title":{"rendered":"How to use Application.MeasurementUnit in the xlwings API way"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">The <strong>MeasurementUnit<\/strong> property of the <strong>Application<\/strong> object in Excel is a relatively niche but useful feature when dealing with international or regional document settings. This property allows you to retrieve or set the default measurement unit used in the Excel application for various interface elements, such as ruler units, column widths, row heights, and dialog box measurements. The primary utility lies in ensuring consistency when macros or automated processes depend on specific unit systems, especially when sharing workbooks across different regional versions of Excel. For instance, a macro designed assuming inches for column widths might behave unexpectedly if the application is set to centimeters. By programmatically controlling the <strong>MeasurementUnit<\/strong> property, you can standardize the environment, enhancing the reliability of your xlwings automation scripts.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Syntax and Parameters<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In xlwings, you access this property through the <code>Application<\/code> object. The property is both readable and writable, accepting integer values that correspond to specific measurement units.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>xlwings API Call Format:<\/strong><\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code># To get the current measurement unit\ncurrent_unit = xw.apps.active.api.MeasurementUnit\n\n# To set the measurement unit\nxw.apps.active.api.MeasurementUnit = new_unit_value<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><em>Note: The <code>.api<\/code> attribute provides direct access to the underlying Excel VBA object model.<\/em><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Parameter Values:<\/strong><br>The <code>new_unit_value<\/code> is an integer from the <strong>XlMeasurementUnit<\/strong> enumeration. The common values are:<\/li>\n<\/ul>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th class=\"has-text-align-left\" data-align=\"left\">Constant Name (VBA)<\/th><th class=\"has-text-align-left\" data-align=\"left\">Value<\/th><th class=\"has-text-align-left\" data-align=\"left\">Description<\/th><\/tr><\/thead><tbody><tr><td class=\"has-text-align-left\" data-align=\"left\"><strong>xlInches<\/strong><\/td><td class=\"has-text-align-left\" data-align=\"left\">0<\/td><td class=\"has-text-align-left\" data-align=\"left\">Measurement is in inches.<\/td><\/tr><tr><td class=\"has-text-align-left\" data-align=\"left\"><strong>xlCentimeters<\/strong><\/td><td class=\"has-text-align-left\" data-align=\"left\">1<\/td><td class=\"has-text-align-left\" data-align=\"left\">Measurement is in centimeters.<\/td><\/tr><tr><td class=\"has-text-align-left\" data-align=\"left\"><strong>xlMillimeters<\/strong><\/td><td class=\"has-text-align-left\" data-align=\"left\">2<\/td><td class=\"has-text-align-left\" data-align=\"left\">Measurement is in millimeters.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">You can use either the integer values directly or, for better code readability, define the constants in your Python script (e.g., <code>xlInches = 0<\/code>).<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Code Examples<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Here are practical examples demonstrating how to use the <strong>MeasurementUnit<\/strong> property with xlwings.<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Retrieving the Current Measurement Unit:<\/strong><br>This script checks the current setting and prints a descriptive message.<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>import xlwings as xw\n\n# Connect to the active Excel instance\napp = xw.apps.active\n\n# Get the current measurement unit\nunit_constant = app.api.MeasurementUnit\n\n# Interpret the value\nunit_map = {0: \"Inches\", 1: \"Centimeters\", 2: \"Millimeters\"}\nunit_name = unit_map.get(unit_constant, \"Unknown Unit\")\n\nprint(f\"The current application measurement unit is: {unit_name} (Value: {unit_constant})\")<\/code><\/pre>\n\n\n\n<ol start=\"2\" class=\"wp-block-list\">\n<li><strong>Setting the Measurement Unit and Applying a Change:<\/strong><br>This example changes the global measurement unit to centimeters and then adjusts the width of the first column accordingly. This showcases how the property affects subsequent actions.<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>import xlwings as xw\n\n# Define constants for clarity (these are not built into xlwings)\nxlInches = 0\nxlCentimeters = 1\nxlMillimeters = 2\n\napp = xw.apps.active\nwb = app.books.active\nws = wb.sheets&#91;0]\n\n# Set the application's measurement unit to Centimeters\napp.api.MeasurementUnit = xlCentimeters\nprint(\"Measurement unit set to Centimeters.\")\n\n# Now, set the width of column A to 5 centimeters.\n# The .column_width property in xlwings uses the application's current MeasurementUnit.\nws.range('A:A').column_width = 5\nprint(\"Column A width set to 5 centimeters.\")\n\n# Optional: Switch back to Inches and read the column width\napp.api.MeasurementUnit = xlInches\nwidth_in_inches = ws.range('A:A').column_width\nprint(f\"Column A width in inches is approximately: {width_in_inches:.2f}\")<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The **MeasurementUnit** property of the **Application** object in Excel is a relatively niche but us&#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-2142","post","type-post","status-publish","format-standard","hentry","category-xlwings-api-reference"],"_links":{"self":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2142","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=2142"}],"version-history":[{"count":1,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2142\/revisions"}],"predecessor-version":[{"id":3276,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2142\/revisions\/3276"}],"wp:attachment":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/media?parent=2142"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/categories?post=2142"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/tags?post=2142"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}