Page 1 of 1

python embedded within document

Posted: Fri Dec 10, 2010 9:21 am
by Amber10
Hello

I would like to talk how to start python scripts from OpenOffice document.
I downloaded yesterday many different softwares ad manuals, nevertheless could not understand how
to add buttons,
to add search lines
to add choises
within OpenOffice which selection executes scripts.

Maybe smbd could give short lesson
skype: gintare.statkute

I am short in time and do not want to read all helps. I think i manage programming part, just need an advice how to integrate python script inside the OpenOffice document.


http://lucasmanual.com/mywiki/OpenOffice

Re: pyhton embedded within an OpenOffice.org's document

Posted: Sat Dec 11, 2010 7:58 pm
by FJCC
To add a button that can be tied to a macro
1. Select View -> Toolbars and choose Form Controls
2. A new floating tool bar will appear. Click the Push Button icon on this tool bar. This icon is a plain gray rectangle with rounded corners. If you cannot select the Push Button icon, you need to change the tool bar to Design Mode ON. This is done with icon that contains a pencil, ruler and triangle. On my tool bar it is the second icon, just next to the selection arrow.
3. Draw the button wherever you want it to be.
4. Right click on the button and select Control. This will bring up a new dialog.
5. On the General tab of the dialog you can adjust the appearance of the button.
6. On the Events tab you can tie the button to a macro. Pick an event, say Mouse Button Released, and click the small button aligned with that event on the right side of the dialog.
7. In the new dialog that appears, click on the Macro button.
8. In the Macro Selector dialog expand the My Macros list. Any Python modules you have saved in your user profile will be in the list. Select the Module and Function that you want associated with the button.
9. Close all of the open dialogs.
10. Put the Form Control tool bar in Design Mode Off by clicking the same button that you used to turn Design Mode On. Most of the items on the tool bar become unavailable when the design mode is off.
A single click on the button will now execute your Python macro.

I don't understand what you mean by
to add search lines
to add choises

Re: pyhton embedded within an OpenOffice.org's document

Posted: Sat Dec 11, 2010 8:09 pm
by Villeroy
I think Amber10 wants to embed Python code in document which is possible in theory. No, I don't know how, even though Python is my preferred macro language. When embedding makes sense I switch to Basic.

Re: python embedded within an OpenOffice.org's document

Posted: Sun Dec 12, 2010 6:15 pm
by Amber10
Can smbd explain what means sentence "move the scripts into the document with your favorite zip tool."

If you want to ship your self written python scripts within a document, you should finally move the scripts into the document with your favorite zip tool.
from http://wiki.services.openoffice.org/wik ... o_language
"Embedded within an OpenOffice.org's document"

OR whole paragraph:
Embedded within an OpenOffice.org's document

An OpenOffice.org document is a zip-File, which contains different files. Python scripts within documents are stored in Scripts/python subdirectory.

If you want to ship your self written python scripts within a document, you should first develop your scripts in the (above mentioned) user directory and then finally move the scripts into the document with your favorite zip tool. However, note that you must reassign every binding you did before to the script instances in the document. Ideally, you move away the scripts from the user directory and do a regression test on the document's functionality.

After moving the script files into the document, you have to add some lines (boldly printed) to the META-INF/manifest.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE manifest:manifest PUBLIC "-//OpenOffice.org//DTD Manifest 1.0//EN" "Manifest.dtd">
<manifest:manifest xmlns:manifest="http://openoffice.org/2001/manifest">
<manifest:file-entry manifest:media-type="application/vnd.oasis.opendocument.text" manifest:full-path="/"/>
<manifest:file-entry manifest:media-type="application/vnd.sun.xml.ui.configuration" manifest:full-path="Configurations2/"/>
<manifest:file-entry manifest:media-type="" manifest:full-path="Pictures/"/>
<manifest:file-entry manifest:media-type="text/xml" manifest:full-path="content.xml"/>
<manifest:file-entry :media-type="text/xml" manifest:full-path="styles.xml"/>
<manifest:file-entry manifest:media-type="text/xml" manifest:full-path="meta.xml"/>
<manifest:file-entry manifest:media-type="" manifest:full-path="Thumbnails/thumbnail.png"/>
<manifest:file-entry manifest:media-type="" manifest:full-path="Thumbnails/"/>
<manifest:file-entry manifest:media-type="text/xml" manifest:full-path="settings.xml"/>
<manifest:file-entry manifest:media-type="" manifest:full-path="Scripts/python/push_me.py"/>
<manifest:file-entry manifest:media-type="application/binary" manifest:full-path="Scripts/python/"/>
<manifest:file-entry manifest:media-type="application/binary" manifest:full-path="Scripts/"/>
</manifest:manifest>

When you open the document afterwards, the OpenOffice.org's UI should warn you about script content within the document (when this is not the case, you either have switched off the warning in the options or you did something wrong).

Example:To see how it works, download push_me_python4.odt. The document contains a push button and a multi line edit control. Pressing the button adds a new line with the current time stamp to the multi line edit control.

Re: pyhton embedded within an OpenOffice.org's document

Posted: Mon Dec 13, 2010 9:01 am
by Amber10
I don't understand what you mean by
to add search lines
to add choises
the same as a "button" - i mean i wanted to make a tool-bar from which i am going filter-out all "search text occurrences" and which has "choice buttons".

Thanks for the reply. It is incredible simply. Maybe you can help more.
I am getting errors:

1) Build in modules do not work:

