[Solved] Create Python script

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
TAB
Posts: 283
Joined: Sun Feb 24, 2008 5:04 am

[Solved] Create Python script

Post by TAB »

Hello!
In Calc, click Tools>Macros>Organize>Python; click MyMacros. Why is the Create button shaded --inactive? How do I create a new script?

TY for the help --particularly from Karolus. I may have found the answer (but it's not easy to digest!):
http://www.openoffice.org/udk/python/python-bridge.html
Last edited by RoryOF on Wed Oct 28, 2015 7:57 am, edited 2 times in total.
Reason: Added [Solved].[RoryOF, Moderator]
FJCC
Moderator
Posts: 9651
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: create Python script

Post by FJCC »

You create the script with a plain text editor and save it in your user profile. You don't say what operating system you have. On my Windows 7 system my python scripts are in the folder
C:\Users\fjcc\AppData\Roaming\OpenOffice\4\user\Scripts\python
where AppData is a hidden folder.
OpenOffice 4.1 on Windows 10 and Linux Mint
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
TAB
Posts: 283
Joined: Sun Feb 24, 2008 5:04 am

Re: create Python script

Post by TAB »

Hello!
I created the script (in Win10)
C:\Users\TorrA_000\AppData\Roaming\OpenOffice\4\user\Scripts\Python\Hello.py
as you explained. Calc: Tools>Macros>Organize>Python gives the screen attached; MyMacros has the Hello item, but I can do nothing with it.
Attachments
screenshot sowing the Python scripts seen by Calc
screenshot sowing the Python scripts seen by Calc
PythMacs.png (8.95 KiB) Viewed 16921 times
FJCC
Moderator
Posts: 9651
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: create Python script

Post by FJCC »

Typically, that means you have a syntax error in your script. Please post your script so we can try to diagnose the problem.
OpenOffice 4.1 on Windows 10 and Linux Mint
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
TAB
Posts: 283
Joined: Sun Feb 24, 2008 5:04 am

Re: create Python script

Post by TAB »

I doubt it, because it works.
'The extension py is not allowed'; so, here it is:

Code: Select all

print ('Hello')
FJCC
Moderator
Posts: 9651
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: create Python script

Post by FJCC »

I'm no expert on Python in OpenOffice. My understanding is that you have to define at least one function within the script and it is the functions that are listed. Also, print() within the functions does not work the way you would expect. I saw that explained once but I don't remember why. If you are using calc, try this for your script.

Code: Select all

import uno
def SayHello():
  oDoc = XSCRIPTCONTEXT.getDocument()
  oSheet = oDoc.Sheets.getByName("Sheet1")
  oCell = oSheet.getCellRangeByName("E1")
  oCell.String = 'Hello'
OpenOffice 4.1 on Windows 10 and Linux Mint
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
User avatar
Villeroy
Volunteer
Posts: 31365
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: create Python script

Post by Villeroy »

Start the office suite from a terminal and watch it

Code: Select all

def hello_world(*args):
    print 'hello world'
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
User avatar
karolus
Volunteer
Posts: 1252
Joined: Sat Jul 02, 2011 9:47 am

Re: create Python script

Post by karolus »

@FJCC: If you use nothing from Module `uno` you don't need to import it.
Libreoffice 25.2… on Debian 13 (trixie) (on RaspberryPI5)
Libreoffice 25.8… flatpak on Debian 13 (trixie) (on RaspberryPI5)
TAB
Posts: 283
Joined: Sun Feb 24, 2008 5:04 am

Re: create Python script

Post by TAB »

Villeroy wrote:Start the office suite from a terminal and watch it
Your code had syntactical errors; I changed it to

Code: Select all

def hello_world():
	print ('hello world')
and it worked! --screenshot.
But, when I clicked Run, I got a fatal error:
pyUno_createUnoStructHelper: too few elements in the initializer tuple, expected at least 2, got 1.
OK. How do I invoke a Python function (like hello_world()) from Calc or Writer? In other words, how to link OO with a Python script?
Attachments
macro correctly displayed
macro correctly displayed
PythMacs.png (6.27 KiB) Viewed 16829 times
User avatar
Villeroy
Volunteer
Posts: 31365
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: create Python script

Post by Villeroy »

TAB wrote:
Villeroy wrote:Start the office suite from a terminal and watch it
Your code had syntactical errors; I changed it to

Code: Select all

def hello_world():
	print ('hello world')
and it worked! --screenshot.
def hello_world(*args):
print "hello world"
works for me (of course) with OpenOffice and Python 2.7. Until now, I refused to learn a completely new Python language and therefore I do not use any Python coding with LibreOffice.
YOU are the Python programmer and it is YOUR f***g obligation to learn your programming language and DEBUG YOUR f****g code! Instead you do not even tell us the about software you try using.
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
User avatar
karolus
Volunteer
Posts: 1252
Joined: Sat Jul 02, 2011 9:47 am

Re: Create Python script

Post by karolus »

Hallo

@villeroy: the differences between the 2 (python) releases are not that hard to master... -but I agree with you - the OP should know about wich python is used by AOO versus Libre-office.
Libreoffice 25.2… on Debian 13 (trixie) (on RaspberryPI5)
Libreoffice 25.8… flatpak on Debian 13 (trixie) (on RaspberryPI5)
User avatar
Villeroy
Volunteer
Posts: 31365
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Create Python script

Post by Villeroy »

karolus wrote:Hallo

@villeroy: the differences between the 2 (python) releases are not that hard to master... -but I agree with you - the OP should know about wich python is used by AOO versus Libre-office.
He is not even able to read error messages so it does not matter which language he did not learn.
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
User avatar
Villeroy
Volunteer
Posts: 31365
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Create Python script

Post by Villeroy »

print "hello world" fails because the Python developers have gone pedantic. print is not accepted as a statement anymore. It has to be a function call.
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
User avatar
karolus
Volunteer
Posts: 1252
Joined: Sat Jul 02, 2011 9:47 am

Re: Create Python script

Post by karolus »

Villeroy wrote:print "hello world" fails because the Python developers have gone pedantic. print is not accepted as a statement anymore. It has to be a function call.

I would not call it "pedantic" I would call it "well-considert decision" - This kind of Development is one of the reasons for the success of python in the last 15(20) years ( compared for Example to Basic with close to None Development ;-) )

