A simple Python Macro in LibreOffice 4.0

These instructions are provided on Ubuntu 13.04 environment.
Libre Office 4.0 was the newest version that was installed in my PC. I was experimenting Macro programming in LibreOffice 4.0. Since Libre is newer the documentation provides for macro writing and the extension writing seems less. I spent days on fiding the stuff related with the topics, as I am new to this topic. Yet there are documentation linked with Apache OpenOffice project. The beginners find more troubles there.

Open Office Documentation

LibreOffice Documentation

First open LibreOffice Writer and go through the following path.

    Tools->Macros->Organize Macros

In the last submenu you may find both ‘Libre Office Basic’ and ‘Python’. If you get this menu click Python. Then a dialog will be appeared with available Python Macros.

If you did not get ‘Python’ along with ‘LibreOfficeBasic’, below instructions will get you there. First close LibreOffice and start the ubuntu terminal. Then install the package libreoffice-script-provider-python by typing the following command.

sudo apt-get install libreoffice-script-provider-python

As I was noted this package install the python3-uno package which works with some other python version(python 3.x) rather than python 2.7. Because of that when the libraries are imported to the Macro confilcts could occur. Becuase of that I installed python-uno package which refers to the python version 2.7 by running following command on the terminal.

sudo apt-get install python-uno

It will remove the already installed python3-uno. Now when you go to Tools->Macros->OrganizeMacros both ‘LibreOfficeBasic’ and ‘Python’ will be found. When you click python you will get the dialog shown below.

pythonmacro1

LibreOffice provides only the ‘Run’ facility for Python Macros. But for LibreOfficeBasic you will get all the facilities mentioned in the dialog. The macro “HelloWorld.py” is provided by the package script provider as a sample macro and it can be seen in the dialog.

Select the macro ‘LibreOfficeMacros->HelloWorld->HelloWorldPython’ and Run it. The string ‘Hello World’ will be printed on the opened LibreOffice Document. The code of  ‘HelloWorld.py’ is shown below.

 

def HelloWorldPython( ):

    desktop = XSCRIPTCONTEXT.getDesktop()
    model = desktop.getCurrentComponent()
    if not hasattr(model, “Text”):
    model = desktop.loadComponentFromURL(
    “private:factory/swriter”,”_blank”, 0, () )
    text = model.Text
    tRange = text.End
    tRange.String = “Hello World (in Python)”
    return None

 

If the macro is not found in the dialog open a text document and paste the above mentioned code and save the file as ‘HelloWorld.py‘. Then move it to the directory,

    ‘/usr/lib/libreoffice/share/Scripts/python’

You cannot just copy and paste, use terminal to copy the file.

sudo cp file_name_with_location /usr/lib/libreoffice/share/Scripts/python

After that run the macro in LibreOffice. By following above steps you can write python macros and execute them. Try to build an extension.

Advertisements

A simple Dart program with MongoDb on Ubuntu

What is Dart? – Dart is a new language, with tools and libraries for structured web app engineering according to its’ official web site http://www.dartlang.org/ . The most important features I have found in dart is its simplicity (single language to both server side and client side, avoiding complex features ), faster performance, easier to setup build environment etc.

What is MongoDb? – MongoDb is a NoSQL database where you can store data as documents which are scalable and flexible. http://www.mongodb.org/about/introduction/

Setting up the environment

Installing dart is an easier task. The installation package can be downloaded from http://www.dartlang.org/tools/editor/  and the user only has to unzip it. No other configuration is needed, but if someone need to get configuration details those are also available in the above link. By simply clicking on the DartEditor in the dart folder created by the extractions of the above, you can start coding Dart.

Installing MongoDb is easier than that. By typing  sudo apt-get install mongodb on terminal MongoDb will be installed. After the installation Mongo services will run automatically. If it is not type sudo service mongodb start on terminal. Now the database service is available. By typing mongo on terminal, you can get the MongoDb shell. Using Mongo shell all the database features could be handled. For more info browse http://docs.mongodb.org/manual/contents/ .

Start Creating a Database

I create the same database as mentioned in http://docs.mongodb.org/manual/core/create/ as the database example here. The database name is test. Collection name is bios.

>db.bios.insert( {_id: 1,name: { first: 'John', last: 'Backus' }, birth: new Date('Dec 03, 1924'), death: new Date('Mar 17, 2007') } )

To check whether the data we entered are there, it can be checked with following.

> db.bios.find();
{ "_id" : 1, "name" : { "first" : "John", "last" : "Backus" }, "birth" : ISODate("1924-12-02T18:30:00Z"), "death" : ISODate("2007-03-16T18:30:00Z") }

Start Creating the Application

Now go to the Dart Editor and create new Application. Select command-line application and then click on finish  I named it here as MongoTest.

Screenshot from 2013-07-09 22:53:13

Now the package mongo_dart should be added. For that open pubspec.yaml file in Folder MongoTest->packages in side bar. You will find a section as dependencies. Click on Add button and then type mongo_dart in the dialog appears. Then click Ok button and save by pressing Ctrl+S.  For further details on package manager in Dart, go to http://pub.dartlang.org .

Now open mongotest.dart, You can import the mongo_dart library by adding the following line.

import 'package:mongo_dart/mongo_dart.dart';

Now you will be able to call all the functions which are mentioned in mongo_dart API reference. http://pub.dartlang.org/packages/mongo_dart .

Following code will enable you to connect with MongoDb and fetch data. In the Dart Editor Console the output will be displayed.

import 'package:mongo_dart/mongo_dart.dart';


void main() {
dbConnection()
}


void dbConnection() {
var db = new Db('mongodb://127.0.0.1/test');
db.open().then((success){
if(success){
var _bios = db.collection("bios");
Cursor cur = _bios.find();
cur.each((Map bio){
print(bio);
});
}
});
}

After that press the launch button in the editor. If the launch was not successful and having errors with Dartium try the instructions in the following link   http://code.google.com/p/dart/wiki/PreparingYourMachine#Linux . The instructions given in that didn’t work for me. What I had done was, followed instructions in https://groups.google.com/a/dartlang.org/forum/#!topic/misc/V3u8oogUDxQ and install the Dartium using command line.

sudo add-apt-repository ppa:hachre/dart
sudo apt-get update
sudo apt-get install darteditor dartsdk dartium
Then It worked for me. (Run with Dartium)
Do not import ‘dart:html’ . Then it will ask for associated .html files . The package mongo_dart is using ‘dart:io’ . It works only in the server side.
The following result will be appeared in the DartEditor console.
{_id: 1.0, name: {first: John, last: Backus}, birth: 1924-12-03 00:00:00.000, death: 2007-03-17 00:00:00.000}