3 from unohelper
import Base
,systemPathToFileUrl
, absolutize
6 from com
.sun
.star
.beans
import PropertyValue
7 from com
.sun
.star
.beans
.PropertyState
import DIRECT_VALUE
8 from com
.sun
.star
.uno
import Exception as UnoException
9 from com
.sun
.star
.io
import IOException
,XInputStream
, XOutputStream
11 class OutputStream( Base
, XOutputStream
):
15 def closeOutput(self
):
18 def writeBytes( self
, seq
):
19 sys
.stdout
.write( seq
.value
)
30 opts
, args
= getopt
.getopt(sys
.argv
[1:], "hc:",["help", "connection-string=" , "html"])
32 url
= "uno:socket,host=localhost,port=2002;urp;StarOffice.ComponentContext"
33 filterName
= "Text (Encoded)"
35 if o
in ("-h", "--help"):
38 if o
in ("-c", "--connection-string" ):
39 url
= "uno:" + a
+ ";urp;StarOffice.ComponentContext"
41 filterName
= "HTML (StarWriter)"
48 ctxLocal
= uno
.getComponentContext()
49 smgrLocal
= ctxLocal
.ServiceManager
51 resolver
= smgrLocal
.createInstanceWithContext(
52 "com.sun.star.bridge.UnoUrlResolver", ctxLocal
)
53 ctx
= resolver
.resolve( url
)
54 smgr
= ctx
.ServiceManager
56 desktop
= smgr
.createInstanceWithContext("com.sun.star.frame.Desktop", ctx
)
58 cwd
= systemPathToFileUrl( getcwd() )
60 PropertyValue( "FilterName" , 0, filterName
, 0 ),
61 PropertyValue( "OutputStream",0, OutputStream(),0))
62 inProps
= PropertyValue( "Hidden" , 0 , True, 0 ),
65 fileUrl
= uno
.absolutize( cwd
, systemPathToFileUrl(path
) )
66 doc
= desktop
.loadComponentFromURL( fileUrl
, "_blank", 0,inProps
)
69 raise UnoException( "Couldn't open stream for unknown reason", None )
71 doc
.storeToURL("private:stream",outProps
)
72 except IOException
, e
:
73 sys
.stderr
.write( "Error during conversion: " + e
.Message
+ "\n" )
75 except UnoException
, e
:
76 sys
.stderr
.write( "Error ("+repr(e
.__class
__)+") during conversion:" + e
.Message
+ "\n" )
81 except UnoException
, e
:
82 sys
.stderr
.write( "Error ("+repr(e
.__class
__)+") :" + e
.Message
+ "\n" )
84 except getopt
.GetoptError
,e
:
85 sys
.stderr
.write( str(e
) + "\n" )
92 sys
.stderr
.write( "usage: ooextract.py --help |\n"+
93 " [-c <connection-string> | --connection-string=<connection-string>\n"+
96 "Extracts plain text from documents and prints it to stdout.\n" +
97 "Requires an OpenOffice.org instance to be running. The script and the\n"+
98 "running OpenOffice.org instance must be able to access the file with\n"+
99 "by the same system path.\n"
101 "-c <connection-string> | --connection-string=<connection-string>\n" +
102 " The connection-string part of a uno url to where the\n" +
103 " the script should connect to in order to do the conversion.\n" +
104 " The strings defaults to socket,host=localhost,port=2002\n"
106 " Instead of the text filter, the writer html filter is used\n"