{"id":185,"date":"2016-03-15T17:09:48","date_gmt":"2016-03-15T17:09:48","guid":{"rendered":"https:\/\/www.pyxll.com\/blog\/?p=185"},"modified":"2019-12-14T19:08:03","modified_gmt":"2019-12-14T19:08:03","slug":"getting-started-with-pyxll","status":"publish","type":"post","link":"https:\/\/www.pyxll.com\/blog\/getting-started-with-pyxll\/","title":{"rendered":"Getting started with PyXLL"},"content":{"rendered":"<p>If you&#8217;re using Enthought Canopy you can download PyXLL from the Canopy Package Manager. For all other Python users simply download PyXLL from the\u00a0<a href=\"https:\/\/www.pyxll.com\/download.html\" target=\"_blank\" rel=\"noopener noreferrer\">Download Page<\/a>\u00a0instead, where it says &#8220;Download PyXLL for Standalone Installation&#8221;.<\/p>\n<p>Once you&#8217;ve downloaded PyXLL, the rest of this post will get you up and running.<\/p>\n<p>When you download PyXLL, pay attention to what version of Python you are downloading for and whether you want the 32 bit or 64 bit version. If you are using a 32 bit version of Excel you must download the 32 bit PyXLL package and you will also need to use a 32 bit version of Python (PyXLL embeds Python into Excel, and so the processor version must be the same).<\/p>\n<p>Once you&#8217;ve done that, follow the following steps to install PyXLL:<\/p>\n<h3><strong>1. Unpack the zipfile<\/strong><\/h3>\n<p><em>Canopy users can skip this step<\/em><\/p>\n<p>PyXLL can be used with any Python distribution. Depending on how you have Python installed on your system\u00a0you may need to configure PyXLL so it knows where to find your Python installation.<\/p>\n<p>PyXLL is packaged as a zip file. Simply unpack the zip file where you want PyXLL to be installed, there\u00a0is no installer to run.<\/p>\n<h3><strong>2. Edit the config file<\/strong><\/h3>\n<p>Once you&#8217;ve unzipped the PyXLL download is to edit the file <em>pyxll.cfg\u00a0<\/em>in any text editor. The default configuration may be fine for you while you&#8217;re getting started, and you can\u00a0come back to it later if you want to make any changes.<\/p>\n<p><em>Canopy users launch Canopy and type &#8220;edit_pyxll_configuration&#8221; to open the config file.<\/em><\/p>\n<p>If your Python installation is not configured to be the default Python installation (this is common with Anaconda and PortablePython distributions) you will need set the following in your config file<\/p>\n<pre>[PYTHON]\r\nexecutable = &lt;your python.exe file here&gt;<\/pre>\n<p>If you get an error saying that Python is not installed or the Python dll can&#8217;t be found you may need to set the Python executable in the config.<\/p>\n<p>If setting the executable doesn&#8217;t resolve the problem then it&#8217;s possible your\u00a0Python dll is in a non-standard location. You can also set the dll location\u00a0in the config to tell PyXLL where to find it.<\/p>\n<pre>[PYTHON]\r\nexecutable = &lt;your python.exe file here&gt;\r\ndll = &lt;your pythonXX.dll file here - usually not needed&gt;<\/pre>\n<h3><strong>3. Install the addin<\/strong><\/h3>\n<p>Once you&#8217;re happy with the configuration you can install the addin in Excel by following the instructions<br \/>\nbelow.<\/p>\n<ul>\n<li><strong>Excel 2010 &#8211; 2016<\/strong><br \/>\nSelect the File menu in Excel and go to Options -&gt; Add-Ins -&gt; Manage Excel Addins\u00a0and browse for the folder you unpacked PyXLL to and select pyxll.xll.<\/li>\n<li><strong>Excel 2007<\/strong><br \/>\nClick the large circle at the top left of Excel and go to Options -&gt; Add-Ins -&gt; Manage Excel Addins\u00a0and browse for the folder you unpacked PyXLL to and select pyxll.xll.<\/li>\n<li><strong>Excel 97 &#8211; 2003<\/strong><br \/>\nGo to Tools -&gt; Add-Ins -&gt; Browse and locate pyxll.xll in the folder you unpacked the zip file to.<\/li>\n<\/ul>\n<p><a href=\"https:\/\/www.pyxll.com\/blog\/wp-content\/uploads\/2016\/02\/add_addin.png\" rel=\"attachment wp-att-186\"><img decoding=\"async\" data-attachment-id=\"186\" data-permalink=\"https:\/\/www.pyxll.com\/blog\/getting-started-with-pyxll\/add_addin\/\" data-orig-file=\"https:\/\/www.pyxll.com\/blog\/wp-content\/uploads\/2016\/02\/add_addin.png\" data-orig-size=\"779,461\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"add_addin\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/www.pyxll.com\/blog\/wp-content\/uploads\/2016\/02\/add_addin.png\" class=\"alignnone wp-image-186\" src=\"https:\/\/www.pyxll.com\/blog\/wp-content\/uploads\/2016\/02\/add_addin.png\" alt=\"add_addin\" width=\"803\" height=\"475\" srcset=\"https:\/\/www.pyxll.com\/blog\/wp-content\/uploads\/2016\/02\/add_addin.png 779w, https:\/\/www.pyxll.com\/blog\/wp-content\/uploads\/2016\/02\/add_addin-300x178.png 300w, https:\/\/www.pyxll.com\/blog\/wp-content\/uploads\/2016\/02\/add_addin-768x454.png 768w\" sizes=\"(max-width: 803px) 100vw, 803px\" \/><\/a><\/p>\n<h3>4. <strong><em>Optional<\/em> Install the PyXLL stubs package<\/strong><\/h3>\n<p>If you are using a Python IDE that provides autocompletion or code checking, because the pyxll module isn&#8217;t\u00a0installed, you may find it complains and won&#8217;t be able to provide any autocompletion<br \/>\nfor functions imported from the pyxll module.<\/p>\n<p>In the downloaded zip file you will find a <code>.whl<\/code> file. The exact filename varies depending on the version\u00a0of PyXLL you&#8217;ve downloaded. That&#8217;s a Python Wheel containing a dummy pyxll module\u00a0that you can use for the purposes of keeping your IDE from complaining, and for writing code that<br \/>\ndepends on the pyxll module that you want to use outside of Excel (e.g. when unit testing).<\/p>\n<p>To install the wheel run the following command (substituting the actual wheel filename)\u00a0from a command line::<\/p>\n<pre>&gt; pip install {pyxll wheel filename}<\/pre>\n<p>The real pyxll module is compiled into the pyxll.xll addin.<\/p>\n<p>If you are using a version of Python that isn&#8217;t supported by pip all you need to do is to unzip the\u00a0wheel file into your Python site-packages folder (the wheel file is simply a zip file with a different\u00a0file extension).<\/p>\n<h2>Calling a Python Function in Excel<\/h2>\n<p>One of the main features of PyXLL is being able to call a Python function from a formula in an Excel\u00a0workbook.<\/p>\n<p>For this tutorial we&#8217;ll use a simple recursive form of the well known fibonacci function.<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\ndef fibonacci(n):\r\n    assert n &gt;= 0, &quot;n must be &gt;= 0&quot;\r\n    if n == 0:\r\n        return 0\r\n    if n == 1:\r\n        return 1\r\n    return fibonacci(n-1) + fibonacci(n-2)\r\n<\/pre>\n<p>Save that to a file called <em>fibonacci.py<\/em> and try calling it from a Python prompt to check it works correctly.<\/p>\n<pre>In [1]: from fibonacci import fibonacci\r\nIn [2]: fibonacci(5)\r\nOut[2]: 5\r\nIn [3]: fibonacci(10)\r\nOut[3]: 55\r\n<\/pre>\n<p>To expose this function to Excel using PyXLL, all that&#8217;s needed is to apply the\u00a0<em>xl_func<\/em> decorator.<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nfrom pyxll import xl_func\r\n\r\n@xl_func\r\ndef fibonacci(n):\r\n    assert n &gt;= 0, &quot;n must be &gt;= 0&quot;\r\n    if n == 0:\r\n        return 0\r\n    if n == 1:\r\n        return 1\r\n    return fibonacci(n-1) + fibonacci(n-2)\r\n<\/pre>\n<p>Now add the fibonacci module to the PyXLL config:<\/p>\n<pre>[PYXLL]\r\nmodules = fibonacci\r\n\r\n[PYTHON]\r\npythonpath = &lt;path where you saved fibonacci.py&gt;<\/pre>\n<p>Reload PyXLL by going to the Addins menu in Excel and selecting PyXLL -&gt; Reload.<\/p>\n<p>Now in a worksheet you can type a formula using your new Python function.<\/p>\n<pre>=fibonacci(10)<\/pre>\n<p>If you make any mistakes in your code or your function returns an error you can check the log file\u00a0to find out what the error was, make and necessary changes to your code and reload PyXLL again.<\/p>\n<p>This function is not by any means an optimal solution to the fibonacci problem, and in fact it&#8217;s so slow that you may find that if you try and calculate it for anything other than a very small number it takes a long time! In Excel, you can abort a calculation by pressing the Esc (escape) key . PyXLL will raise a KeyboardInterupt exception which will stop the Python function early, <em>but only if <strong>allow_abort<\/strong> is enabled.\u00a0<\/em>This can be done on a function by function basis by passing allow_abort to xl_func, or globally by setting it in the config file:<\/p>\n<pre>[PYXLL]\r\nallow_abort = 1<\/pre>\n<h2>Next Steps<\/h2>\n<p>The <a href=\"https:\/\/www.pyxll.com\/docs\/userguide\/index.html\" target=\"_blank\" rel=\"noopener noreferrer\">User Guide<\/a>\u00a0has comprehensive coverage of how to use all the features of PyXLL and\u00a0a quick look through there will quickly bring you up to speed on how to do most things with PyXLL.<\/p>\n<p>Many of PyXLL&#8217;s features are demonstrated in the examples included in download. These are a good place to\u00a0start to learn more about what PyXLL can do.<\/p>\n<p>More example code can be found on PyXLL&#8217;s GitHub page <a href=\"https:\/\/github.com\/pyxll\" target=\"_blank\" rel=\"noopener noreferrer\">https:\/\/github.com\/pyxll.<\/a><\/p>\n<p>If there is anything specifically you&#8217;re trying to achieve and can&#8217;t find an example or help in\u00a0the user guide please <a href=\"https:\/\/www.pyxll.com\/contact.html\" target=\"_blank\" rel=\"noopener noreferrer\">contact us<\/a> and we will do our best to help.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>PyXLL is a tool that allows you to build rich Excel spreadsheets using Python. Using PyXLL you can expose your Python code to Excel as functions that can be called directly from Excel spreadsheets, or as menu items or macros. PyXLL supports advanced features like real time data integration and ribbon customisations.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[2],"tags":[],"class_list":["post-185","post","type-post","status-publish","format-standard","hentry","category-pyxll"],"acf":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p7l3LP-2Z","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.pyxll.com\/blog\/wp-json\/wp\/v2\/posts\/185","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.pyxll.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.pyxll.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.pyxll.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pyxll.com\/blog\/wp-json\/wp\/v2\/comments?post=185"}],"version-history":[{"count":2,"href":"https:\/\/www.pyxll.com\/blog\/wp-json\/wp\/v2\/posts\/185\/revisions"}],"predecessor-version":[{"id":1092,"href":"https:\/\/www.pyxll.com\/blog\/wp-json\/wp\/v2\/posts\/185\/revisions\/1092"}],"wp:attachment":[{"href":"https:\/\/www.pyxll.com\/blog\/wp-json\/wp\/v2\/media?parent=185"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.pyxll.com\/blog\/wp-json\/wp\/v2\/categories?post=185"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.pyxll.com\/blog\/wp-json\/wp\/v2\/tags?post=185"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}