Connecting/talking to OpenOffice Base files

D

deostroll

Hi,

I was wondering if the python interpretor can talk to files with
extension *.odb (OpenOffice Base files). They are like flat database
files, similar to Microsoft Access files. I want to store data into
them as well as extract data out of them.

--deostroll
 
K

Krishnakant

hi,
Hi,

I was wondering if the python interpretor can talk to files with
extension *.odb (OpenOffice Base files). They are like flat database
files, similar to Microsoft Access files. I want to store data into
them as well as extract data out of them.
This is done either using ooolib if you want to do some simple read and
write tasks or then use odfpy which is a complete library.

I had asked the same question a few months back and come from the same
path you are coming.

But odfpy was initially difficult for me and would take a while to
understand.
It is a kind of wrapper around the xml structure of an odf document.

happy hacking.
Krishnakant.
 
N

norseman

deostroll said:
Hi,

I was wondering if the python interpretor can talk to files with
extension *.odb (OpenOffice Base files). They are like flat database
files, similar to Microsoft Access files. I want to store data into
them as well as extract data out of them.

--deostroll
=========================================
No and Yes.

Python cannot, as far as I know, do direct read/write to the file proper.

Python can talk to OOo via a OOo type of COM setup, by way of which
Python can send OOo commands to do things. My personal experience with
this has shown me that OOo docs on the subject are 'a bit light' in both
narrative and example.

Suggestions:
1) Go to www.openoffice.org and look for UNO (OOo's COM)
2) Check out whatever seems best for you.
3) Do not expect much help, People or docs, from OOo.
4) To use the UNO you will need to use the OOo package supplied
python. Set up a wrapper to change to their environment
and start their python from it. Otherwise expect chaos.

5) Use the make macro builtins and save as python. Use those as
the base to work from. You MUST have OOo running BEFORE
trying to link an outside Python process.

Hope you have a better experience with them than I did.


Steve


A sample that does function. How valid? I'm not sure. :)

#!/bin/bash /opt/openoffice.org2.0/program/python.sh
# on my system, above actually works if this file is chmod 755
# A TEST
# SLT
# September, 2008
#
# works - sort of: the writer functions, although badly.
# there are no examples of cursor placement in text.
# the calc functions, but saveas .csv is NOT text
# it winds up OOo-PKzipped on disk.
# I have yet to find where the verbs below come from. OOo docs are
# among the world's worst, in my opinion. In order to get this
# to work at all I had to go to third party locations and horse
# around until I found a combination that worked.
# While I'm no fan of Microsoft;
# If you need automation in your office endeavors, Use Microsoft.
# At least it works with less effort and has some help to be found
# on the web. SUN seems to be sponsoring it's own demise. I guess
# they hired too many Microsoft loyalists.
#
# DOWN BELOW: change paths and files to suit your system.
#
# I know this prints ugly on paper. Blame it on children liking long
# words, presumably preferring increased typos too...
#

import os
import sys
import popen2
import time
import uno

pgm_in, pgm_out, pgm_err = popen2.popen3("scalc
accept=socket,host=localhost,port=2002\;urp\; &")


## if you use soffice in above line, program is likely to crash.
## it will not open generic txt files.
## for scalc, use that name above. for swriter, use that name above.

time.sleep(10)
## OOo and uno need time to activate. Otherwise whole thing bombs.

local = uno.getComponentContext()
resolver =
local.ServiceManager.createInstanceWithContext("com.sun.star.bridge.UnoUrlResolver",
local)
context =
resolver.resolve("uno:socket,host=localhost,port=2002;urp;StarOffice.ComponentContext")
OOoAPP =
context.ServiceManager.createInstanceWithContext("com.sun.star.frame.Desktop",
context)

comment= """
##for
OOoDOC = OOoAPP.loadComponentFromURL("file:///mnt/mass/py/zz.txt"
,"_blank", 0, ())
## line above can blow up. soffice doesn't handle generic text/names at
## all in this method of endeavor. Not even .txt extensions!!!!
## must start swriter, not soffice, if a generic text open is to succeed.

OOoDOC = OOoAPP.getCurrentComponent()
cursor = OOoDOC.Text.createTextCursor()
OOoDOC.Text.insertString(cursor, "This text is being added to openoffice
using python and uno package.", 0)
OOoDOC.Text.insertString(cursor, "\n\nThis is a new paragraph.", 0)
OOoDOC.storeAsURL("file:///mnt/mass/py/zz-tst.txt",())
##next
"""
#for
SPRDSHT1 = OOoAPP.loadComponentFromURL("file:///mnt/mass/py/x.xls"
,"_blank", 0, ())
SPRDSHT = OOoAPP.getCurrentComponent()
##cursor = SPRDSHT.Text.createTextCursor()
#SPRDSHT.storeAsURL("file:///mnt/mass/py/x-tst.csv",("Text - txt - csv
(StarCalc)"))
# ran, loaded, no-save
SPRDSHT.storeToURL("file:///mnt/mass/py/x-tst.csv",SPRDSHT._toProperties(FilterName="Text
- txt - csv (StarCalc)"))
SPRDSHT.exportToURL("file:///mnt/mass/py/x-tst.csv",("Text - txt - csv
(StarCalc)"))


SPRDSHT.dispose()
##next

OOoAPP.dispose()
## it may take several minutes for 'blank' to unload itself. Then too,
## this may leave the 'blank' hanging. If so, stop it manually before
## trying to use this again.
##
## if you have to kill it, run soffice manually and close it manually
## before attempting to use it again. Otherwise things get worse.
# end of test
 
L

Lawrence D'Oliveiro

norseman said:
# I know this prints ugly on paper. Blame it on children liking long
# words, presumably preferring increased typos too...

Remember this was initially designed to be done with Java. Python was an
afterthought, though it's interesting to see lots of examples where 3 lines
of Java (get object of superclass, cast to subclass, access method) collapse
to a single line of Python (get object, access method). :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,172
Messages
2,570,934
Members
47,477
Latest member
ColumbusMa

Latest Threads

Top