Discussion:
Python and Open Office
Maric Michaud
2008-09-15 12:16:00 UTC
Permalink
Hello,
I would like to create and manipulate Open Office documents using Python.
I have found then UNO Python page and odfpy modules which seem to be
exactly what I need. The odfpy manual is, to me, a confusing list of
objects and methods (it's an impressive list!), but does not have much in
the way of how to use them. For example, I can open a spreadsheet and
create new pages (there's a nice example near the back of the manual) but I
can't figure out how to open an existing spreadsheet and list the names of
the individual sheets ("tabs").
I have written an application that access Microsoft Excel and creates
reports for work, but would like to create an Open Source version using
Open Office and release it to the community (and maybe get a talk at PyCon
:-).
Is there someone here who can help me out, or is there an appropriate
mailing list for me to join?
Thanks
--greg
I don't like the UNO solution, first, UNO API is not that practical, second
you'll need to have a running instance of openoffice, which I wouldn't want
to manage for a server application in term of performance, reliability and
security.

I had to produce text document within a zope application once, and did this by
replacing variables values in a template directly in the xml, the function
was no more than twenty lines long, and used only stdlib modules (see below).
A far better approach IMHO.

Of course for big manipultaion it could be more error prone and it's a good
news that odfpy exists (I didn't use for my example because it was too
[1]: import odf.opendocument
[2]: import odf.table
[3]: new = odf.opendocument.OpenDocumentSpreadsheet()
new.spreadsheet.addElement(odf.table.Table(name=n))
[5]: new.save("toto.ods")
maric at redflag1 17:10:27:~/odfpy-0.8$ ooffice toto.ods # document ok !
maric at redflag1 17:10:31:~/odfpy-0.8$ ipython
[12]: import odf.table
[13]: import odf.opendocument
print t.getAttribute('name')
....:
....:
titi
tata
toto

Great !


This was the function I use (from a local copy of the product I backed up, I
have not the application working nor the svn server so I didn't test this
version, but it's mainly the idea that is interesting) :

def updateVariablesInSXW(self, variables, sxw) :
from zipfile import ZipFile
from StringIO import StringIO
from xml.dom.minidom import parseString
sxw = StringIO(sxw)
zip = ZipFile(sxw, 'a')
dom_document = parseString(zip.read('content.xml'))
dom_styles = parseString(zip.read('styles.xml'))
def update_variables(node) :
if node.nodeName in ('text:variable-set','text:variable-get') :
variable = node.attributes['text:name'].value
if variable in variables :
value = variables[variable].decode('utf-8')
if node.attributes.has_key('text:value') :
node.attributes['text:value'].value = value
if node.attributes.has_key('text:string-value') :
node.attributes['text:string-value'].value = value
if node.firstChild is not None :
node.firstChild.nodeValue = value
for i in node.childNodes :
update_variables(i)
for i in (dom_document, dom_styles) :
update_variables(i)
zip.writestr('content.xml', dom_document.toxml().encode('utf-8'))
zip.writestr('styles.xml', dom_styles.toxml().encode('utf-8'))
sxw.seek(0)
return zip.read()


and a sample use (code frome a Zope product, facture is french term for
invoice) :

security.declarePrivate('prepareFacture')
def prepareFacture(self) :
import math
price, devise = self.getProgramme().getPrice().split()
end = self.getProgramme().end()
start = self.getProgramme().start()
subscriber = self.getSubscriberInfo()
variables = {
'fullname' : subscriber.fullname,
'address' : subscriber.address,
'company' : subscriber.company,
'price' : price,
'quantity' : str(1),
'duration' : str(int(math.ceil(end - start))),
'ht' : price + ' ' + devise,
'ttc' : str(float(price)*1.196) + ' ' + devise,
'tva' : str(float(price)*0.196) + ' ' + devise,
'cours' : self.getProgramme().title_or_id(),
'location' : self.getProgramme().getLocation().title_or_id(),
'start' : self.toLocalizedTime(start),
'end' : self.toLocalizedTime(end),
#'timetable' : self.getProgramme().getTimetable(),
'num_facture' : self.getNumFacture(),
'cp' : subscriber.postal,
'country' : subscriber.country,
'city' : subscriber.city,
}
if subscriber.country.strip().lower() == 'france' :
template_name = 'ModeleFactureFormationTVA.sxw'
else :
template_name = 'ModeleFactureFormation.sxw'

template = str(getattr(self, template_name))
facture = self.updateVariablesInSXW(variables, template)
self.setFactureSXW(facture, filename="facture_%s.sxw" %
self.getNumFacture())
--
_____________

Maric Michaud
Colin J. Williams
2008-09-10 21:04:57 UTC
Permalink
Hello,
I would like to create and manipulate Open Office documents using Python. I
have found then UNO Python page and odfpy modules which seem to be exactly
what I need. The odfpy manual is, to me, a confusing list of objects and
methods (it's an impressive list!), but does not have much in the way of how
to use them. For example, I can open a spreadsheet and create new pages
(there's a nice example near the back of the manual) but I can't figure out
how to open an existing spreadsheet and list the names of the individual
sheets ("tabs").
I have written an application that access Microsoft Excel and creates
reports for work, but would like to create an Open Source version using Open
Office and release it to the community (and maybe get a talk at PyCon :-).
Is there someone here who can help me out, or is there an appropriate
mailing list for me to join?
Ciao, Greg.
you should check with the openoffice.org mailing list; I think what
you are looking for is the api mailing list for openoffice; you could
try to get the OpenOffice.org developers guide and the SDK, and check
it (but it is not a little work)
Regards
Marco
Thanks
--greg
--
http://mail.python.org/mailman/listinfo/python-list
Greg,

If you follow this up, I hope that you
will post info to c.l.p
and let us know whether the UNO
interface is Python 2.5 compatible.

The last time I looked it was set for
2.3 or 2.4.

Colin W
Peter Georgeson
2008-09-11 13:35:32 UTC
Permalink
Post by Colin J. Williams
Hello,
I would like to create and manipulate Open Office documents using Python. ?I
have found then UNO Python page and odfpy modules which seem to be exactly
what I need. ?The odfpy manual is, to me, a confusing list of objects and
methods (it's an impressive list!), but does not have much in the way of how
to use them. ?For example, I can open a spreadsheet and create new pages
(there's a nice example near the back of the manual) but I can't figure out
how to open an existing spreadsheet and list the names of the individual
sheets ("tabs").
I have written an application that access Microsoft Excel and creates
reports for work, but would like to create an Open Source version using Open
Office and release it to the community (and maybe get a talk at PyCon :-).
Is there someone here who can help me out, or is there an appropriate
mailing list for me to join?
Ciao, Greg.
you should check with the openoffice.org mailing list; I think what
you are looking for is the api mailing list for openoffice; you could
try to get the OpenOffice.org developers guide and the SDK, and check
it (but it is not a little work)
Regards
Marco
Thanks
--greg
--
http://mail.python.org/mailman/listinfo/python-list
Greg,
If you follow this up, I hope that you
will post info to c.l.p
and let us know whether the UNO
interface is Python 2.5 compatible.
The last time I looked it was set for
2.3 or 2.4.
Colin W
I've recently been working on interfacing with OpenOffice via UNO with
Python.

I can confirm that unfortunately, the PyUNO interface presently
(OpenOffice 2.4) is built with Python 2.3... so to use the UNO
interface from Python you have to write a separate script to run in
the OpenOffice Python 2.3 environment.

Apparently someone is working on rebuilding the PyUNO library for
Python 2.5 but there's no timeframe for when that might be included
with the OpenOffice distribution.

You could try building PyUNO with Python 2.5 yourself, I'm sure I came
across instructions somewhere on the OpenOffice developer website...

I've not used odfpy so don't know how it compares in terms of degree
of difficulty to UNO.
Hartmut Goebel
2008-09-16 17:32:14 UTC
Permalink
Post by Peter Georgeson
I can confirm that unfortunately, the PyUNO interface presently
(OpenOffice 2.4) is built with Python 2.3... so to use the UNO
interface from Python you have to write a separate script to run in
the OpenOffice Python 2.3 environment.
This may be true for Windows. On Linux - at least for Mandriva --, OOo
is already integrated with Python 2.5.
--
Sch?nen Gru? - Regards
Hartmut Goebel
Dipl.-Informatiker (univ.), CISSP

Goebel Consult
Spezialist f?r IT-Sicherheit in komplexen Umgebungen
http://www.goebel-consult.de
David Boddie
2008-09-18 19:58:47 UTC
Permalink
<http://opendocumentfellowship.com/files/api-for-odfpy_2.odt>
I wrote my comment *after* looking at the above, which I found easily
enough. After 7 pages of (helpful) explanatory text, there follow 88
[...]
which are translated to a more readable form from the Relax-NG schema
(formal specs) in the standard. But I have no idea what a Ruby property
is in this context. It would be much like reading the grammar entries
in the Python Reference without the explanatory text that follows.
I started using odfpy for a project, and I found that I spent a lot of time
flicking back and forth in the API manual and cross-referencing it with the
specification. The simple examples were useful enough to get started with,
but I could have used more information about how to use certain classes
and what elements and attributes they required.

I also wanted to add equations to the documents I was producing and that
requires some knowledge of MathML.

Certainly, it's not a very high level API for documentation creation - you
need to know something about the format to be able to construct documents.

David
Greg Lindstrom
2008-09-10 20:04:57 UTC
Permalink
Hello,

I would like to create and manipulate Open Office documents using Python. I
have found then UNO Python page and odfpy modules which seem to be exactly
what I need. The odfpy manual is, to me, a confusing list of objects and
methods (it's an impressive list!), but does not have much in the way of how
to use them. For example, I can open a spreadsheet and create new pages
(there's a nice example near the back of the manual) but I can't figure out
how to open an existing spreadsheet and list the names of the individual
sheets ("tabs").

I have written an application that access Microsoft Excel and creates
reports for work, but would like to create an Open Source version using Open
Office and release it to the community (and maybe get a talk at PyCon :-).

Is there someone here who can help me out, or is there an appropriate
mailing list for me to join?

Thanks

--greg
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20080910/35357106/attachment.html>
Marco Bizzarri
2008-09-10 20:08:17 UTC
Permalink
Hello,
I would like to create and manipulate Open Office documents using Python. I
have found then UNO Python page and odfpy modules which seem to be exactly
what I need. The odfpy manual is, to me, a confusing list of objects and
methods (it's an impressive list!), but does not have much in the way of how
to use them. For example, I can open a spreadsheet and create new pages
(there's a nice example near the back of the manual) but I can't figure out
how to open an existing spreadsheet and list the names of the individual
sheets ("tabs").
I have written an application that access Microsoft Excel and creates
reports for work, but would like to create an Open Source version using Open
Office and release it to the community (and maybe get a talk at PyCon :-).
Is there someone here who can help me out, or is there an appropriate
mailing list for me to join?
Ciao, Greg.

you should check with the openoffice.org mailing list; I think what
you are looking for is the api mailing list for openoffice; you could
try to get the OpenOffice.org developers guide and the SDK, and check
it (but it is not a little work)

Regards
Marco
Thanks
--greg
--
http://mail.python.org/mailman/listinfo/python-list
--
Marco Bizzarri
http://notenotturne.blogspot.com/
http://iliveinpisa.blogspot.com/
Terry Reedy
2008-09-16 19:17:56 UTC
Permalink
As I thought everybody already knows, ODF is simply a Zip-File
containing some XML files.
I thought it just *was* xml. But examination with notepad showed
otherwise.
So there is no need to use OOo for handling
the files.
So odfpy seams to be the best way to to this.
Since odfpy automatically handles combining the at-least-4 xml files
into one coherent class on loading, and putting the various properties
into the proper xml files on saving, this looks pretty straightforward.
Thanks for the reference.

One way to learn the meaning of some of the numerous attributes and
values is to create a file with the wanted features with OOo, save,
unzip, and examine the xml to see which tags are used for which features.


You may also want to check
OOopy <http://pypi.python.org/pypi/OOoPy/>.
I will glance at that too.
If you are going to interact with OOo, you may want to try out
<http://openoffice-python.origo.ethz.ch/>
Gary Herron
2008-09-10 20:39:18 UTC
Permalink
Hello,
I would like to create and manipulate Open Office documents using
Python. I have found then UNO Python page and odfpy modules which
seem to be exactly what I need. The odfpy manual is, to me, a
confusing list of objects and methods (it's an impressive list!), but
does not have much in the way of how to use them. For example, I can
open a spreadsheet and create new pages (there's a nice example near
the back of the manual) but I can't figure out how to open an existing
spreadsheet and list the names of the individual sheets ("tabs").
I have written an application that access Microsoft Excel and creates
reports for work, but would like to create an Open Source version
using Open Office and release it to the community (and maybe get a
talk at PyCon :-).
Is there someone here who can help me out, or is there an appropriate
mailing list for me to join?
Thanks
--greg
------------------------------------------------------------------------
--
http://mail.python.org/mailman/listinfo/python-list
Here's a snippet of code I use to open a spreadsheet (given as a file
path name), and compute and return the list of sheets it contains. It
connects to an existing OpenOffice if possible, otherwise it starts
OpenOffice and waits for it to accept a connection.



def OpenSS(path):
localContext = uno.getComponentContext()
resolver = localContext.ServiceManager.createInstanceWithContext(
'com.sun.star.bridge.UnoUrlResolver', localContext )


resolveArg='uno:socket,host=localhost,port=2002;urp;StarOffice.ComponentContext'

# Conect to running OO; First, starting OO if necessary;
try:
ctx = resolver.resolve(resolveArg)
except NoConnectException:
os.system("ooffice '-accept=socket,host=localhost,port=2002;urp;'&")
while 1:
print ' waiting for OpenOffice'
time.sleep(1)
try:
ctx = resolver.resolve(resolveArg)
break
except NoConnectException:
pass
smgr = ctx.ServiceManager
desktop = smgr.createInstanceWithContext(
'com.sun.star.frame.Desktop', ctx)

url = 'file://' + path
component = desktop.loadComponentFromURL(url, '_default', 0, ())
sheets = [component.getSheets().getByIndex(i)
for i in range(component.getSheets().getCount())]
return sheets


Hope this helps,

Gary Herro
Hartmut Goebel
2008-09-19 08:36:37 UTC
Permalink
<http://opendocumentfellowship.com/files/api-for-odfpy_2.odt>
I wrote my comment *after* looking at the above, which I found easily
enough. After 7 pages of (helpful) explanatory text, there follow 88
[...]

Well, I wrote my comment *before* looking at the API docs. Your are
absolutely right: It is not helpful.
<http://docs.oasis-open.org/office/v1.1/OS/OpenDocument-v1.1.pdf>
v1.0 is the adopted international (ISO/IEC) standard.
Specs for ODF 1.0 are available the oasis-open.org, too. So if you want
to keep close the the ISO standard you surely are better off using 1.0.
Alternativly you may use the 1.1 docs and skim the appendix about changes.

Regards
H. Goebel
Terry Reedy
2008-09-17 19:59:28 UTC
Permalink
Post by Terry Reedy
One way to learn the meaning of some of the numerous attributes and
values is to create a file with the wanted features with OOo, save,
unzip, and examine the xml to see which tags are used for which features.
<http://opendocumentfellowship.com/files/api-for-odfpy_2.odt>
I wrote my comment *after* looking at the above, which I found easily
enough. After 7 pages of (helpful) explanatory text, there follow 88
pages with hundreds of entries like this:

5.13.30 style.RubyProperties
Requires the following attributes: No attribute is required.
Allows the following attributes: rubyalign, rubyposition.
These elements contain style.RubyProperties: style.DefaultStyle,
style.Style.
The following elements occur in style.RubyProperties: No element is allowed.

which are translated to a more readable form from the Relax-NG schema
(formal specs) in the standard. But I have no idea what a Ruby property
is in this context. It would be much like reading the grammar entries
in the Python Reference without the explanatory text that follows.
<http://docs.oasis-open.org/office/v1.1/OS/OpenDocument-v1.1.pdf>
v1.0 is the adopted international (ISO/IEC) standard.
I did not notice whether odfpy supports the 1.1 extensitons, but since
it does appear that OOo now uses them, I will presume so.

This does help. It has more explanatory material.
"5.4 Ruby
A ruby is additional text that is displayed above or below some base
text. The purpose of ruby is to annotate the base text or provide
information about its pronunciation.

tjr
Hartmut Goebel
2008-09-17 13:46:13 UTC
Permalink
Post by Terry Reedy
One way to learn the meaning of some of the numerous attributes and
values is to create a file with the wanted features with OOo, save,
unzip, and examine the xml to see which tags are used for which features.
The API docs are a bit hidden on the webpage. Here is the link:
<http://opendocumentfellowship.com/files/api-for-odfpy_2.odt>

Additionally teh ODF sepcs may help:
<http://docs.oasis-open.org/office/v1.1/OS/OpenDocument-v1.1.pdf>

Regards
H. Goebel
Marco Bizzarri
2008-09-11 13:52:37 UTC
Permalink
Greg, as an addition to what I already said to you, you can consider
taking a look at oood from ERP5 project

http://wiki.erp5.org/HowToUseOood

OOOd (openoffice.org daemon) runs openoffice behind the scene, and
allows you to interact with it via XML-RPC; it should be quite robust,
since it is actively mantained and used in a big software project.
And, also, it should be quite easy to extend in order to have your
custom functions run via XML-RPC.

Regards
Marco
--
Marco Bizzarri
http://notenotturne.blogspot.com/
http://iliveinpisa.blogspot.com/
Hartmut Goebel
2008-09-16 17:29:31 UTC
Permalink
Hello,
I would like to create and manipulate Open Office documents using Python. I
[...]
Is there someone here who can help me out, or is there an appropriate
mailing list for me to join?
Ciao, Greg.
you should check with the openoffice.org mailing list; I think what
you are looking for is the api mailing list for openoffice; you could
try to get the OpenOffice.org developers guide and the SDK, and check
it (but it is not a little work)
If want to create documents, there is no nead to fight with OOo and UNO.
As I thought everybody already knows, ODF is simply a Zip-File
containing some XML files. So there is no need to use OOo for handling
the files.

So odfpy seams to be the best way to to this. You may also want to check
OOopy <http://pypi.python.org/pypi/OOoPy/>.

If you are going to interact with OOo, you may want to try out
<http://openoffice-python.origo.ethz.ch/>
--
Sch?nen Gru? - Regards
Hartmut Goebel
Dipl.-Informatiker (univ.), CISSP

Goebel Consult
Spezialist f?r IT-Sicherheit in komplexen Umgebungen
http://www.goebel-consult.de
Continue reading on narkive:
Loading...