| 1 |
#!/usr/bin/python |
|---|
| 2 |
# Copyright (c) 2004-2005 Nuxeo SARL <http://nuxeo.com> |
|---|
| 3 |
# Authors: |
|---|
| 4 |
# Laurent Godard <lgodard@indesko.com> |
|---|
| 5 |
# M.-A. Darche (Nuxeo) |
|---|
| 6 |
# |
|---|
| 7 |
# This program is free software; you can redistribute it and/or modify |
|---|
| 8 |
# it under the terms of the GNU General Public License version 2 as published |
|---|
| 9 |
# by the Free Software Foundation. |
|---|
| 10 |
# |
|---|
| 11 |
# This program is distributed in the hope that it will be useful, |
|---|
| 12 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 13 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 14 |
# GNU General Public License for more details. |
|---|
| 15 |
# |
|---|
| 16 |
# You should have received a copy of the GNU General Public License |
|---|
| 17 |
# along with this program; if not, write to the Free Software |
|---|
| 18 |
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
|---|
| 19 |
# 02111-1307, USA. |
|---|
| 20 |
# |
|---|
| 21 |
# See ``COPYING`` for more information |
|---|
| 22 |
# |
|---|
| 23 |
# $Id$ |
|---|
| 24 |
|
|---|
| 25 |
# UNO |
|---|
| 26 |
import uno, unohelper |
|---|
| 27 |
from com.sun.star.beans import PropertyValue |
|---|
| 28 |
from com.sun.star.connection import NoConnectException |
|---|
| 29 |
|
|---|
| 30 |
import sys |
|---|
| 31 |
import getopt |
|---|
| 32 |
import os.path |
|---|
| 33 |
from os import sep |
|---|
| 34 |
from os import remove |
|---|
| 35 |
import time |
|---|
| 36 |
|
|---|
| 37 |
VERSION = '0.6' |
|---|
| 38 |
|
|---|
| 39 |
#filter parameters |
|---|
| 40 |
FILTER_PARAMS = { |
|---|
| 41 |
'png': ('.png', 'image/png'), |
|---|
| 42 |
'svg': ('.svg', 'image/svg+xml'), |
|---|
| 43 |
'jpg': ('.jpg', 'image/jpeg'), |
|---|
| 44 |
'tiff': ('.tiff', 'image/tiff'), |
|---|
| 45 |
'eps': ('.eps', 'application/postscript'), |
|---|
| 46 |
'bmp-ms': ('.bmp', 'image/x-MS-bmp'), |
|---|
| 47 |
'bmp-portable': ('.bmp', 'image/x-portable-bitmap'), |
|---|
| 48 |
'gif': ('.gif', 'image/gif'), |
|---|
| 49 |
} |
|---|
| 50 |
|
|---|
| 51 |
|
|---|
| 52 |
def usage(): |
|---|
| 53 |
lst_formats = str(FILTER_PARAMS.keys()) |
|---|
| 54 |
print "usage: ole2img [options] openoffice.org-file" |
|---|
| 55 |
print |
|---|
| 56 |
print "options:" |
|---|
| 57 |
print " --target target directory (mandatory)" |
|---|
| 58 |
print " --oooserverhost the name of server running OpenOffice.org (defaults to localhost)" |
|---|
| 59 |
print " --oooserverport the port of the server running OpenOffice.org (defaults to 2002)" |
|---|
| 60 |
print " --format exported image format (defaults to png)" |
|---|
| 61 |
print " %s" % lst_formats.replace("'","") |
|---|
| 62 |
print " --version show version and exit" |
|---|
| 63 |
print " --help show this help message and exit" |
|---|
| 64 |
print |
|---|
| 65 |
print "Example:" |
|---|
| 66 |
print "~/OpenOffice.org1.1.2/program/python ole2img --format png --target ~/outputdir/ ~/exampleFile.sxw" |
|---|
| 67 |
print |
|---|
| 68 |
print "OpenOffice.org must run in listen mode" |
|---|
| 69 |
print './soffice "-accept=socket,host=localhost,port=2002;urp;"' |
|---|
| 70 |
|
|---|
| 71 |
|
|---|
| 72 |
def version(): |
|---|
| 73 |
print "Converts all OLE objects of a .sxw OpenOffice.org document as images in a target directory" |
|---|
| 74 |
print "ole2img version: " + VERSION |
|---|
| 75 |
|
|---|
| 76 |
|
|---|
| 77 |
def exec_args(): |
|---|
| 78 |
"""Analyze command line arguments. |
|---|
| 79 |
""" |
|---|
| 80 |
source = None |
|---|
| 81 |
target = None |
|---|
| 82 |
# Default server having running OpenOffice.org instance(s) is localhost |
|---|
| 83 |
oooserver_host = 'localhost' |
|---|
| 84 |
oooserver_port = '2002' |
|---|
| 85 |
# Default format is PNG |
|---|
| 86 |
format = 'png' |
|---|
| 87 |
try: |
|---|
| 88 |
opts, args = getopt.getopt(sys.argv[1:], '', |
|---|
| 89 |
['target=', |
|---|
| 90 |
'oooserverhost=', |
|---|
| 91 |
'oooserverport=', |
|---|
| 92 |
'format=', |
|---|
| 93 |
'version', 'help']) |
|---|
| 94 |
except: |
|---|
| 95 |
usage() |
|---|
| 96 |
# Command line syntax errors return the error code "2" |
|---|
| 97 |
sys.exit(2) |
|---|
| 98 |
|
|---|
| 99 |
if len(args) == 1: |
|---|
| 100 |
source = args[0] |
|---|
| 101 |
|
|---|
| 102 |
for opt in opts: |
|---|
| 103 |
if opt[0] == '--target': |
|---|
| 104 |
target = opt[1] |
|---|
| 105 |
elif opt[0] == '--oooserverhost': |
|---|
| 106 |
oooserver_host = opt[1] |
|---|
| 107 |
elif opt[0] == '--oooserverport': |
|---|
| 108 |
oooserver_port = opt[1] |
|---|
| 109 |
elif opt[0] == '--format': |
|---|
| 110 |
format = opt[1] |
|---|
| 111 |
elif opt[0] == '--version': |
|---|
| 112 |
version() |
|---|
| 113 |
sys.exit(0) |
|---|
| 114 |
elif opt[0] == '--help': |
|---|
| 115 |
usage() |
|---|
| 116 |
sys.exit(0) |
|---|
| 117 |
error = False |
|---|
| 118 |
|
|---|
| 119 |
if source == None: |
|---|
| 120 |
error = True |
|---|
| 121 |
print "\nOne and only one OpenOffice.org file must be given" |
|---|
| 122 |
else: |
|---|
| 123 |
source = os.path.abspath(os.path.expanduser(source)) |
|---|
| 124 |
if not os.path.isfile(source): |
|---|
| 125 |
error = True |
|---|
| 126 |
print "\n--source Invalid file" |
|---|
| 127 |
|
|---|
| 128 |
if target == None: |
|---|
| 129 |
error = True |
|---|
| 130 |
print "\n--target Missing Parameter" |
|---|
| 131 |
else: |
|---|
| 132 |
target = os.path.abspath(os.path.expanduser(target)) |
|---|
| 133 |
if not os.path.isdir(target): |
|---|
| 134 |
error = True |
|---|
| 135 |
print "\n--target Invalid directory" |
|---|
| 136 |
|
|---|
| 137 |
if not FILTER_PARAMS.has_key(format): |
|---|
| 138 |
error = True |
|---|
| 139 |
print "\n--format Undefined value" |
|---|
| 140 |
|
|---|
| 141 |
if error: |
|---|
| 142 |
usage() |
|---|
| 143 |
# Command line syntax errors return the error code "2" |
|---|
| 144 |
sys.exit(2) |
|---|
| 145 |
|
|---|
| 146 |
#add Os separator if missing to target |
|---|
| 147 |
if target[len(target) - 1] != os.sep: |
|---|
| 148 |
target += os.sep |
|---|
| 149 |
|
|---|
| 150 |
return source, target, oooserver_host, oooserver_port, format |
|---|
| 151 |
|
|---|
| 152 |
|
|---|
| 153 |
def ole2img(source, target, |
|---|
| 154 |
oooserver_host='localhost', oooserver_port='2002', format='png'): |
|---|
| 155 |
#fileter parameters |
|---|
| 156 |
extension, theFilter = FILTER_PARAMS[format] |
|---|
| 157 |
|
|---|
| 158 |
#Connect to OOo |
|---|
| 159 |
|
|---|
| 160 |
# get the uno component context from the PyUNO runtime |
|---|
| 161 |
localContext = uno.getComponentContext() |
|---|
| 162 |
# create the UnoUrlResolver |
|---|
| 163 |
resolver = localContext.ServiceManager.createInstanceWithContext( |
|---|
| 164 |
'com.sun.star.bridge.UnoUrlResolver', localContext ) |
|---|
| 165 |
|
|---|
| 166 |
# connect to the running office |
|---|
| 167 |
try: |
|---|
| 168 |
print "Connecting to server %s:%s ..." % (oooserver_host, oooserver_port) |
|---|
| 169 |
ctx = resolver.resolve( |
|---|
| 170 |
'uno:socket,host=%s,port=%s;urp;StarOffice.ComponentContext' |
|---|
| 171 |
% (oooserver_host, oooserver_port)) |
|---|
| 172 |
except NoConnectException: |
|---|
| 173 |
print "Unable to connect to OpenOffice.org instance" |
|---|
| 174 |
sys.exit(1) |
|---|
| 175 |
|
|---|
| 176 |
smgr = ctx.ServiceManager |
|---|
| 177 |
|
|---|
| 178 |
# get the central desktop object |
|---|
| 179 |
desktop = smgr.createInstanceWithContext('com.sun.star.frame.Desktop', ctx) |
|---|
| 180 |
|
|---|
| 181 |
# Now connected to OOo |
|---|
| 182 |
|
|---|
| 183 |
# load source file |
|---|
| 184 |
args = (PropertyValue('Hidden', 0, True, 0),) |
|---|
| 185 |
url = unohelper.systemPathToFileUrl(source) |
|---|
| 186 |
sourceDoc = desktop.loadComponentFromURL(url, '_blank', 0, args) |
|---|
| 187 |
|
|---|
| 188 |
# Hack/Bug : Needed to sanitize the object hierarchy for use with CurrentController |
|---|
| 189 |
url = unohelper.systemPathToFileUrl(target + 'temp.sxw') |
|---|
| 190 |
sourceDoc.storeToURL(url,()) |
|---|
| 191 |
os.remove(target + 'temp.sxw') |
|---|
| 192 |
|
|---|
| 193 |
oGraphic=smgr.createInstanceWithContext( |
|---|
| 194 |
'com.sun.star.drawing.GraphicExportFilter', ctx) |
|---|
| 195 |
|
|---|
| 196 |
#dispatcher for copy-paste |
|---|
| 197 |
dispatcher = smgr.createInstanceWithContext( |
|---|
| 198 |
'com.sun.star.frame.DispatchHelper', ctx) |
|---|
| 199 |
|
|---|
| 200 |
#creates an hidden draw docuement |
|---|
| 201 |
args = (PropertyValue('Hidden', 0, True, 0),) |
|---|
| 202 |
url = 'private:factory/sdraw' |
|---|
| 203 |
drawDoc = desktop.loadComponentFromURL(url, '_blank', 0, args) |
|---|
| 204 |
|
|---|
| 205 |
theOleObjects = sourceDoc.EmbeddedObjects |
|---|
| 206 |
|
|---|
| 207 |
for i in range(theOleObjects.Count): |
|---|
| 208 |
oleObject = theOleObjects.getByIndex(i) |
|---|
| 209 |
print oleObject.Name |
|---|
| 210 |
# Selection |
|---|
| 211 |
sourceDoc.CurrentController.select(oleObject) |
|---|
| 212 |
|
|---|
| 213 |
# XXX : OOo |
|---|
| 214 |
# strange random bug on importing documents from previous OOo version |
|---|
| 215 |
# sometimes the current object may not be selected |
|---|
| 216 |
# the selection remains on the previous in the loop |
|---|
| 217 |
|
|---|
| 218 |
# May be not needed any more as done previously ? |
|---|
| 219 |
|
|---|
| 220 |
# Verify the selection is ok |
|---|
| 221 |
obj = sourceDoc.CurrentController.getSelection() |
|---|
| 222 |
if obj.Name != oleObject.Name: |
|---|
| 223 |
# Save the file and select again the object |
|---|
| 224 |
# Updates the object and the can be selected |
|---|
| 225 |
url = unohelper.systemPathToFileUrl(target + 'temp.sxw') |
|---|
| 226 |
sourceDoc.storeToURL(url,()) |
|---|
| 227 |
os.remove(target + 'temp.sxw') |
|---|
| 228 |
sourceDoc.CurrentController.select(theOleObjects.getByIndex(i)) |
|---|
| 229 |
obj = sourceDoc.CurrentController.Selection |
|---|
| 230 |
|
|---|
| 231 |
# if the previous did not work |
|---|
| 232 |
# creates an image telling a problem occured |
|---|
| 233 |
if obj.Name != oleObject.Name: |
|---|
| 234 |
print "unable to process " + oleObject.Name |
|---|
| 235 |
drawingShape = drawDoc.createInstance("com.sun.star.drawing.TextShape") |
|---|
| 236 |
theSapeSize = drawingShape.Size |
|---|
| 237 |
drawDoc.DrawPages.getByIndex(0).add(drawingShape) |
|---|
| 238 |
theSapeSize.Width = 5000 |
|---|
| 239 |
theSapeSize.Height = 2500 |
|---|
| 240 |
drawingShape.setSize(theSapeSize) |
|---|
| 241 |
drawingShape.String = oleObject.Name + " - Unable to process OLE object" |
|---|
| 242 |
drawingShape.CharColor = 255 |
|---|
| 243 |
drawingShape.CharHeight = 8 |
|---|
| 244 |
objDraw = drawingShape |
|---|
| 245 |
else: |
|---|
| 246 |
# Normal behaviour |
|---|
| 247 |
# Copy |
|---|
| 248 |
dispatcher.executeDispatch(sourceDoc.CurrentController.Frame, |
|---|
| 249 |
'.uno:Copy', |
|---|
| 250 |
'', |
|---|
| 251 |
0, |
|---|
| 252 |
()) |
|---|
| 253 |
# Paste into draw |
|---|
| 254 |
dispatcher.executeDispatch(drawDoc.CurrentController.Frame, |
|---|
| 255 |
'.uno:Paste', |
|---|
| 256 |
'', |
|---|
| 257 |
0, |
|---|
| 258 |
()) |
|---|
| 259 |
|
|---|
| 260 |
# get draw object |
|---|
| 261 |
objDraw = drawDoc.CurrentController.Selection |
|---|
| 262 |
|
|---|
| 263 |
# Export |
|---|
| 264 |
oGraphic.setSourceDocument(objDraw) |
|---|
| 265 |
url = unohelper.systemPathToFileUrl(target + oleObject.Name + extension) |
|---|
| 266 |
argsExport = (PropertyValue('URL' , 0 , url, 0 ), |
|---|
| 267 |
PropertyValue('MediaType' , 0 , theFilter, 0 )) |
|---|
| 268 |
oGraphic.filter(argsExport) |
|---|
| 269 |
|
|---|
| 270 |
# Close files |
|---|
| 271 |
drawDoc.close(True) |
|---|
| 272 |
sourceDoc.close(True) |
|---|
| 273 |
|
|---|
| 274 |
|
|---|
| 275 |
# Shell access |
|---|
| 276 |
if __name__ == "__main__": |
|---|
| 277 |
startTime = time.time() |
|---|
| 278 |
source, target, oooserver_host, oooserver_port, format = exec_args() |
|---|
| 279 |
print "Exporting into %s format to target %s..." % (format, target) |
|---|
| 280 |
ole2img(source, target, oooserver_host, oooserver_port, format) |
|---|
| 281 |
endTime = time.time() |
|---|
| 282 |
duration = round(endTime - startTime, 2) |
|---|
| 283 |
print 'duration : %s sec' % duration |
|---|