#! /opt/OpenOffice.org1.1.0/program/python """ Converts a sxw file to doc, using OpenOffice. Will fire up OpenOffice by itself if no instance is currently running. """ import getopt,sys sys.path += ["/opt/OpenOffice.org1.1.0/program/"] import uno from unohelper import Base,systemPathToFileUrl, absolutize import os import time from com.sun.star.beans import PropertyValue from com.sun.star.beans.PropertyState import DIRECT_VALUE from com.sun.star.uno import Exception as UnoException from com.sun.star.io import IOException,XInputStream, XOutputStream pid = 0 def main(): retVal = 0 doc = None opts, args = getopt.getopt(sys.argv[1:], "hc:",["help", "connection-string=" , "html"]) format = None url = "uno:socket,host=localhost,port=2002;urp;StarOffice.ComponentContext" for o, a in opts: if o in ("-h", "--help"): usage() sys.exit() if o in ("-c", "--connection-string" ): url = "uno:" + a + ";urp;StarOffice.ComponentContext" if not len( args ): usage() sys.exit() ctxLocal = uno.getComponentContext() smgrLocal = ctxLocal.ServiceManager resolver = smgrLocal.createInstanceWithContext( "com.sun.star.bridge.UnoUrlResolver", ctxLocal ) try: ctx = resolver.resolve( url ) except UnoException, e: sys.stderr.write( "Error ("+repr(e.__class__)+") :" + e.Message + "\n" ) print "Couldn't connect to soffice- starting our own..." global pid pid=os.fork() if pid: time.sleep(5) else: os.execlp("soffice", "\"-accept=socket,host=localhost,port=2002;urp;\"") sys.exit(); try: ctx = resolver.resolve( url ) smgr = ctx.ServiceManager desktop = smgr.createInstanceWithContext("com.sun.star.frame.Desktop", ctx ) cwd = systemPathToFileUrl( os.getcwd() ) inProps = PropertyValue( "Hidden" , 0 , True, 0 ), for path in args: try: fileUrl = uno.absolutize( cwd, systemPathToFileUrl(path) ) doc = desktop.loadComponentFromURL( fileUrl , "_blank", 0,inProps) print path if not doc: raise UnoException( "Couldn't open stream for unknown reason", None ) filterName = "MS Word 97" # for writing to stdout class OutputStream( Base, XOutputStream ): def __init__( self ): self.closed = 0 def closeOutput(self): self.closed = 1 def writeBytes( self, seq ): sys.stdout.write( seq.value ) def flush( self ): pass outProps = (PropertyValue( "FilterName" , 0, filterName , 0 ), PropertyValue( "Overwrite" , 0, True , 0 ), PropertyValue( "OutputStream",0, OutputStream(),0)) doc.storeToURL("file://"+os.getcwd()+"/"+"test.doc",outProps) except IOException, e: sys.stderr.write( "IOError during conversion: " + e.Message + "\n" ) retVal = 1 except UnoException, e: sys.stderr.write( "Error ("+repr(e.__class__)+") during conversion:" + e.Message + "\n" ) retVal = 1 if doc: doc.dispose() except UnoException, e: sys.stderr.write( "Error ("+repr(e.__class__)+") :" + e.Message + "\n" ) retVal = 1 except getopt.GetoptError,e: sys.stderr.write( str(e) + "\n" ) usage() retVal = 1 print "Conversion finished" if pid: print "kill "+str(pid) os.system("kill "+str(pid)) sys.exit(retVal) def usage(): sys.stderr.write( "usage: sxw2doc.py --help |\n"+ " [-c | --connection-string=\n"+ " file1 file2 ...\n"+ "\n" + "Converts a sxw file to a ms doc file\n" + "Will either connect to a given openoffice instance or start its own\n"+ "\n"+ ) main()