<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>hello world &#8211; tutolibro.org</title>
	<atom:link href="https://tutolibro.org/tag/hello-world/feed/" rel="self" type="application/rss+xml" />
	<link>https://tutolibro.org</link>
	<description>Free online tutorials</description>
	<lastBuildDate>Thu, 27 Nov 2025 22:59:18 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.8.3</generator>

<image>
	<url>https://tutolibro.org/wp-content/uploads/2020/02/cropped-logo_mini-32x32.png</url>
	<title>hello world &#8211; tutolibro.org</title>
	<link>https://tutolibro.org</link>
	<width>32</width>
	<height>32</height>
</image> 
<site xmlns="com-wordpress:feed-additions:1">173775150</site>	<item>
		<title>Assign Your Python Macro to a Button in LibreOffice Calc</title>
		<link>https://tutolibro.org/2025/11/26/assign-your-python-macro-to-a-button-in-libreoffice/</link>
					<comments>https://tutolibro.org/2025/11/26/assign-your-python-macro-to-a-button-in-libreoffice/#respond</comments>
		
		<dc:creator><![CDATA[gweno]]></dc:creator>
		<pubDate>Wed, 26 Nov 2025 16:03:15 +0000</pubDate>
				<category><![CDATA[Calc]]></category>
		<category><![CDATA[LibreOffice]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[calc]]></category>
		<category><![CDATA[hello world]]></category>
		<category><![CDATA[libreoffice]]></category>
		<category><![CDATA[python]]></category>
		<guid isPermaLink="false">https://tutolibro.org/?p=1555</guid>

					<description><![CDATA[Assign the macro to a LibreOffice Control Button. When you write a macro within libreOffice, you will quickly realise something. It&#8217;s not really convenient to execute macros through the Macro Manager. This is because you need to access that menu,...<br /><a class="read-more-button" href="https://tutolibro.org/2025/11/26/assign-your-python-macro-to-a-button-in-libreoffice/">Read more</a>]]></description>
										<content:encoded><![CDATA[
<h4 class="wp-block-heading" id="top"> Assign the macro to a LibreOffice Control Button.</h4>



<p id="switch">When you write a macro within libreOffice, you will quickly realise something. It&#8217;s not really convenient to execute macros through the Macro Manager. This is because you need to access that menu, and it takes a few clicks to do so. (Quickest way to access the APSO Macro manager being [Alt] + [Shift] + [F11]). Creating a button on your spreadsheet is easy. You can assign this button to your macro. When you click on the button, it will execute the macro.<br>So first we&#8217;ll <strong><a href="#switch">Switch to Design Mode</a></strong>. This allows us to <strong><a href="#button">create a button</a></strong> on the spreadsheet. Then, we&#8217;ll <strong><a href="#assign">assign the python macro</a></strong> to this button.</p>



<p><strong><span style="text-decoration: underline;">Switch to Design Mode</span></strong>:                                                                                                            (<a href="#top">Back to Top</a>)</p>



<p>The first step is to set the document to &#8216;Design Mode&#8217; . Select &#8216;Design Mode&#8217; From &#8216;Menu Tools/Forms/Design Mode&#8217;.</p>



<figure class="wp-block-image size-full" id="button"><img fetchpriority="high" decoding="async" width="555" height="463" src="https://tutolibro.org/wp-content/uploads/2025/11/lopy-designMode-1.png" alt="" class="wp-image-1606" srcset="https://tutolibro.org/wp-content/uploads/2025/11/lopy-designMode-1.png 555w, https://tutolibro.org/wp-content/uploads/2025/11/lopy-designMode-1-300x250.png 300w" sizes="(max-width: 555px) 100vw, 555px" /></figure>



<p><strong><span style="text-decoration: underline;">Create a button</span></strong>:                                                                                                            (<a href="#top">Back to Top</a>)<br>Then simply by clicking on Menu &#8216;<strong>Insert>Form Controls>Push Button&#8217;</strong>:</p>



<figure class="wp-block-image size-full"><img decoding="async" width="492" height="730" src="https://tutolibro.org/wp-content/uploads/2025/11/lopy-InsertPushButtonMenu.png" alt="" class="wp-image-1605" srcset="https://tutolibro.org/wp-content/uploads/2025/11/lopy-InsertPushButtonMenu.png 492w, https://tutolibro.org/wp-content/uploads/2025/11/lopy-InsertPushButtonMenu-202x300.png 202w" sizes="(max-width: 492px) 100vw, 492px" /></figure>



<p>Once you have the little cross to insert the button, click for the button&#8217;s upper left corner. Then hold and release where you want the bottom right corner of the button.</p>



<figure class="wp-block-image size-large is-resized" id="assign"><img decoding="async" width="124" height="84" src="https://tutolibro.org/wp-content/uploads/2019/08/pyCalc11.png" alt="" class="wp-image-858"/></figure>



<p><strong><span style="text-decoration: underline;">Assign the python macro</span></strong><span style="text-decoration: underline;">:</span>                                                                                                            (<a href="#top">Back to Top</a>)<br>Once you have the button displayed, right click on it and select &#8216;Control&#8230;&#8217;</p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="241" height="373" src="https://tutolibro.org/wp-content/uploads/2019/08/pyCalc12.png" alt="" class="wp-image-859" srcset="https://tutolibro.org/wp-content/uploads/2019/08/pyCalc12.png 241w, https://tutolibro.org/wp-content/uploads/2019/08/pyCalc12-194x300.png 194w" sizes="auto, (max-width: 241px) 100vw, 241px" /><figcaption class="wp-element-caption">Select &#8216;Control&#8217; from the new Button contextual menu</figcaption></figure>



<p>Then in the window &#8216;Properties&#8217; select the &#8216;Events&#8217; tab, scroll down a bit to find in the list of events the one called &#8216;Mouse button pressed&#8217;. On the right of this &#8216;Mouse button pressed&#8217; entry click on the button &#8216;[&#8230;]&#8217;.</p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="564" height="430" src="https://tutolibro.org/wp-content/uploads/2019/08/pyCalc13.png" alt="" class="wp-image-860" srcset="https://tutolibro.org/wp-content/uploads/2019/08/pyCalc13.png 564w, https://tutolibro.org/wp-content/uploads/2019/08/pyCalc13-300x229.png 300w" sizes="auto, (max-width: 564px) 100vw, 564px" /></figure>



<p>A window named  &#8220;Assign Action&#8217; pops up, click on the button &#8216;[Macro]&#8217; on the right hand side of this window.</p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="592" height="373" src="https://tutolibro.org/wp-content/uploads/2019/08/pyCalc14.png" alt="" class="wp-image-861" srcset="https://tutolibro.org/wp-content/uploads/2019/08/pyCalc14.png 592w, https://tutolibro.org/wp-content/uploads/2019/08/pyCalc14-300x189.png 300w" sizes="auto, (max-width: 592px) 100vw, 592px" /><figcaption class="wp-element-caption">Assign a macro to a button</figcaption></figure>



<p>You should then get the &#8216;Macro Selector&#8217; with your Macro Libraries. Simply go to the library where your macro is saved and select the macro &#8216;HelloWorld&#8217;,then press &#8216;OK&#8217;.</p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="722" height="202" src="https://tutolibro.org/wp-content/uploads/2019/08/pyCalc15-e1565986476973.png" alt="" class="wp-image-862" srcset="https://tutolibro.org/wp-content/uploads/2019/08/pyCalc15-e1565986476973.png 722w, https://tutolibro.org/wp-content/uploads/2019/08/pyCalc15-e1565986476973-300x84.png 300w" sizes="auto, (max-width: 722px) 100vw, 722px" /><figcaption class="wp-element-caption">Select the macro from Macro Selector</figcaption></figure>



<p>Then &#8216;OK&#8217; again in &#8216;Assign Macro&#8217; window, than close the &#8216;Properties&#8217; window.<br>The last thing to do is exit the &#8216;Design&#8217; mode. This allows you to click on the button. Do that by going to &#8216;Tools&gt;Forms&gt;Design Mode&#8217;.</p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="461" height="541" src="https://tutolibro.org/wp-content/uploads/2019/08/pyCalc17.png" alt="" class="wp-image-864" srcset="https://tutolibro.org/wp-content/uploads/2019/08/pyCalc17.png 461w, https://tutolibro.org/wp-content/uploads/2019/08/pyCalc17-256x300.png 256w" sizes="auto, (max-width: 461px) 100vw, 461px" /><figcaption class="wp-element-caption">Set design mode off</figcaption></figure>



<p>And now when you click on the button&#8230; tada&#8230;<br>If you get this ERROR!</p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="546" height="255" src="https://tutolibro.org/wp-content/uploads/2019/08/pyCalc16.png" alt="" class="wp-image-863" srcset="https://tutolibro.org/wp-content/uploads/2019/08/pyCalc16.png 546w, https://tutolibro.org/wp-content/uploads/2019/08/pyCalc16-300x140.png 300w" sizes="auto, (max-width: 546px) 100vw, 546px" /><figcaption class="wp-element-caption">Error: missing argument</figcaption></figure>



<p>The function HelloWorld requires all information from LibreOffice Context to work. This information is needed in the form of an argument. So we need to pass the argument &#8216;*args&#8217; in your function called by the Push Button.<br>For example in the program HelloWorld done in a previous post, in your code instead of:<br><code>def HelloWorld():</code><br>You should have:<br><code>def HelloWorld(*args):</code><br>Save your program again and then when you press on the button the cell A1 will display &#8216;Hello World!&#8217; !!!</p>



<p>So there is the final version of the macro:</p>



<hr class="wp-block-separator has-css-opacity"/>



<pre class="wp-block-code"><code># Created by Gweno 16/08/2019 for tutolibro.tech
# This program displays 'Hello World!" in cell A1 of the 
# current Calc document.

def HelloWorld(*args):
    """Write 'Hello World!' in Cell A1"""
    
# get the doc from the scripting context 
# which is made available to all scripts
    desktop = XSCRIPTCONTEXT.getDesktop()
    model = desktop.getCurrentComponent()
    
# access the active sheet
    active_sheet = model.CurrentController.ActiveSheet

# write 'Hello World' in A1
    active_sheet.getCellRangeByName("A1").String = "Hello World!"</code></pre>



<p>You can clone or download this code from gitHub <a href="https://github.com/Gweno/tutolibro.tech/tree/master/lopy/part2">here</a>.</p>



<hr class="wp-block-separator has-css-opacity"/>



<h3 class="wp-block-heading">Good luck! and leave a comment if you are having problem to have this macro to work.</h3>



<hr class="wp-block-separator has-alpha-channel-opacity is-style-default"/>



<p>If you like this tutorial, you could <a href="https://buymeacoffee.com/tutolibro" data-type="link" data-id="https://buymeacoffee/tutolibro" target="_blank" rel="noreferrer noopener">buy me a coffee</a> to help me continue writing add-free tutorials.</p>



<p>Thank you!</p>



<div class="wp-block-group is-content-justification-space-between is-nowrap is-layout-flex wp-container-core-group-is-layout-0dfbf163 wp-block-group-is-layout-flex">

</div>
]]></content:encoded>
					
					<wfw:commentRss>https://tutolibro.org/2025/11/26/assign-your-python-macro-to-a-button-in-libreoffice/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">1555</post-id>	</item>
		<item>
		<title>Embedding vs. Storing Python Macros in LibreOffice</title>
		<link>https://tutolibro.org/2025/11/19/embedding-vs-storing-python-macros-in-libreoffice/</link>
					<comments>https://tutolibro.org/2025/11/19/embedding-vs-storing-python-macros-in-libreoffice/#respond</comments>
		
		<dc:creator><![CDATA[gweno]]></dc:creator>
		<pubDate>Wed, 19 Nov 2025 23:22:50 +0000</pubDate>
				<category><![CDATA[Calc]]></category>
		<category><![CDATA[LibreOffice]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[calc]]></category>
		<category><![CDATA[hello world]]></category>
		<category><![CDATA[libreoffice]]></category>
		<category><![CDATA[python]]></category>
		<guid isPermaLink="false">https://tutolibro.org/?p=1580</guid>

					<description><![CDATA[This article helps you decide whether to embed your python macro in the file. It also explores saving it in a macro repository. With the APSO add-on (see previous article) you can embed your macro within the LibreOffice File. This...<br /><a class="read-more-button" href="https://tutolibro.org/2025/11/19/embedding-vs-storing-python-macros-in-libreoffice/">Read more</a>]]></description>
										<content:encoded><![CDATA[
<ul class="wp-block-list">
<li></li>
</ul>



<p>This article helps you decide whether to embed your python macro in the file. It also explores saving it in a macro repository.</p>



<p>With the APSO add-on (<a href="https://tutolibro.org/2019/07/26/getting-started-with-libreoffice-calc-and-python/" data-type="post" data-id="563">see previous article</a>) you can embed your macro within the LibreOffice File. This can be useful for example when you want to send your LibreOffice file with the Python Macro. A downside is that the Python macro will not be accessible outside of the LibreOffice file you sent. So you will need to open the LibreOffice file. Then open the Python Macro organiser. This is necessary to edit the Python program.</p>



<p>There are alternatives to embedding the Python program. You can place it in an organized directory while still using APSO. This lets you re-use it for any other LibreOffice document. You can also edit the program without opening the LibreOffice document if you open it from the relevant folder. If you want to send your LibreOffice Document to someone, there is a trade-off. You will also need to send the Python macro.</p>



<p>I am in favor of keeping programs in a tidy manner. This approach allows me to re-use them across many different files. But the choice is entirely up to you. I hope </p>



<p>To illustrate these 2 cases, let&#8217;s have a look at an example:</p>



<p>I have a file called &#8220;<strong>HelloWorld.ods</strong>&#8221; and two Python files: the first one is called &#8220;<strong>HelloWorld.py</strong>&#8221; and the other file is called &#8220;<strong>Testing.py&#8221;</strong>. The first python file is embedded in the LibreOffice file, the second is in the shared Python Scripts directory.</p>



<p class="has-text-align-left">In the first case scenario, after opening HelloWorld.ods, I can go to the Python Script Organizer, in Tools&gt;Macro&gt;Organize Python Scripts (shortcut Alt+Shift+F11) from the APSO extension. Alternatively I can use the LibreOffice Standard Macro Organiser in Tools&gt;Macro&gt;Organise Macros&gt;Python&#8230;.</p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="543" height="672" src="https://tutolibro.org/wp-content/uploads/2019/08/pyCalc01.png" alt="" class="wp-image-848" srcset="https://tutolibro.org/wp-content/uploads/2019/08/pyCalc01.png 543w, https://tutolibro.org/wp-content/uploads/2019/08/pyCalc01-242x300.png 242w" sizes="auto, (max-width: 543px) 100vw, 543px" /></figure>



<p class="has-text-align-left"><br>In the Python Scripts Organiser you will see that your Python Macro is listed within the LibreOffice File&#8217;s name.</p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="302" height="311" src="https://tutolibro.org/wp-content/uploads/2019/08/pyCalc02.png" alt="" class="wp-image-849" srcset="https://tutolibro.org/wp-content/uploads/2019/08/pyCalc02.png 302w, https://tutolibro.org/wp-content/uploads/2019/08/pyCalc02-291x300.png 291w" sizes="auto, (max-width: 302px) 100vw, 302px" /><figcaption class="wp-element-caption">Python Macro HelloWorld is within the calc document.</figcaption></figure>



<p><br>When you choose to edit the Python Macro, select &#8216;Menu&gt;Edit&#8217; from the &#8216;Python scripts&#8217; window. The edited file&#8217;s path is temporary. It is only used for the time of editing this Python program.</p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="646" height="182" src="https://tutolibro.org/wp-content/uploads/2019/08/pyCalc03.png" alt="" class="wp-image-850" srcset="https://tutolibro.org/wp-content/uploads/2019/08/pyCalc03.png 646w, https://tutolibro.org/wp-content/uploads/2019/08/pyCalc03-300x85.png 300w" sizes="auto, (max-width: 646px) 100vw, 646px" /></figure>



<p>So where is the Python Script saved on your hard drive?<br>As I wrote earlier, it&#8217;s embedded in the LibreOffice File. If you don&#8217;t believe me, here is a trick to find out:<br>If I rename my &#8216;HelloWorld.ods&#8217; in &#8216;HelloWorld.zip&#8217;. I can then unzip it to see the archived compressed files and folders that constitute the LibreOffice file itself(with e.g. Archive Manager on Linux, winzip on Windows, or Archive Utility on Mac). In the list of folders, I spot a folder called &#8216;Scripts&#8217;. Then, I find a sub-folder called &#8216;python&#8217;. OH SURPRISE! The python script is there!</p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="424" height="355" src="https://tutolibro.org/wp-content/uploads/2019/08/pyCalc04.png" alt="" class="wp-image-851" srcset="https://tutolibro.org/wp-content/uploads/2019/08/pyCalc04.png 424w, https://tutolibro.org/wp-content/uploads/2019/08/pyCalc04-300x251.png 300w" sizes="auto, (max-width: 424px) 100vw, 424px" /></figure>



<p> If you want to do that too, when you&#8217;re done don&#8217;t forget to rename the file back with its &#8216;.ods&#8217; extension.</p>



<p>In the second case scenario,we are saving our Python script our Python script &#8216;Testing.py&#8217; is not listed in the Macro manager within the LibreOffice File, but this time I created it as part of my Library &#8216;MyPythonLibrary&#8217; within another Library called &#8216;Tutorial&#8217;.</p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="362" height="373" src="https://tutolibro.org/wp-content/uploads/2019/08/pyCalc00.png" alt="" class="wp-image-847" srcset="https://tutolibro.org/wp-content/uploads/2019/08/pyCalc00.png 362w, https://tutolibro.org/wp-content/uploads/2019/08/pyCalc00-291x300.png 291w" sizes="auto, (max-width: 362px) 100vw, 362px" /><figcaption class="wp-element-caption">&#8216;main&#8217; is the Python function that is in the File &#8216;Testing.py&#8217;.</figcaption></figure>



<p>And this time if I edit &#8216;Testing&#8217;, I can see the path is not a temporary one. I can access it even when LibreOffice is closed.</p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="685" height="282" src="https://tutolibro.org/wp-content/uploads/2019/08/pyCalc05.png" alt="" class="wp-image-852" srcset="https://tutolibro.org/wp-content/uploads/2019/08/pyCalc05.png 685w, https://tutolibro.org/wp-content/uploads/2019/08/pyCalc05-300x124.png 300w" sizes="auto, (max-width: 685px) 100vw, 685px" /></figure>



<p>In fact your Python programs can be directly accessed on your computer at the following location:<br><span style="text-decoration: underline;">For Linux: </span><br>/home/&lt;user name&gt;/.config/libreoffice/4/user/Scripts/python<br>(to see folder &#8216;.config&#8217; you need to &#8216;Show hidden files&#8217; , the shortcut CTRL+h).<br><span style="text-decoration: underline;">For Windows:</span><br>C:\Users\&lt;user_name&gt;\AppData\Roaming\LibreOffice\4\Scripts\python<br><span style="text-decoration: underline;">For Mac:</span><br>(I think it&#8217;s)/Users/&lt;user name&gt;/Library/Application Support/LibreOffice/4/user\Scripts\python<br>(Please let me know in a comment if you know for sure what the folder is for Mac OS).</p>



<h4 class="wp-block-heading">Time for your First LibreOffice line of code!</h4>



<p>I know you can&#8217;t wait anymore! So let&#8217;s create our first program in Python in a LibreOffice Calc Document in <a href="https://tutolibro.org/2025/11/03/write-hello-world-in-libreoffice-calc-using-python/" data-type="post" data-id="1533">this next post</a>.</p>



<p>If you like this tutorial, you could <a href="https://buymeacoffee.com/tutolibro" data-type="link" data-id="https://buymeacoffee/tutolibro" target="_blank" rel="noreferrer noopener">buy me a coffee</a> to help me continue writing add-free tutorials.</p>



<p>Thank you!</p>



<div class="wp-block-group is-content-justification-space-between is-nowrap is-layout-flex wp-container-core-group-is-layout-0dfbf163 wp-block-group-is-layout-flex">

</div>



<p></p>
]]></content:encoded>
					
					<wfw:commentRss>https://tutolibro.org/2025/11/19/embedding-vs-storing-python-macros-in-libreoffice/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">1580</post-id>	</item>
		<item>
		<title>Write &#8216;Hello World!&#8221; in LibreOffice Calc using Python</title>
		<link>https://tutolibro.org/2025/11/03/write-hello-world-in-libreoffice-calc-using-python/</link>
					<comments>https://tutolibro.org/2025/11/03/write-hello-world-in-libreoffice-calc-using-python/#respond</comments>
		
		<dc:creator><![CDATA[gweno]]></dc:creator>
		<pubDate>Mon, 03 Nov 2025 23:00:40 +0000</pubDate>
				<category><![CDATA[Calc]]></category>
		<category><![CDATA[LibreOffice]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[hello world]]></category>
		<category><![CDATA[libreoffice]]></category>
		<category><![CDATA[python]]></category>
		<guid isPermaLink="false">https://tutolibro.org/?p=1533</guid>

					<description><![CDATA[First let&#8217;s get the coding done, then we&#8217;ll dive in to it. You can either copy this block of code above or better type it yourself. Typing the code yourself will take longer but it&#8217;s the best way to remember...<br /><a class="read-more-button" href="https://tutolibro.org/2025/11/03/write-hello-world-in-libreoffice-calc-using-python/">Read more</a>]]></description>
										<content:encoded><![CDATA[
<p>First let&#8217;s get the coding done, then we&#8217;ll dive in to it.</p>



<pre class="wp-block-code"><code># Created by Gweno 16/08/2019 for tutolibro.org
# This program displays 'Hello World!" in cell A1 of the 
# current Calc document.

# get the doc from the scripting context 
# which is made available to all scripts
desktop = XSCRIPTCONTEXT.getDesktop()
model = desktop.getCurrentComponent()

def HelloWorld():
    """Write 'Hello World!' in Cell A1"""

    # access the active sheet
    active_sheet = model.CurrentController.ActiveSheet

    # write 'Hello World' in A1
    active_sheet.getCellRangeByName("A1").String = "Hello World!"
</code></pre>



<p>You can either copy this block of code above or better type it yourself. Typing the code yourself will take longer but it&#8217;s the best way to remember a new language.</p>



<p>The first lines starting with &#8216;#&#8217; are comments. <br>A comment in a program is a line that is not processed when the program is executed. It helps the person reading the program to understand what the code is doing. Most of the time, this person is yourself. Comments will help you remember what you were trying to achieve with this code.</p>



<p>The blank space at the start of indented lines should be made of 4 spaces:<br><code>&nbsp;&nbsp;&nbsp;&nbsp;active_sheet = model.CurrentController.ActiveSheet</code></p>



<p>If you don&#8217;t do that it will create an error because the Python interpreter will not recognise the line as being part of the function define by:<br><code>def HelloWorld():</code></p>



<p>The instruction &#8216;def&#8217; is to &#8216;define a function&#8217;. A function is a set of written instructions. It can be run on its own. It can also be &#8216;called&#8217; by other functions to be executed.<br>Our function here is called &#8216;HelloWorld&#8217;. The part between parenthesis &#8216;()&#8217; is where you write the arguments to be used by the function. In our case we are not &#8216;passing&#8217; any argument to the function &#8216;HelloWorld&#8217; for now. That&#8217;s why we have nothing within the parenthesis &#8216;()&#8217;.<br>If you want to assign this macro to a button, you need to pass a specific argument. This argument is necessary for the macro to work when pressing the button. (see <a href="https://tutolibro.org/2025/10/27/libreoffice-calc-python-programming-part-2-hello-world/" data-type="post" data-id="578">this post</a> explains how to assign a button to a python macro)</p>



<figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="590" height="384" src="https://tutolibro.org/wp-content/uploads/2025/11/HelloWorldCalc.png" alt="" class="wp-image-1540" srcset="https://tutolibro.org/wp-content/uploads/2025/11/HelloWorldCalc.png 590w, https://tutolibro.org/wp-content/uploads/2025/11/HelloWorldCalc-300x195.png 300w" sizes="auto, (max-width: 590px) 100vw, 590px" /></figure>



<p><strong><span style="text-decoration: underline;">Explanations:</span></strong><br><strong>Lines 1,2,3,5,6: </strong>these lines start with a &#8216;#&#8217; so they are comments.<br><strong>Lines 4,9,18:</strong> these empty lines are only for making the program easier to read. They serve to separate the different sets of instructions within the program. The Python interpreter does not need them but these empty lines make the code more readable for us.</p>



<p><strong>Line 7:</strong><br><code>desktop = <a href="https://tutolibro.tech/2022/11/14/what-is-xscriptcontext/" data-type="post" data-id="1149">XSCRIPTCONTEXT</a>.<a type="post" href="https://tutolibro.org/2023/02/08/what-is-getdesktop/" id="1157">getDesktop</a>()</code><br>This line is what allows us to use Python language to manipulate LibreOffice&#8217;s components. It is a bit complicated to explain exactly what this line does. In short, we define &#8216;desktop&#8217; to hold the &#8216;<code><a href="https://tutolibro.tech/2022/11/14/what-is-xscriptcontext/" data-type="post" data-id="1149">XSCRIPTCONTEXT</a></code>&#8216; interface provided by LibreOffice for our scripts (programs). The &#8216;<a type="post" href="https://tutolibro.org/2023/02/08/what-is-getdesktop/" id="1157">getDesktop</a>()&#8217; function is used to obtain a reference for our scripts to operate on. (It is not a reference to a computer desktop!). (Click on the links above to read more in my &#8216;bitesize&#8217; section).</p>



<p><strong>Line 8:</strong><br><code>model = desktop.getCurrentComponent()</code><br>Here, we define &#8216;model&#8217; to be the current component of &#8216;desktop&#8217;. Thus, we are referencing the current document open. It should be a Calc document<strong>.</strong></p>



<p><strong>Line 10:</strong><br><code>def HelloWorld():</code> <br>Define a new function called &#8216;HelloWorld&#8217; that has no arguments.<br>The &#8216;:&#8217; at the end is essential, it&#8217;s to say that what is following is the actual code written for this function. And every line that follow this function&#8217;s 1st line should start with 4 spaces.<br>The function finishes when there is no lines after it. Or when the next non empty line is not a comment and starts with no space.<br><strong>Line 11:</strong><br>      &#8220;&#8221;&#8221;Write &#8216;Hello World!&#8217; in Cell A1&#8243;&#8221;&#8221;<br>This line is simply a description of the function. Also called a Docstring (DocStrings is short for Documentation Strings), it starts and ends with 3 quotation marks. It is not really useful for us right now. Still, it is good practice to have a short description for each of your functions. It&#8217;s different from a comment because this description can be displayed outside of the program as a &#8216;help&#8217; note.<br>To display docstrings, you can use the built-in function &#8216;help&#8217;, like illustrated in the APSO console below:</p>



<p><img loading="lazy" decoding="async" width="450" height="202" class="wp-image-1541" style="width: 450px;" src="https://tutolibro.org/wp-content/uploads/2025/11/help_helloworld.png" alt="Help function to display Docstring of HelloWorld.py" srcset="https://tutolibro.org/wp-content/uploads/2025/11/help_helloworld.png 354w, https://tutolibro.org/wp-content/uploads/2025/11/help_helloworld-300x135.png 300w" sizes="auto, (max-width: 450px) 100vw, 450px" /><br><strong><br>Line 14:</strong><br><code>&nbsp;&nbsp;&nbsp;&nbsp;active_sheet = model.CurrentController.ActiveSheet</code><br>Here we are defining &#8216;active_sheet&#8217; to be the current spreadsheet you are looking at.<br><strong>Line 17:</strong><br><code>&nbsp;&nbsp;&nbsp;&nbsp;active_sheet.getCellRangeByName("A1").String = "Hello World!"</code><br>Finally! Now we have a reference to the current LibreOffice Context. We have the current LibreOffice Calc Document (component) and the current spreadsheet. Therefore, we can access the cell &#8216;A1&#8217;. We can then assign to its &#8216;String&#8217; parameter the text &#8220;Hello World!</p>



<p><strong><span style="text-decoration: underline;">Exercise:</span></strong><br>Add to function HelloWorld a line of code to display &#8220;Bye Bye World!&#8221; in cell &#8216;B2&#8217;. I am sure you can handle that!</p>



<p>If you like this tutorial, you could <a href="https://buymeacoffee.com/tutolibro" data-type="link" data-id="https://buymeacoffee/tutolibro" target="_blank" rel="noreferrer noopener">buy me a coffee</a> to help me continue writing add-free tutorials.</p>



<p>Thank you!</p>



<p></p>
]]></content:encoded>
					
					<wfw:commentRss>https://tutolibro.org/2025/11/03/write-hello-world-in-libreoffice-calc-using-python/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">1533</post-id>	</item>
		<item>
		<title>LibreOffice Calc &#038; Python Programming: Part 2 – Hello World</title>
		<link>https://tutolibro.org/2025/10/27/libreoffice-calc-python-programming-part-2-hello-world/</link>
					<comments>https://tutolibro.org/2025/10/27/libreoffice-calc-python-programming-part-2-hello-world/#comments</comments>
		
		<dc:creator><![CDATA[gweno]]></dc:creator>
		<pubDate>Mon, 27 Oct 2025 00:32:47 +0000</pubDate>
				<category><![CDATA[Calc]]></category>
		<category><![CDATA[LibreOffice]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[calc]]></category>
		<category><![CDATA[hello world]]></category>
		<category><![CDATA[libreoffice]]></category>
		<category><![CDATA[python]]></category>
		<guid isPermaLink="false">http://superbusydaddy.com/?p=578</guid>

					<description><![CDATA[In this episode of my LibreOffice Calc &#38; Python Programming tutorial, we are going to write our first Python Macro. It will write &#8216;Hello World&#8217; in cell A1 of a LibreOffice Calc document. How exciting is that!?! Here is the...<br /><a class="read-more-button" href="https://tutolibro.org/2025/10/27/libreoffice-calc-python-programming-part-2-hello-world/">Read more</a>]]></description>
										<content:encoded><![CDATA[
<p id="top">In this episode of my LibreOffice Calc &amp; Python Programming tutorial, we are going to write our first Python Macro. It will write &#8216;Hello World&#8217; in cell A1 of a LibreOffice Calc document. How exciting is that!?!</p>



<p>Here is the plan for this part 2 of the tutorial:</p>



<ul class="wp-block-list">
<li><a href="#workflow">Get your workflow</a> right: deciding between your macro embedded in the file or saved in a macro repository.</li>



<li><a href="#code">First line of code</a> and writing &#8220;Hello World!&#8221; in Cell A1.</li>



<li id="workflow">Assigning the macro to a LibreOffice <a href="#button">Control Button</a>.</li>
</ul>



<h4 class="wp-block-heading">Get your workflow right                                                                                                                 (<a href="#top">Back to top</a>)</h4>



<p>With the APSO add-on (<a href="https://tutolibro.org/2019/07/26/libreoffice-calc-python-programming-part-1-requirements/">see previous article</a>) you can embed your macro within the LibreOffice File. This can be useful when you want to send your file with the Python Macro. One downside if you do that is that the Python macro is not accessible outside of the LibreOffice file. So you&#8217;ll need to open the LibreOffice file and open the Python Macro organiser in order to edit the Python program. There are alternatives to embedding the Python program. You can place it in an organized directory while still using APSO. This allows you to re-use it for any other LibreOffice document. You can also edit the program without opening the LibreOffice document if you open it from the relevant folder. If you want to send your LibreOffice Document to someone, there is a trade-off. You will also need to send the Python macro.</p>



<p>I am in favor of keeping my programs in a tidy manner. This approach allows me to re-use them across many different files. However, the choice is entirely up to you.</p>



<p>To illustrate these 2 cases, let&#8217;s have a look at an example:</p>



<p>I have a file called &#8220;<strong>HelloWorld.ods</strong>&#8221; and two Python files: the first one is called &#8220;<strong>HelloWorld.py</strong>&#8221; and the other file is called &#8220;<strong>Testing.py&#8221;</strong>. The first python file is embedded in the LibreOffice file, the second is in the shared Python Scripts directory.</p>



<p class="has-text-align-left">In the first case scenario, after opening HelloWorld.ods, I can go to the Python Script Organizer, in Tools&gt;Macro&gt;Organize Python Scripts (shortcut Alt+Shift+F11) from the APSO extension. Alternatively I can use the LibreOffice Standard Macro Organiser in Tools&gt;Macro&gt;Organise Macros&gt;Python&#8230;.</p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="543" height="672" src="https://tutolibro.org/wp-content/uploads/2019/08/pyCalc01.png" alt="" class="wp-image-848" srcset="https://tutolibro.org/wp-content/uploads/2019/08/pyCalc01.png 543w, https://tutolibro.org/wp-content/uploads/2019/08/pyCalc01-242x300.png 242w" sizes="auto, (max-width: 543px) 100vw, 543px" /></figure>



<p class="has-text-align-left"><br>In the Python Scripts Organiser you will see that your Python Macro is listed within the LibreOffice File&#8217;s name.</p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="302" height="311" src="https://tutolibro.org/wp-content/uploads/2019/08/pyCalc02.png" alt="" class="wp-image-849" srcset="https://tutolibro.org/wp-content/uploads/2019/08/pyCalc02.png 302w, https://tutolibro.org/wp-content/uploads/2019/08/pyCalc02-291x300.png 291w" sizes="auto, (max-width: 302px) 100vw, 302px" /><figcaption class="wp-element-caption">Python Macro HelloWorld is within the calc document.</figcaption></figure>



<p><br>When you choose to edit the Python Macro, select &#8216;Menu&gt;Edit&#8217; from the &#8216;Python scripts&#8217; window. The edited file&#8217;s path is temporary. It is only used for the time of editing this Python program.</p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="646" height="182" src="https://tutolibro.org/wp-content/uploads/2019/08/pyCalc03.png" alt="" class="wp-image-850" srcset="https://tutolibro.org/wp-content/uploads/2019/08/pyCalc03.png 646w, https://tutolibro.org/wp-content/uploads/2019/08/pyCalc03-300x85.png 300w" sizes="auto, (max-width: 646px) 100vw, 646px" /></figure>



<p>So where is the Python Script saved on your hard drive?<br>As I wrote earlier, it&#8217;s embedded in the LibreOffice File. If you don&#8217;t believe me, here is a trick to find out:<br>If I rename my &#8216;HelloWorld.ods&#8217; in &#8216;HelloWorld.zip&#8217; I can then unzip it to see the archived compressed files and folders that constitute the LibreOffice file itself (with  e.g. Archive Manager on Linux, winzip on Windows, or Archive Utility on Mac). In the list of folders I can then spot a folder called &#8216;Scripts&#8217;, then a sub-folder called &#8216;python&#8217;, and OH SURPRISE! the python script is there!</p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="424" height="355" src="https://tutolibro.org/wp-content/uploads/2019/08/pyCalc04.png" alt="" class="wp-image-851" srcset="https://tutolibro.org/wp-content/uploads/2019/08/pyCalc04.png 424w, https://tutolibro.org/wp-content/uploads/2019/08/pyCalc04-300x251.png 300w" sizes="auto, (max-width: 424px) 100vw, 424px" /></figure>



<p> If you want to do that too, when you&#8217;re done don&#8217;t forget to rename the file back with its &#8216;.ods&#8217; extension.</p>



<p>In the second case scenario,we are saving our Python script our Python script &#8216;Testing.py&#8217; is not listed in the Macro manager within the LibreOffice File, but this time I created it as part of my Library &#8216;MyPythonLibrary&#8217; within another Library called &#8216;Tutorial&#8217;.</p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="362" height="373" src="https://tutolibro.org/wp-content/uploads/2019/08/pyCalc00.png" alt="" class="wp-image-847" srcset="https://tutolibro.org/wp-content/uploads/2019/08/pyCalc00.png 362w, https://tutolibro.org/wp-content/uploads/2019/08/pyCalc00-291x300.png 291w" sizes="auto, (max-width: 362px) 100vw, 362px" /><figcaption class="wp-element-caption">&#8216;main&#8217; is the Python function that is in the File &#8216;Testing.py&#8217;.</figcaption></figure>



<p>And this time if I edit &#8216;Testing&#8217;, I can see the path is not a temporary one and I can access it even when LibreOffice is closed.</p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="685" height="282" src="https://tutolibro.org/wp-content/uploads/2019/08/pyCalc05.png" alt="" class="wp-image-852" srcset="https://tutolibro.org/wp-content/uploads/2019/08/pyCalc05.png 685w, https://tutolibro.org/wp-content/uploads/2019/08/pyCalc05-300x124.png 300w" sizes="auto, (max-width: 685px) 100vw, 685px" /></figure>



<p id="code">In fact your Python programs can be directly accessed on your computer at the following location:<br><span style="text-decoration: underline;">For Linux: </span><br>/home/&lt;user name&gt;/.config/libreoffice/4/user/Scripts/python<br>(to see folder &#8216;.config&#8217; you need to &#8216;Show hidden files&#8217; , the shortcut CTRL+h).<br><span style="text-decoration: underline;">For Windows:</span><br>C:\Users\&lt;user_name&gt;\AppData\Roaming\LibreOffice\4\Scripts\python<br><span style="text-decoration: underline;">For Mac:</span><br>(I think it&#8217;s)/Users/&lt;user name&gt;/Library/Application Support/LibreOffice/4/user\Scripts\python<br>(Please let me know in a comment if you know for sure what the folder is for Mac OS).</p>



<h4 class="wp-block-heading">First line of code!                                                                                                                 (<a href="#top">Back to top</a>)</h4>



<p>I know you can&#8217;t wait anymore! So let&#8217;s write our first line of code in Python, within a LibreOffice Calc Document.</p>



<p>So after creating a new Calc document, go in the Macro manager (if you have installed the APSO extension: ALT+Shift+F11), then as explained earlier in this lesson you can have your program either in calc document or in a library outside the document. To do so, choose between &#8216;My Macros&#8217;, &#8216;LibreOffice Macros&#8217; or the name of the document itself.<br>I do prefer saving my python macros in the &#8216;My Macros&#8217; Library.<br>You can also create sub-folders, called  libraries, or directly create your program by choosing &#8216;Create Module&#8217; in the &#8216;Menu&#8217;.</p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="339" height="335" src="https://tutolibro.org/wp-content/uploads/2019/08/pyCalc06.png" alt="" class="wp-image-853" srcset="https://tutolibro.org/wp-content/uploads/2019/08/pyCalc06.png 339w, https://tutolibro.org/wp-content/uploads/2019/08/pyCalc06-300x296.png 300w" sizes="auto, (max-width: 339px) 100vw, 339px" /></figure>



<p>So go ahead and create a Module, name it &#8220;HelloWorld&#8221;, then once the module is listed and selected, click again on &#8216;Menu&#8217; and choose &#8216;edit&#8217;.</p>



<p>And then you get either a default editor or the one you have linked with APSO to pop up with a new program that content nothing but a couple of lines like these:</p>



<figure class="wp-block-image size-large is-resized"><img loading="lazy" decoding="async" width="427" height="76" src="https://tutolibro.org/wp-content/uploads/2019/08/pyCalc07.png" alt="" class="wp-image-854" srcset="https://tutolibro.org/wp-content/uploads/2019/08/pyCalc07.png 427w, https://tutolibro.org/wp-content/uploads/2019/08/pyCalc07-300x53.png 300w" sizes="auto, (max-width: 427px) 100vw, 427px" /></figure>



<p>The first line is a comment.<br>A comment in a program is a line that is not processed when the program is executed. So it&#8217;s not a line of code, it is usually to help the person who is going to read your program (most of the time, yourself) to understand what the following code is aiming to do.<br>So to write comments in your Python programs simply add a &#8216;#&#8217; at the start of the line.<br>Don&#8217;t worry about the second line, in fact you can delete these two lines for now.<br>Instead you could like me write your name, the date and what the program is for:</p>



<p><code># Created by Gweno 16/08/2019 for tutolibro.tech<br># This program displays 'Hello World!" in cell A1 of the</code><br><code># current Calc document.</code></p>



<p>Then Finally here is the code for writing &#8220;Hello World!&#8221; in cell A1:</p>



<hr class="wp-block-separator has-css-opacity"/>



<pre class="wp-block-code"><code># Created by Gweno 16/08/2019 for tutolibro.tech
# This program displays 'Hello World!" in cell A1 of the 
# current Calc document.

def HelloWorld():
    """Write 'Hello World!' in Cell A1"""

# get the doc from the scripting context
# which is made available to all scripts
    desktop = XSCRIPTCONTEXT.getDesktop()
    model = desktop.getCurrentComponent()

# access the active sheet
    active_sheet = model.CurrentController.ActiveSheet

# write 'Hello World' in A1
    active_sheet.getCellRangeByName("A1").String = "Hello World!"
</code></pre>



<hr class="wp-block-separator has-css-opacity"/>



<p>You can either copy this block of code above or better type it manually yourself. Typing the code yourself will take longer but it&#8217;s the best way to remember a new language.<br>When you type it make sure that you leave 4 empty spaces to indent the lines that are not starting at the beginning of their lines, like:<br><code>    desktop = XSCRIPTCONTEXT.getDesktop()</code><br>If you don&#8217;t do that it will create an error because the Python interpreter will not recognise the line as part of the function define by:<br><code>def HelloWorld():</code><br>So the instruction &#8216;def&#8217; is to &#8216;define a function&#8217;. A function is a set of written instructions that can be run on its own or &#8216;called&#8217; by other functions to be executed.<br>Our function here is called &#8216;HelloWorld&#8217; and the part between parenthesis &#8216;()&#8217; is where you write the arguments to be used by the function. In our case we are not &#8216;passing&#8217; any argument to the function &#8216;HelloWorld&#8217; for now, that is why we have nothing within the parenthesis &#8216;()&#8217;.<br>You need to know that when we will assign our macro to a button we will need to pass a specific argument for our macro to work when we press the button.</p>



<figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="605" height="332" src="https://tutolibro.org/wp-content/uploads/2023/11/lopy-helloworld.png" alt="" class="wp-image-1202" srcset="https://tutolibro.org/wp-content/uploads/2023/11/lopy-helloworld.png 605w, https://tutolibro.org/wp-content/uploads/2023/11/lopy-helloworld-300x165.png 300w" sizes="auto, (max-width: 605px) 100vw, 605px" /></figure>



<p><strong><span style="text-decoration: underline;">Explanations:</span></strong><br><strong>Lines 1,2,3,8,9,13,16: </strong>these lines start with a &#8216;#&#8217; so they are comments.<br><strong>Lines 4,7,12,15,18:</strong> these empty lines are only for making the program easier to read,by separating the different set of instructions within the program. The Python interpreter does not need them but these empty lines make the code more readable for us.<br><strong>Line 5:</strong><br><code>def HelloWorld():</code> <br>Define a new function called &#8216;HelloWorld&#8217; that has no arguments.<br>The &#8216;:&#8217; at the end is essential, it&#8217;s to say that what is following is the actual code written for this function. And every line that follow this function&#8217;s 1st line should start with 4 spaces.<br>The function finishes when either there is nothing after, or when a new line starts from the beginning with no space to start with (excluding comment lines).<br><strong>Line 6:</strong><br><code>   """Write 'Hello World!' in Cell A1"""</code><br>This line is simply a description of the function, it starts and ends with 3 quotation marks. It is not really useful for us right now but it is a good practice to have a short description for each of your function. It&#8217;s different from a comment because this description can be displayed from outside of the program as a &#8216;help&#8217; note.<br><strong>Line 10:</strong><br><code>    desktop = <a href="https://tutolibro.tech/2022/11/14/what-is-xscriptcontext/" data-type="post" data-id="1149">XSCRIPTCONTEXT</a>.<a href="https://tutolibro.org/2023/02/08/what-is-getdesktop/" type="post" id="1157">getDesktop</a>()</code><br>This line is what allows us to use Python language to manipulate LibreOffice&#8217;s components. It is a bit complicated to explained exactly what this line does but in short:<br>We define &#8216;desktop&#8217; to hold the &#8216;<code><a href="https://tutolibro.tech/2022/11/14/what-is-xscriptcontext/" data-type="post" data-id="1149">XSCRIPTCONTEXT</a></code>&#8216; interface provided by LibreOffice for our scripts (programs). The &#8216;<a href="https://tutolibro.org/2023/02/08/what-is-getdesktop/" type="post" id="1157">getDesktop</a>()&#8217; function is used to obtain a reference for our scripts to operate on. (It is not a reference to a computer desktop!). (Click on the links above to read more in my &#8216;bitesize&#8217; section)<br><strong>Line 11:</strong><br><code>    model = desktop.getCurrentComponent()</code><br>In the same way we define here &#8216;model&#8217; to be the current component of &#8216;desktop&#8217;, so effectively we are referencing to the current document open, which should be a Calc document<strong>.<br>Line 14:</strong><br><code>    active_sheet = model.CurrentController.ActiveSheet</code><br>Here we are defining &#8216;active_sheet&#8217;  to be the current spreadsheet you are looking at.<br><strong>Line 17:</strong><br><code>    active_sheet.getCellRangeByName("A1").String = "Hello World!"</code><br>Finally! Now that we have a reference to the current LibreOffice Context, the current LibreOffice Calc Document (component) and the current spreadsheet, we can access the cell &#8216;A1&#8217; from that current spreadsheet and assign to its &#8216;String&#8217; parameter the text &#8220;Hello World!&#8221;.</p>



<p id="button"><strong><span style="text-decoration: underline;">Exercise:</span></strong><br>Add to function HelloWorld a line of code to display &#8220;Bye Bye World!&#8221; in cell &#8216;B2&#8217;. I am sure you can handle that!</p>



<h4 class="wp-block-heading">Assign the macro to a LibreOffice Control Button.                                                                    (<a href="#top">Back to top</a>)</h4>



<p>You will quickly realise when you write a macro within libreOffice that it&#8217;s not really convenient to execute them through the Macro Manager because you need to access that menu and it takes a few clicks to do so (Quickest way to access the APSO Macro manager being [Alt] + [Shift] + [F11]). It is easy to create a button on your spreadsheet and to assign it to your macro and then when you&#8217;ll click on that button it will execute the macro.<br>So first we&#8217;ll <strong>create a button</strong> on the spreadsheet then we&#8217;ll <strong>assign the python macro</strong> to this button.</p>



<p><strong><span style="text-decoration: underline;">Create a button</span></strong>:<br>Simply by clicking on Menu &#8216;<strong>Insert&gt;Form Controls&gt;Push Button&#8217;</strong>:</p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="430" height="711" src="https://tutolibro.org/wp-content/uploads/2019/08/pyCalc10.png" alt="" class="wp-image-857" srcset="https://tutolibro.org/wp-content/uploads/2019/08/pyCalc10.png 430w, https://tutolibro.org/wp-content/uploads/2019/08/pyCalc10-181x300.png 181w" sizes="auto, (max-width: 430px) 100vw, 430px" /></figure>



<p>Then once you have the little cross to insert the button, just click for upper left corner of the button, then hold and release where you want the bottom right corner of the button.</p>



<figure class="wp-block-image size-large is-resized"><img loading="lazy" decoding="async" width="124" height="84" src="https://tutolibro.org/wp-content/uploads/2019/08/pyCalc11.png" alt="" class="wp-image-858"/></figure>



<p><strong><span style="text-decoration: underline;">Assign the python macro</span></strong><span style="text-decoration: underline;">:</span><br>Once you have the button displayed, right click on it and select &#8216;Control&#8230;&#8217;</p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="241" height="373" src="https://tutolibro.org/wp-content/uploads/2019/08/pyCalc12.png" alt="" class="wp-image-859" srcset="https://tutolibro.org/wp-content/uploads/2019/08/pyCalc12.png 241w, https://tutolibro.org/wp-content/uploads/2019/08/pyCalc12-194x300.png 194w" sizes="auto, (max-width: 241px) 100vw, 241px" /><figcaption class="wp-element-caption">Select &#8216;Control&#8217; from the new Button contextual menu</figcaption></figure>



<p>Then in the window &#8216;Properties&#8217; select the &#8216;Events&#8217; tab, scroll down a bit to find in the list of events the one called &#8216;Mouse button pressed&#8217;. On the right of this &#8216;Mouse button pressed&#8217; entry click on the button &#8216;[&#8230;]&#8217;.</p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="564" height="430" src="https://tutolibro.org/wp-content/uploads/2019/08/pyCalc13.png" alt="" class="wp-image-860" srcset="https://tutolibro.org/wp-content/uploads/2019/08/pyCalc13.png 564w, https://tutolibro.org/wp-content/uploads/2019/08/pyCalc13-300x229.png 300w" sizes="auto, (max-width: 564px) 100vw, 564px" /></figure>



<p>A window named  &#8220;Assign Action&#8217; pops up, click on the button &#8216;[Macro]&#8217; on the right hand side of this window.</p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="592" height="373" src="https://tutolibro.org/wp-content/uploads/2019/08/pyCalc14.png" alt="" class="wp-image-861" srcset="https://tutolibro.org/wp-content/uploads/2019/08/pyCalc14.png 592w, https://tutolibro.org/wp-content/uploads/2019/08/pyCalc14-300x189.png 300w" sizes="auto, (max-width: 592px) 100vw, 592px" /><figcaption class="wp-element-caption">Assign a macro to a button</figcaption></figure>



<p>You should then get the &#8216;Macro Selector&#8217; with your Macro Libraries. Simply go to the library where your macro is saved and select the macro &#8216;HelloWorld&#8217;,then press &#8216;OK&#8217;.</p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="722" height="202" src="https://tutolibro.org/wp-content/uploads/2019/08/pyCalc15-e1565986476973.png" alt="" class="wp-image-862" srcset="https://tutolibro.org/wp-content/uploads/2019/08/pyCalc15-e1565986476973.png 722w, https://tutolibro.org/wp-content/uploads/2019/08/pyCalc15-e1565986476973-300x84.png 300w" sizes="auto, (max-width: 722px) 100vw, 722px" /><figcaption class="wp-element-caption">Select the macro from Macro Selector</figcaption></figure>



<p>Then &#8216;OK&#8217; again in &#8216;Assign Macro&#8217; window, than close the &#8216;Properties&#8217; window.<br>Last thing to do is to come out the &#8216;Design&#8217; mode to be able to click on the button, do that by going to &#8216;Tools&gt;Forms&gt;Design Mode&#8217;.</p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="461" height="541" src="https://tutolibro.org/wp-content/uploads/2019/08/pyCalc17.png" alt="" class="wp-image-864" srcset="https://tutolibro.org/wp-content/uploads/2019/08/pyCalc17.png 461w, https://tutolibro.org/wp-content/uploads/2019/08/pyCalc17-256x300.png 256w" sizes="auto, (max-width: 461px) 100vw, 461px" /><figcaption class="wp-element-caption">Set design mode off</figcaption></figure>



<p>And now when you click on the button&#8230; tada&#8230;<br>ERROR!</p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="546" height="255" src="https://tutolibro.org/wp-content/uploads/2019/08/pyCalc16.png" alt="" class="wp-image-863" srcset="https://tutolibro.org/wp-content/uploads/2019/08/pyCalc16.png 546w, https://tutolibro.org/wp-content/uploads/2019/08/pyCalc16-300x140.png 300w" sizes="auto, (max-width: 546px) 100vw, 546px" /><figcaption class="wp-element-caption">Error: missing argument</figcaption></figure>



<p>This is because to be able to work, the function HelloWorld need all information from LibreOffice Context in the form of an argument. So we need to pass the argument &#8216;*args&#8217; in our function HelloWorld. So in your code in line 5 instead of:<br><code>def HelloWorld():</code><br>You should have:<br><code>def HelloWorld(*args):</code><br>Save your program again and then when you press on the button the cell  A1 will display &#8216;Hello World!&#8217; !!!</p>



<p>So there is the final version of the macro:</p>



<hr class="wp-block-separator has-css-opacity"/>



<pre class="wp-block-code"><code># Created by Gweno 16/08/2019 for tutolibro.tech
# This program displays 'Hello World!" in cell A1 of the 
# current Calc document.

def HelloWorld(*args):
    """Write 'Hello World!' in Cell A1"""
    
# get the doc from the scripting context 
# which is made available to all scripts
    desktop = XSCRIPTCONTEXT.getDesktop()
    model = desktop.getCurrentComponent()
    
# access the active sheet
    active_sheet = model.CurrentController.ActiveSheet

# write 'Hello World' in A1
    active_sheet.getCellRangeByName("A1").String = "Hello World!"</code></pre>



<p>You can clone or download this code from gitHub <a href="https://github.com/Gweno/tutolibro.tech/tree/master/lopy/part2">here</a>.</p>



<hr class="wp-block-separator has-css-opacity"/>



<h3 class="wp-block-heading">Good luck! and leave a comment if you are having problem to have this macro to work.</h3>



<hr class="wp-block-separator has-alpha-channel-opacity is-style-default"/>



<p>If you like this tutorial, you could <a href="https://buymeacoffee.com/tutolibro" data-type="link" data-id="https://buymeacoffee/tutolibro" target="_blank" rel="noreferrer noopener">buy me a coffee</a> to help me continue writing add-free tutorials.</p>



<p>Thank you!</p>



<div class="wp-block-group is-content-justification-space-between is-nowrap is-layout-flex wp-container-core-group-is-layout-0dfbf163 wp-block-group-is-layout-flex">

</div>
]]></content:encoded>
					
					<wfw:commentRss>https://tutolibro.org/2025/10/27/libreoffice-calc-python-programming-part-2-hello-world/feed/</wfw:commentRss>
			<slash:comments>16</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">578</post-id>	</item>
	</channel>
</rss>
