[Solved] Create Python script
[Solved] Create Python script
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
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]
Reason: Added [Solved].[RoryOF, Moderator]
Re: create Python script
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.
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.
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
Re: create Python script
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.
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
- PythMacs.png (8.95 KiB) Viewed 16926 times
Re: create Python script
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.
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
Re: create Python script
I doubt it, because it works.
'The extension py is not allowed'; so, here it is:
'The extension py is not allowed'; so, here it is:
Code: Select all
print ('Hello')Re: create Python script
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.
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
Re: create Python script
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
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
Re: create Python script
@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)
Libreoffice 25.8… flatpak on Debian 13 (trixie) (on RaspberryPI5)
Re: create Python script
Your code had syntactical errors; I changed it toVilleroy wrote:Start the office suite from a terminal and watch it
Code: Select all
def hello_world():
print ('hello world')
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
- PythMacs.png (6.27 KiB) Viewed 16834 times
Re: create Python script
def hello_world(*args):TAB wrote:Your code had syntactical errors; I changed it toVilleroy wrote:Start the office suite from a terminal and watch itand it worked! --screenshot.Code: Select all
def hello_world(): print ('hello world')
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
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
Re: Create Python script
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.
@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)
Libreoffice 25.8… flatpak on Debian 13 (trixie) (on RaspberryPI5)
Re: Create Python script
He is not even able to read error messages so it does not matter which language he did not learn.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.
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
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
Re: Create Python script
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
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
Re: Create Python script
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)
Libreoffice 25.8… flatpak on Debian 13 (trixie) (on RaspberryPI5)
Re: create Python script
What's the excitement? Why all the f-words? I thought the OO Forum was civil...Villeroy wrote: YOU are the Python programmer and...! Instead you do not even tell us the about software you try using.
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
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.
Re: Create Python script
To access cells and get their value you can use code like the following.
I don't think you can call a Python function directly from a cell, but others will know better than I.
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.ValueOpenOffice 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.
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
Re: Create Python script
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.I don't think you can call a Python function directly from a cell, but others will know better than I.
Examples:
http://www.biochemfusion.com/doc/Calc_addin_howto.html
AddInn for Numbertext|Moneytext
Example from this Forum
Another from German Forum
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 )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.
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)
Libreoffice 25.8… flatpak on Debian 13 (trixie) (on RaspberryPI5)
Re: Create Python script
TY. I will examine the examples.
What is the kink to ApacheOpenOffice 4.1.1 with Python2.7?
TAB
What is the kink to ApacheOpenOffice 4.1.1 with Python2.7?
TAB
Re: Create Python script
Sorry, you lost me -- I'm not native English!TAB wrote: What is the kink to ApacheOpenOffice 4.1.1 with Python2.7?
Libreoffice 25.2… on Debian 13 (trixie) (on RaspberryPI5)
Libreoffice 25.8… flatpak on Debian 13 (trixie) (on RaspberryPI5)
Libreoffice 25.8… flatpak on Debian 13 (trixie) (on RaspberryPI5)
Re: Create Python script
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?
I goofed! I meant 'link', not 'kink' --Sorry! Where to find ApacheOpenOffice 4.1.1 with Python2.7?
Re: Create Python script
scroll down.

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
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
Re: Create Python script
Earlier you statet: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?
It seems youre actually use exactly this Office-suiteI use OO 4.1.1.
Libreoffice 25.2… on Debian 13 (trixie) (on RaspberryPI5)
Libreoffice 25.8… flatpak on Debian 13 (trixie) (on RaspberryPI5)
Libreoffice 25.8… flatpak on Debian 13 (trixie) (on RaspberryPI5)
Re: Create Python script
[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?
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?
Re: Create Python script
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
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
Re: Create Python script
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)
Libreoffice 25.8… flatpak on Debian 13 (trixie) (on RaspberryPI5)
Re: Create Python script
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?Villeroy wrote:Because manifest.xml is not a formally correct xml file.
Re: Create Python script
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
Apache OpenOffice 4-dev on Xubuntu 14.04
Re: Create Python script
You asked why Firefox complains about the faulty xml. Firefox is an xml viewer.TAB wrote: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?Villeroy wrote: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
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice