Skip navigation.

Open Life

Opendocuments, Web Office, Office suites

Posts tagged with "python"

Small hack on Python and OpenOffice.org

, , , ...

So I want to make a small tutorial for a baby step world. The main point is to re-use the python samples included in OpenOffice.org. We won't generate code, instead we will just compare notice the difference and modify slightly to change it's behavior.

The first thing we will do is copy the samples that are on the OpenOffice.org system and move them to our OpenOffice.org profile. We will assure that the permissions are assigned to us and finally try to make the pythonTables script behave like the HelloWorld script.

The HelloWorld insert a text into the document, while the pythonTables will generate a new document and then insert text, tables, frames and other writer utilities.

Let's get going!!!

First we will copy our script:


$ sudo cp -Rv /opt/openoffice.org2.2/share/Scripts/python/ \\
   ~/.openoffice.org2/user/Scripts/


Now we will change the permissions of the script:

$ cd ~/.openoffice.org2/user/Scripts/ \\
$ chown -Rv user.user python/


where user is your current user account name.

Now we will have some writing permissions:
$ chmod -Rv 766 python/*


Let the Hacking begin!!

So first we will run the scripts just to see that they work and nothing has gone wrong....
  • First step is starting OpenOffice.org: Launch OpenOffice.org
  • Go to Tools > Macros > Organize macros > Python...
  • You will face a dialog with a list of Python macros divided into 3 areas My Macros, OpenOffice.org Macros and Untitled.
  • Go to My Macros and expand it by clicking on the "+"
  • Select the HelloWorld and HelloWorldPython and click on Execute.


This will insert the text Hello World (in Python) on the current document.

Now lets try the other script, TableSample which will add a full text of the document. Repeat the steps that we mentioned before but instead of selecting HelloWorld, go to PythonSamples > TableSample > createTable.

This will generate a New Document with a bunch of information. So we now know that our scripts are working properly.

Open a text editor and open HelloWorld.py and TableSample.py from the ~/.openoffice.org2/user/Scripts/python folder. TableSamples might be in the pythonSamples folders so you will have to dig deeper to find it.

You will see that HelloWorld is a simple script of just 13 lines of code while TableSample.py have nearly 96 lines of code. Don't let that intimidate you, most of the lines are just because of the amount of data that it dump to the document, but the principle is not that many lines different.

Look at the HelloWorld.py and you will see the following line:
model = XSCRIPTCONTEXT.getDocument()
#get the XText interface
    text = model.Text


This is the most important line and the key to what we want. We can see that there is something called the XSCRIPTCONTEXT. This is what we are looking for and major difference between both codes.

XSCRIPTCONTEXT will save us a lot of hacking in the future of coding with pyuno. However now this line will help us use the current document.

Now lets turn to the TableSample.py, we will see many functions each one with a good chunk of code. If we look closer we can see that we have 2 functions, insertTextIntoCell, and createTable. InsertTextintoCell is just a few lines long and we can see that there is nothing to our use since it focus on configuring the table. We look into the createTable and we see immediately the comment: creates a new writer document and inserts a table with some data (also known as the SWriter sample).

See that wasn't that hard, we see that following the comment there is a line like:
 ctx=uno.getComponent()


We remember that the first script had something similar but on XSCRIPTCONTEXT instead of uno and getDocument() instead of getComponent(). If we keep on looking at the code we see other lines:
    smgr = ctx.ServiceManager
    desktop = smgr.createInstanceWithContext( "com.sun.star.frame.Desktop",ctx)
    
    # open a writer document
    doc = desktop.loadComponentFromURL( "private:factory/swriter","_blank", 0, () )


This becomes clearer that if something in the code should change, should be this part. The doc variable is the one that actually generates the new writer document. And then we will see actions applied to the doc container like on line 32 when we generate a table by creating an instance of the TextTable.
    table = doc.createInstance( "com.sun.star.text.TextTable" )


If we go back to HelloWorld.py we have the model variable as opposed to the doc variable but there is no fundamental difference and we can just replace that chunk of code. So we will just comment those lines and have something like:

def createTable():
    """creates a new writer document and inserts a table with some data (also known as the SWriter sample)""" 
#    ctx = uno.getComponentContext()
#    smgr = ctx.ServiceManager
#    desktop = smgr.createInstanceWithContext( "com.sun.star.frame.Desktop",ctx)
    
    # open a writer document
#    doc = desktop.loadComponentFromURL( "private:factory/swriter","_blank", 0, () )
    model = XSCRIPTCONTEXT.getDocument()
    text = model.Text
    cursor = text.createTextCursor()
    text.insertString( cursor, "The first line in the newly created text document.\n", 0 )
    text.insertString( cursor, "Now we are in the second line\n" , 0 )
    
    # create a text table
    table = model.createInstance( "com.sun.star.text.TextTable" )


However since we mention before that the original code reference the document as 'doc' and now we just assigned to 'model' we can do a search and replace so that the instructions like the table one reference to model.

We go ahead and execute createTable under Tools > Macros > Organize Macros > Python... and vioala we are done.
Final shot
Happy hacking!!!

LinuxCabal talk on Python and C

, , , ...

This saturday Patricio Paez gave a talk at Linux cabal on Saturday 28. So the talk main focus is not learning C or Python but a broad view of the interaction between Python and C. The first part was thinking about the way code works. First we will need the following elements:
  1. Language
  2. Platform
  3. Compilers
  4. Libraries
The stack of C will be:
  1. gcc - the compiler
  2. as - the assembler
  3. ld - linker
  4. libc - the C library
Additionally we will need a text editor, and a debugger usually known gdb.

Read more...

Digging into OLPC

, , , ...

So today I actually got into OLPC site once again, it all started when I was bored and download the FOSDEM video about the OLPC. One graph on the presentation of the video woke up my curiosity. It was a map of the earth with each country having different colors, like a rating for each country.
I wanted to download the presentation but failed, so my second choice was going to the website. However I found something better, a wiki of the OLPC project.
So decided to find what this colors actually meant, I start doing some searches for keywords related to ratings which gave me no result. The next step is to search for the country, I did a search for mexico and that gave me a whole profile page and it indeed had the grading.
Now that I saw that I look for the link where you can link up to the grading scale. In this case mexico was a red and there was other colors. The grades were the following:
  • Green
  • Red
  • Orange
  • Yellow
  • Gray

With the Green it means they will inmediately adopt Linux, in this countries there were already some interesting countries with not so lagging technical knowledge and with a great pump of technology.

Some other countries that were close to the challenge actually declined adopting linux such as Romania. Mexico, had an interesting stage, it was a 'Red' country which means that they might adopt it once is already working.

Interesting question is what will this make to the country waiting a few years. If they adopt it, there is also the challenge from the same government and just administration within the education.

The education ministry has had bad ventures within education and technology, it will be a shame projects such as OLPC get dirty under the bureacratic and corrupted government that can turn a good idea into uselessness.

Recently I read a post about a wikipedia scandal where government officials were deleting and modifiying posts of some of the government projects such as the same Enciclomedia which is an Encarta hooked into more interacting blackboards.

Unfortunately there had been many issues on that way. kOLPC

Managing Python extensions part II

, , , ...

This second part of Python extensions we will run some of the sample extensions in OpenOffice.org and how some of the more complex code happens in OOo. And how you can work it through.

First we will locate where the Python samples live in OpenOffice.org and how to get them to run in the OOo.

Running OOo Sample Macros
OpenOffice.org scripting framework support different languages, the menu also coordinate and manage different languages on different options. The Macros item located under Tools will give you options to record macros, execute macros as well as just view the macros acording to a specific language. This can be either:
  • OpenOffice.org BASIC
  • Python
  • Beanshell
  • Javascript
When you select any of this options you get a dialog that filter your macros by the language and just show you the language you got. Once you select the language you can just select the macros and click on execute.

If we want to select the HelloWorld.py we would do the following:
  1. Select Tools from the main menu
  2. Choose Macros and then decide what to choose
  3. You can select Execute macros and choose between the different macro languages
  4. Or select Organize Macros > Python and just choose between the python macros
  5. On any of the methods you will get a dialog and you will have 3 choices including: My Macros, OpenOffice.org Macros and the name of the file you are currently open.
  6. We want to select the OpenOffice.org Macros and you will see HelloWorld, choose it and u will expand it and display HelloWorldPython
  7. Once executed it will insert the legend Hello World (in Python) in your opendocument text.

You can experimenting running other samples:
  • Capitalize - This will change the capitalization of the text
  • MsgBox - This will open a dialog box which will ask for data and return one in exchange of the option
  • pythonSamples - TableSample - createTable - This will generate a document with text, headers and a rich format table populated with numbers and sum formula.

Editing OOo Macros
OpenOffice.org have different ways to edit the macros depending on the language. For OOoBasic OpenOffice.org includes an internal IDE which is in charge of editing the Macros.
For Javascript it uses the Rhyno Javascript Debugger, this debugger is brought from the mozilla community.

Finally the Beanshell scripts are also done with the Beanshell debugger, for more information check on the Openoffice.org website.

Finally our language for this session is Python, which doesn't has a native IDE and which scripting will not be able to edit within openoffice.org but on external editors. Please look on the previous blog entry to learn about how to install python scripts on OpenOffice.org.

Managing Python Extensions in OpenOffice.org I

, , , ...

For all Python fans out there, you might not know that OpenOffice.org can be run on Python. This is great because OpenOffice.org is a very interesting application but is hard to modify or develop on top of it. However extensions has been the way to go when you want to implement something new in OpenOffice.org. So here is a mini tour oriented to the way OpenOffice.org manage Python, which is different to the way OpenOffice.org handles the native OOoBasic. One thing is that OOoBasic is compatible with OpenOffice.org IDE that you get when you go to Tools > Macros > Manage Macros > OpenOffice.org Basic.

How Macros are handled.
Macros are usually handled on 3 stages, it can be either file based, user based, or application base.
  • The file based it means that the macros are stored in the file, this files will have the python source code within the document.
  • User based is that the macros are saved on the user preferences, this is usually stored in linux on the dot folders, in windows under the Application data folder.
  • Application base are stored in the applications folder which in linux is under /opt folder and on windows is under the Program Files folder.


The difference between this thre methods are the accesibility that you will get, for example document macros gain the portability of sending OpenDocument files and transporting the macro to other people on different computers.

The user-centric affect just the individual user account and might not be transferable to other users, for that you will want the application base. This also gain the availability since you can use the macro regardless of the document you are working on and it lives in OpenOffice.org. The global one lives in OOo and affect all users, this is the best one if you want to make large deployments.

What about Python
As I said before, Python is not a native language to OpenOffice.org, however you will be happy to know that python is included by default under OpenOffice.org. Is stored at openoffice.org2.2/program/python-core-2.3.4/ (versions might change); so you are able to use this macros on every openoffice.org installation.

As I mention before, the 3 layers that OOo can store macros will affect the installation of new macros developed in Python.

The easiest one is to have python saved in your application, so let say that you have the file MyScript.py. To load it to your OpenOffice.org application will be:
$ su
$ cp MyScript.py /opt/openoffice.org2.2/share/Scripts/ptyhon/
.

The user account level will be similar nad will execute by just doing the following:
$ cp MyScript ~/.openoffice.org2.2/user/Scripts/python/

* Be careful since the python folder is not created by default you will need to do create a folder under the Script folder.

The last one -- the document level is actually quite hard and might just put it here for reference but is more complicated than just coping the script inside the document. You will actually have to edit an XML file in order to manually register the file inside the document.

As you may know OpenDocuments are Zip files containing other files. They also have a file structure which will include:
meta.xml
content.xml
style.xml
mimetype
current.xml
Configuration2/
   accelerator
   images
   popmenu
   ...
META-INF/
   manifest.xml
Thumbnails/
   file.png

You will actually need to unzip and create a new folder called Scripts and inside create the python folder. Only then you will be able to copy your script MyScript.py inside the python folder.

Here is where you will need to edit the registry which is in the META-INF/manifest file and add 3 lines (before the final tag manifest:manifest].
 <manifest:file-entry manifest:media-type="" manifest:full-path="Scripts/python/MsgBox.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/"/>


This will register the path of the script "Scripts/python/MsgBox.py" , the path of the scripting folders one for "Scripts/python" and other just for the "Scripts" folder.

Next post I will put some sample script so you can run and test your scripts meanwhile I will like to point to some default Python scripts already in OpenOffice.org.

Please go to Tools > Macros > Administer Macros > Python select the main application labeled as OpenOffice.org Macros and expand. You will see many sample codes that might look familiar to the ones you found at openoffice.org2.2/share/Scripts/python/HelloWorld.py you might also find Capitalized and the folder pythonSamples.

Later I will suggest you some useful sites to download some Python components.
November 2009
S M T W T F S
October 2009December 2009
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30