Here's the Doc-string for the new Implementation of `print` as Function:

Code: Select all

print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)

Prints the values to a stream, or to sys.stdout by default.
Optional keyword arguments:
file:  a file-like object (stream); defaults to the current sys.stdout.
sep:   string inserted between values, default a space.
end:   string appended after the last value, default a newline.
flush: whether to forcibly flush the stream. 
Libreoffice 25.2… on Debian 13 (trixie) (on RaspberryPI5)
Libreoffice 25.8… flatpak on Debian 13 (trixie) (on RaspberryPI5)
TAB
Posts: 283
Joined: Sun Feb 24, 2008 5:04 am

Re: create Python script

Post by TAB »

Villeroy wrote: YOU are the Python programmer and...! Instead you do not even tell us the about software you try using.
What's the excitement? Why all the f-words? I thought the OO Forum was civil...
Yes, I am a (new) Python programmer, and I am not asking you to debug my script --I have done that, and it works.
All I need now is find how I can use it within OO.
For example, I have written this script to add two numbers:

Code: Select all

def add(a,b):
	c=a+b
	return c
It works under Python3.5.
How do I incorporate it in Calc to take a and b from cells A1 and A2,and put the result in cell A3?
I use OO 4.1.1.
FJCC
Moderator
Posts: 9651
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: Create Python script

Post by FJCC »

To access cells and get their value you can use code like the following.

Code: Select all

def add():
  oDoc = XSCRIPTCONTEXT.getDocument()
  oSheet = oDoc.Sheets.getByName("Sheet1")
  oCellA1 = oSheet.getCellRangeByName("A1")
  oCellA2 = oSheet.getCellRangeByName("A2")
  oCellA3 = oSheet.getCellRangeByName("A3")
  oCellA3.Value = oCellA2.Value + oCellA1.Value
I don't think you can call a Python function directly from a cell, but others will know better than I.
OpenOffice 4.1 on Windows 10 and Linux Mint
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
User avatar
karolus
Volunteer
Posts: 1252
Joined: Sat Jul 02, 2011 9:47 am

Re: Create Python script

Post by karolus »

I don't think you can call a Python function directly from a cell, but others will know better than I.
Of course, you can -- but you need to build some boilerplate around the function[s], to have at the End full access from Calc inclusive help-text and support in the Function-wizard.

