Connecting/talking to OpenOffice Base files

norseman norseman at hughes.net
Tue Apr 28 13:39:17 EDT 2009


deostroll wrote:
> 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
> --
> http://mail.python.org/mailman/listinfo/python-list
> 
=========================================
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




More information about the Python-list mailing list