Code: Select all

com.sun.star.uno.RuntimeExceptionError during invoking function createTable in module
Samples/TableSample.py (<type [b][size=150]'exceptopns.TypeError[/size][/b]'>:createTable()[b][size=150] takes no argument (1 given)[/size][/b])
/opt/openoffice.org/basis3.2/program/pythonscript.py:790 in function invoke() [ret=self.func(*args)]
SOLUTION: go to the script and add to the empty function argumrnt list word "object",
i.e. for /opt/openoffice.org/basis3.2/share/Scripts/python/HelloWorld.py

Code: Select all

def HelloWorldPython():
change to

Code: Select all

def HelloWorldPython(object ):
Assign this Pyhton script to the button by
drawing it with View-->Toolbars-->FormControls
and after doubl cliccking , coosing action, assigning the sript As described above in answer of "FJCC » Sat Dec 11, 2010 7:58 pm".

2) My modules are seen - it is impossible to select them as a function for a button, "OK" is disabled for my own modules in folder Samples/

Re: python embedded within document

Posted: Mon Dec 13, 2010 10:22 am
by Amber10
Maybe there is a problem with OpenOffice version

The button "OK" is active and assigns Python Macros which come with installation (Nevertheless they do not execute, show TypeError: takes no argument (1 given)). Python Macros which come with installation and are in folders:
/opt/openoffice.org/basis3.2/share/Scripts/python
/opt/openoffice.org/basis3.2/share/Scripts/python/pythonSamples

The button "OK" is not active for my own Python scripts although they are seen in MacroSelector from Tools-->Macros-->RunMacro.

SOLUTION
Do not try to run scripts from internet examples.
Just copy and paste already given example to another name file in sample directory and try to loud/run.
( /opt/openoffice.org/basis3.2/share/Scripts/python/ )
Follow logics in example scripts in installation, since internet examples are 6-8 years old.

For example:
1) Installation example which works after adding word "object"

View->Toobars -> Form Controls select button and draw on OpenOffice page. Double press, choose action, alter choose your file "CopiedOffice321Hello.py"
Tools->Macros->OrganizeMacros->Python - it will be possible to run the HelloWorldPython in "CopiedOffice321Hello.py"

def HelloWorldPython(object ):
"""Prints the string 'Hello World(in Python)' into the current document"""
#get the doc from the scripting context which is made available to all scripts
model = XSCRIPTCONTEXT.getDocument()
#get the XText interface
text = model.Text
#create an XTextRange at the end of the document
tRange = text.End
#and set the string
tRange.String = "Hello World (in Python)"
return None


2) Internet example is impossible to loud. Button "OK" is grey:

import uno

""" Here is the sequence of things the lines do:
1. Get the uno component context from the PyUNO runtime
2. Create the UnoUrlResolver
3. Get the central desktop object
4. Declare the ServiceManager
5. Get the central desktop object
6. Access the current writer document
7. Access the document's text property
8. Create a cursor
9. Insert the text into the document """

localContext = uno.getComponentContext()
resolver = localContext.ServiceManager.createInstanceWithContext("com.sun.star.bridge.UnoUrlResolver", localContext )
ctx = resolver.resolve( "uno:socket,host=localhost,port=2002;urp;StarOffice.ComponentContext" )
smgr = ctx.ServiceManager
desktop = smgr.createInstanceWithContext( "com.sun.star.frame.Desktop",ctx)
model = desktop.getCurrentComponent()
#text = model.Text
cursor = model.Text.createTextCursor()

text.insertString( cursor, "Hello World", 0 )

""" Do a nasty thing before exiting the python process. In case the
last call is a one-way call (e.g. see idl-spec of insertString),
it must be forced out of the remote-bridge caches before python
exits the process. Otherwise, the one-way call may or may not reach
the target object.
I do this here by calling a cheap synchronous call (getPropertyValue)."""
ctx.ServiceManager

Re: python embedded within document

Posted: Mon Dec 13, 2010 12:45 pm
by Villeroy
For some crude reason Tools>Macros>Run depends on Java.
May be menu:Tools>Macros>Organize>Python?
A button on a document?
Button on a toolbar? menu:Tools>Customize...
A hyperlink can also call a macro:
vnd.sun.star.script:MyModule.py$MyDef?language=Python&location=application
 Edit: Addition to macros called by hyperlink: 
The above location specifies the python macros in the OOo installation directory.
The following locations specify the python macros in the user profile directory and in a document,
vnd.sun.star.script:MyModule.py$MyDef?language=Python&location=user
vnd.sun.star.script:MyModule.py$MyDef?language=Python&location=document

Re: python embedded within document

Posted: Wed Dec 15, 2010 6:36 pm
by hanya
My extension supports to embed a python script into a document.
http://hermione.s41.xrea.com/pukiwiki/i ... bbs2%2F194

- Install ModifiedPythonScriptProvider-0.4.2.oxt and EditorKicker-0.1.0.oxt.
- Restart your office.
- Set your text editor from Tools - Extension Manager - Editor Kicker - Option.
- Prepare your python macro on your pc.
- Open a document which you want to embed the macro.
- Choose Tools - Macro - Python menu and select the document.
- Create new library and a file in it and and push edit to embed from the file of the python macro.
- Store your document.

Re: python embedded within document

Posted: Wed Dec 15, 2010 7:59 pm
by Villeroy
Thank you very much for this nice script provider. It's an amazing demo of professional coding.