Examples:
http://www.biochemfusion.com/doc/Calc_addin_howto.html
AddInn for Numbertext|Moneytext
Example from this Forum
Another from German Forum
TAB wrote:..
It works under Python3.5.
How do I incorporate it in Calc to take a and b from cells A1 and A2,and put the result in cell A3?
I use OO 4.1.1.
It seems you talk exactly about ApacheOpenOffice 4.1.1 with Python2.7 (wich btw. has not thrown a SyntaxError by running Villeroy's Example )
Learn about the differences of Pythonversions!
Libreoffice 25.2… on Debian 13 (trixie) (on RaspberryPI5)
Libreoffice 25.8… flatpak on Debian 13 (trixie) (on RaspberryPI5)
TAB
Posts: 283
Joined: Sun Feb 24, 2008 5:04 am

Re: Create Python script

Post by TAB »

TY. I will examine the examples.
What is the kink to ApacheOpenOffice 4.1.1 with Python2.7?
TAB
User avatar
karolus
Volunteer
Posts: 1252
Joined: Sat Jul 02, 2011 9:47 am

Re: Create Python script

Post by karolus »

TAB wrote: What is the kink to ApacheOpenOffice 4.1.1 with Python2.7?
Sorry, you lost me -- I'm not native English!
Libreoffice 25.2… on Debian 13 (trixie) (on RaspberryPI5)
Libreoffice 25.8… flatpak on Debian 13 (trixie) (on RaspberryPI5)
TAB
Posts: 283
Joined: Sun Feb 24, 2008 5:04 am

Re: Create Python script

Post by TAB »

I thought I replied to this, but can' t see it. So...
I goofed! I meant 'link', not 'kink' --Sorry! Where to find ApacheOpenOffice 4.1.1 with Python2.7?
User avatar
Villeroy
Volunteer
Posts: 31365
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Create Python script

Post by Villeroy »

scroll down.
:shock: :? :ucrazy:
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
User avatar
karolus
Volunteer
Posts: 1252
Joined: Sat Jul 02, 2011 9:47 am

Re: Create Python script

Post by karolus »

TAB wrote:I thought I replied to this, but can' t see it. So...
I goofed! I meant 'link', not 'kink' --Sorry! Where to find ApacheOpenOffice 4.1.1 with Python2.7?
Earlier you statet:
I use OO 4.1.1.
It seems youre actually use exactly this Office-suite
Libreoffice 25.2… on Debian 13 (trixie) (on RaspberryPI5)
Libreoffice 25.8… flatpak on Debian 13 (trixie) (on RaspberryPI5)
TAB
Posts: 283
Joined: Sun Feb 24, 2008 5:04 am

Re: Create Python script

Post by TAB »

[quote="karolus"]Examples:
Another from German Forum
I extracted the contents of TextFunctions.oxt (as *.zip), tried to display META-INF/manifest.xml
with FireFox, got an error:

XML Parsing Error: prefix not bound to a namespace
Location: file:///C:/Users/TorrA_000/Documents/software/OOff/exts/META-INF/manifest.xml
Line Number 1, Column 1:<manifest:manifest>
Why?
User avatar
Villeroy
Volunteer
Posts: 31365
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Create Python script

Post by Villeroy »

Because manifest.xml is not a formally correct xml file.
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
User avatar
karolus
Volunteer
Posts: 1252
Joined: Sat Jul 02, 2011 9:47 am

Re: Create Python script

Post by karolus »

Any .xml-file is at last only Text, you should be able to open it with your preferred IDE|Texteditor.
Libreoffice 25.2… on Debian 13 (trixie) (on RaspberryPI5)
Libreoffice 25.8… flatpak on Debian 13 (trixie) (on RaspberryPI5)
TAB
Posts: 283
Joined: Sun Feb 24, 2008 5:04 am

Re: Create Python script

Post by TAB »

Villeroy wrote:Because manifest.xml is not a formally correct xml file.
If it does not need to be 'formally correct', why bother with the .xml type? For documentation, why not use a txt file, like most 'Read-me' files?
hanya
Volunteer
Posts: 885
Joined: Fri Nov 23, 2007 9:27 am
Location: Japan

Re: Create Python script

Post by hanya »

Add namespace like following:

Code: Select all

<manifest:manifest xmlns:manifest="http://openoffice.org/2001/manifest">
Please, edit this thread's initial post and add "[Solved]" to the subject line if your problem has been solved.
Apache OpenOffice 4-dev on Xubuntu 14.04
User avatar
Villeroy
Volunteer
Posts: 31365
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Create Python script

Post by Villeroy »

TAB wrote:
Villeroy wrote:Because manifest.xml is not a formally correct xml file.
If it does not need to be 'formally correct', why bother with the .xml type? For documentation, why not use a txt file, like most 'Read-me' files?
You asked why Firefox complains about the faulty xml. Firefox is an xml viewer.
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
Post Reply