Word is smart, but OpenOffice is wise
UPDATE 19/09: it seems that some people had misunderstood the post title, so here is a clarification: I’m not comparing Word with OpenOffice or something like that, the title refers to the design choices of OpenOffice in using Python 2.6 as an option for scripting language, it’s a humorous title and should not be considered in literal sense.
This is a very simple, but powerful, Python script to call Google Sets API (in fact it’s not an API call – since Google doesn’t have an official API for Sets service – but an interesting and well done scraper using BeautifulSoup) inside the OpenOffice 3.1.1 Writer… anyway, you can check the video to understand what it really does:
And here is the very complex source-code:
from xgoogle.googlesets import GoogleSets
def growMyLines():
""" Calls Google Set unofficial API (xgoogle) """
doc = XSCRIPTCONTEXT.getDocument()
controller = doc.getCurrentController()
selection = controller.getSelection()
count = selection.getCount();
text_range = selection.getByIndex(0);
lines_list = text_range.getString().split("\n");
gset = GoogleSets(lines_list)
gset_results = gset.get_results()
results_concat = "\n".join(gset_results)
text_range.setString(results_concat);
g_exportedScripts = growMyLines,
You need to put the “xgoogle” module inside the “OpenOffice.org 3\Basis\program\python-core-2.6.1\lib” path, and the above script inside “OpenOffice.org 3\Basis\share\Scripts\python”.
I hope you enjoyed =) with new Python 2.6 core in OpenOffice 3, they have increased the productivity potential at the limit.
What’s with the comma at the end? is something missing?
Hello man, it’s to create a tuple, in Python, “2” and “2,” are two different object types.
The , at the end is really strange… I haven’t seen that before..
BTW am I the only one who thinks that Openoffice could need a facelift? =)
Yah, Martin,
We make tuples with comma-separated values. Parenthesis are optional.
x = 1, 2, 3, 4, 5
or
x = 1, 2, 3, 4, 5,
or
x = (1, 2)
But what does a tuple with one value look like?
Not
x = 1
or
x = (1)
There’s no container there, just the value one. in the parenthesized version, it looks too much like an arithmetical grouping paren.
x = 1,
x = (1,)
This means make a tuple container and put the only value, 1, in it.
Would OpenOffice really complain if you just returned a one-element list?
[growMyLines]
If you DO need a tuple, this might be clearer:
tuple([growMyLines])
One could easily miss that comma scanning the code, even if you know what it means, so I prefer a more verbose convention.
Love your python posts. Keep them coming.
@Mark, that’s true, but I still love Latex =)
@Chad, thank’s for explanation for Martin
@Paul, I think that OOo doesn’t care if it’s a tuble or list..
@Gmon, thank you for kindly words.
I am working on a Python project that uses the xgoogle modules. It is really fun stuff. I love being able to search for and return a page with 100 websites, instead of the thirty or so that Google lets you look at with their API or the wasted time copying and pasting off the google page itself.