forked from unoconv/unoconv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
unoconv
executable file
·482 lines (418 loc) · 22.7 KB
/
unoconv
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
#!/usr/bin/python
### This program is free software; you can redistribute it and/or modify
### it under the terms of the GNU Library General Public License as published by
### the Free Software Foundation; version 2 only
###
### This program is distributed in the hope that it will be useful,
### but WITHOUT ANY WARRANTY; without even the implied warranty of
### MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
### GNU Library General Public License for more details.
###
### You should have received a copy of the GNU Library General Public License
### along with this program; if not, write to the Free Software
### Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
### Copyright 2007 Dag Wieers <dag@wieers.com>
import getopt, sys, os, glob
try:
import uno
except:
# extrapaths = ('/usr/lib/openoffice/program/', '/usr/lib/openoffice.org2.0/program/')
extrapaths = glob.glob('/usr/lib*/openoffice*/program/') + [ '/Applications/NeoOffice.app/Contents/program', ]
for path in extrapaths:
if os.path.isfile(os.path.join(path, 'pyuno.so')):
sys.path.append(path)
break
else:
print >>sys.stderr, "unoconv: Cannot find the pyuno.so library in sys.path."
print >>sys.stderr, "Please locate this library and send feedback to <tools@lists.rpmforge.net>."
sys.exit(1)
import uno, unohelper
from com.sun.star.beans import PropertyValue
from com.sun.star.connection import NoConnectException
from com.sun.star.io import IOException, XOutputStream
from com.sun.star.script import CannotConvertException
from com.sun.star.uno import Exception as UnoException
__version__ = "$Revision$"
# $Source$
VERSION = '0.2svn'
doctypes = ('document', 'graphics', 'presentation', 'spreadsheet')
class Fmt:
def __init__(self, doctype, name, extension, summary, filter):
self.doctype = doctype
self.name = name
self.extension = extension
self.summary = summary
self.filter = filter
def __str__(self):
return "%s [.%s]" % (self.summary, self.extension)
def __repr__(self):
return "%s/%s" % (self.name, self.doctype)
class FmtList:
def __init__(self):
self.list = []
def add(self, doctype, name, extension, summary, filter):
self.list.append(Fmt(doctype, name, extension, summary, filter))
def byname(self, name):
ret = []
for fmt in self.list:
if fmt.name == name:
ret.append(fmt)
return ret
def byextension(self, extension):
ret = []
for fmt in self.list:
if '.'+fmt.extension == extension:
ret.append(fmt)
return ret
def bydoctype(self, doctype, name):
ret = []
for fmt in self.list:
if fmt.name == name and fmt.doctype == doctype:
ret.append(fmt)
return ret
def display(self, doctype):
print >>sys.stderr, 'The following list of %s formats are currently available:\n' % doctype
for fmt in self.list:
if fmt.doctype == doctype:
print >>sys.stderr, " %-8s - %s" % (fmt.name, fmt)
print >>sys.stderr
class OutputStream( unohelper.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
fmts = FmtList()
### Document / Writer
fmts.add('document', 'bib', 'bib', 'BibTeX', 'BibTeX_Writer')
fmts.add('document', 'doc', 'doc', 'Microsoft Word 97/2000/XP', 'MS Word 97')
fmts.add('document', 'doc6', 'doc', 'Microsoft Word 6.0', 'MS WinWord 6.0')
fmts.add('document', 'doc95', 'doc', 'Microsoft Word 95', 'MS Word 95')
fmts.add('document', 'docbook', 'xml', 'DocBook', 'DocBook File')
fmts.add('document', 'html', 'html', 'HTML Document (OpenOffice.org Writer)', 'HTML (StarWriter)')
fmts.add('document', 'odt', 'odt', 'Open Document Text', 'writer8')
fmts.add('document', 'ott', 'ott', 'Open Document Text', 'writer8_template')
fmts.add('document', 'ooxml', 'xml', 'Microsoft Office Open XML', 'MS Word 2003 XML')
fmts.add('document', 'pdb', 'pdb', 'AportisDoc (Palm)', 'AportisDoc Palm DB')
fmts.add('document', 'pdf', 'pdf', 'Portable Document Format', 'writer_pdf_Export')
fmts.add('document', 'psw', 'psw', 'Pocket Word', 'PocketWord File')
fmts.add('document', 'rtf', 'rtf', 'Rich Text Format', 'Rich Text Format')
fmts.add('document', 'latex', 'ltx', 'LaTeX 2e', 'LaTeX_Writer')
fmts.add('document', 'sdw', 'sdw', 'StarWriter 5.0', 'StarWriter 5.0')
fmts.add('document', 'sdw4', 'sdw', 'StarWriter 4.0', 'StarWriter 4.0')
fmts.add('document', 'sdw3', 'sdw', 'StarWriter 3.0', 'StarWriter 3.0')
fmts.add('document', 'stw', 'stw', 'Open Office.org 1.0 Text Document Template', 'writer_StarOffice_XML_Writer_Template')
fmts.add('document', 'sxw', 'sxw', 'Open Office.org 1.0 Text Document', 'StarOffice XML (Writer)')
fmts.add('document', 'text', 'txt', 'Text Encoded', 'Text (encoded)')
fmts.add('document', 'txt', 'txt', 'Plain Text', 'Text')
fmts.add('document', 'vor', 'vor', 'StarWriter 5.0 Template', 'StarWriter 5.0 Vorlage/Template')
fmts.add('document', 'vor4', 'vor', 'StarWriter 4.0 Template', 'StarWriter 4.0 Vorlage/Template')
fmts.add('document', 'vor3', 'vor', 'StarWriter 3.0 Template', 'StarWriter 3.0 Vorlage/Template')
fmts.add('document', 'xhtml', 'html', 'XHTML Document', 'XHTML Writer File')
### Spreadsheet
fmts.add('spreadsheet', 'csv', 'csv', 'Text CSV', 'Text - txt - csv (StarCalc)')
fmts.add('spreadsheet', 'dbf', 'dbf', 'dBase', 'dBase')
fmts.add('spreadsheet', 'dif', 'dif', 'Data Interchange Format', 'DIF')
fmts.add('spreadsheet', 'html', 'html', 'HTML Document (OpenOffice.org Calc)', 'HTML (StarCalc)')
fmts.add('spreadsheet', 'ods', 'ods', 'Open Document Spreadsheet', 'calc8')
fmts.add('spreadsheet', 'ooxml', 'xml', 'Microsoft Excel 2003 XML', 'MS Excel 2003 XML')
fmts.add('spreadsheet', 'pdf', 'pdf', 'Portable Document Format', 'calc_pdf_Export')
fmts.add('spreadsheet', 'pts', 'pts', 'OpenDocument Spreadsheet Template', 'calc8_template')
fmts.add('spreadsheet', 'pxl', 'pxl', 'Pocket Excel', 'Pocket Excel')
fmts.add('spreadsheet', 'sdc', 'sdc', 'StarCalc 5.0', 'StarCalc 5.0')
fmts.add('spreadsheet', 'sdc4', 'sdc', 'StarCalc 4.0', 'StarCalc 4.0')
fmts.add('spreadsheet', 'sdc3', 'sdc', 'StarCalc 3.0', 'StarCalc 3.0')
fmts.add('spreadsheet', 'slk', 'slk', 'SYLK', 'SYLK')
fmts.add('spreadsheet', 'stc', 'stc', 'OpenOffice.org 1.0 Spreadsheet Template', 'calc_StarOffice_XML_Calc_Template')
fmts.add('spreadsheet', 'sxc', 'sxc', 'OpenOffice.org 1.0 Spreadsheet', 'StarOffice XML (Calc)')
fmts.add('spreadsheet', 'vor3', 'vor', 'StarCalc 3.0 Template', 'StarCalc 3.0 Vorlage/Template')
fmts.add('spreadsheet', 'vor4', 'vor', 'StarCalc 4.0 Template', 'StarCalc 4.0 Vorlage/Template')
fmts.add('spreadsheet', 'vor', 'vor', 'StarCalc 5.0 Template', 'StarCalc 5.0 Vorlage/Template')
fmts.add('spreadsheet', 'xhtml', 'xhtml', 'XHTML', 'XHTML Calc File')
fmts.add('spreadsheet', 'xls', 'xls', 'Microsoft Excel 97/2000/XP', 'MS Excel 97')
fmts.add('spreadsheet', 'xls5', 'xls', 'Microsoft Excel 5.0', 'MS Excel 5.0/95')
fmts.add('spreadsheet', 'xls95', 'xls', 'Microsoft Excel 95', 'MS Excel 95')
fmts.add('spreadsheet', 'xlt', 'xlt', 'Microsoft Excel 97/2000/XP Template', 'MS Excel 97 Vorlage/Template')
fmts.add('spreadsheet', 'xlt5', 'xlt', 'Microsoft Excel 5.0 Template', 'MS Excel 5.0/95 Vorlage/Template')
fmts.add('spreadsheet', 'xlt95', 'xlt', 'Microsoft Excel 95 Template', 'MS Excel 95 Vorlage/Template')
### Graphics
fmts.add('graphics', 'bmp', 'bmp', 'Windows Bitmap', 'draw_bmp_Export')
fmts.add('graphics', 'emf', 'emf', 'Enhanced Metafile', 'draw_emf_Export')
fmts.add('graphics', 'eps', 'eps', 'Encapsulated PostScript', 'draw_eps_Export')
fmts.add('graphics', 'gif', 'gif', 'Graphics Interchange Format', 'draw_gif_Export')
fmts.add('graphics', 'html', 'html', 'HTML Document (OpenOffice.org Draw)', 'draw_html_Export')
fmts.add('graphics', 'jpg', 'jpg', 'Joint Photographic Experts Group', 'draw_jpg_Export')
fmts.add('graphics', 'met', 'met', 'OS/2 Metafile', 'draw_met_Export')
fmts.add('graphics', 'odd', 'odd', 'OpenDocument Drawing', 'draw8')
fmts.add('graphics', 'otg', 'otg', 'OpenDocument Drawing Template', 'draw8_template')
fmts.add('graphics', 'pbm', 'pbm', 'Portable Bitmap', 'draw_pbm_Export')
fmts.add('graphics', 'pct', 'pct', 'Mac Pict', 'draw_pct_Export')
fmts.add('graphics', 'pdf', 'pdf', 'Portable Document Format', 'draw_pdf_Export')
fmts.add('graphics', 'pgm', 'pgm', 'Portable Graymap', 'draw_pgm_Export')
fmts.add('graphics', 'png', 'png', 'Portable Network Graphic', 'draw_png_Export')
fmts.add('graphics', 'ppm', 'ppm', 'Portable Pixelmap', 'draw_ppm_Export')
fmts.add('graphics', 'ras', 'ras', 'Sun Raster Image', 'draw_ras_Export')
fmts.add('graphics', 'std', 'std', 'OpenOffice.org 1.0 Drawing Template', 'draw_StarOffice_XML_Draw_Template')
fmts.add('graphics', 'svg', 'svg', 'Scalable Vector Graphics', 'draw_svg_Export')
fmts.add('graphics', 'svm', 'svm', 'StarView Metafile', 'draw_svm_Export')
fmts.add('graphics', 'swf', 'swf', 'Macromedia Flash (SWF)', 'draw_flash_Export')
fmts.add('graphics', 'sxd', 'sxd', 'OpenOffice.org 1.0 Drawing', 'StarOffice XML (Draw)')
fmts.add('graphics', 'sxd3', 'sxd', 'StarDraw 3.0', 'StarDraw 3.0')
fmts.add('graphics', 'sxd5', 'sxd', 'StarDraw 5.0', 'StarDraw 5.0')
fmts.add('graphics', 'tiff', 'tiff', 'Tagged Image File Format', 'draw_tif_Export')
fmts.add('graphics', 'vor', 'vor', 'StarDraw 5.0 Template', 'StarDraw 5.0 Vorlage')
fmts.add('graphics', 'vor3', 'vor', 'StarDraw 3.0 Template', 'StarDraw 3.0 Vorlage')
fmts.add('graphics', 'wmf', 'wmf', 'Windows Metafile', 'draw_wmf_Export')
fmts.add('graphics', 'xhtml', 'xhtml', 'XHTML', 'XHTML Draw File')
fmts.add('graphics', 'xpm', 'xpm', 'X PixMap', 'draw_xpm_Export')
### Presentation
fmts.add('presentation', 'bmp', 'bmp', 'Windows Bitmap', 'impress_bmp_Export')
fmts.add('presentation', 'emf', 'emf', 'Enhanced Metafile', 'impress_emf_Export')
fmts.add('presentation', 'eps', 'eps', 'Encapsulated PostScript', 'impress_eps_Export')
fmts.add('presentation', 'gif', 'gif', 'Graphics Interchange Format', 'impress_gif_Export')
fmts.add('presentation', 'html', 'html', 'HTML Document (OpenOffice.org Impress)', 'impress_html_Export')
fmts.add('presentation', 'jpg', 'jpg', 'Joint Photographic Experts Group', 'impress_jpg_Export')
fmts.add('presentation', 'met', 'met', 'OS/2 Metafile', 'impress_met_Export')
fmts.add('presentation', 'odd', 'odd', 'OpenDocument Drawing (Impress)', 'impress8_draw')
fmts.add('presentation', 'odg', 'odg', 'OpenOffice.org 1.0 Drawing (OpenOffice.org Impress)', 'impress_StarOffice_XML_Draw')
fmts.add('presentation', 'odp', 'odp', 'OpenDocument Presentation', 'impress8')
fmts.add('presentation', 'pbm', 'pbm', 'Portable Bitmap', 'impress_pbm_Export')
fmts.add('presentation', 'pct', 'pct', 'Mac Pict', 'impress_pct_Export')
fmts.add('presentation', 'pdf', 'pdf', 'Portable Document Format', 'impress_pdf_Export')
fmts.add('presentation', 'pgm', 'pgm', 'Portable Graymap', 'impress_pgm_Export')
fmts.add('presentation', 'png', 'png', 'Portable Network Graphic', 'impress_png_Export')
fmts.add('presentation', 'pot', 'pot', 'Microsoft PowerPoint 97/2000/XP Template', 'MS PowerPoint 97 Vorlage')
fmts.add('presentation', 'ppm', 'ppm', 'Portable Pixelmap', 'impress_ppm_Export')
fmts.add('presentation', 'ppt', 'ppt', 'Microsoft PowerPoint 97/2000/XP', 'MS PowerPoint 97')
fmts.add('presentation', 'pwp', 'pwp', 'PlaceWare', 'placeware_Export')
fmts.add('presentation', 'ras', 'ras', 'Sun Raster Image', 'impress_ras_Export')
fmts.add('presentation', 'sda', 'sda', 'StarDraw 5.0 (OpenOffice.org Impress)', 'StarDraw 5.0 (StarImpress)')
fmts.add('presentation', 'sdd', 'sdd', 'StarImpress 5.0', 'StarImpress 5.0')
fmts.add('presentation', 'sdd3', 'sdd', 'StarDraw 3.0 (OpenOffice.org Impress)', 'StarDraw 3.0 (StarImpress)')
fmts.add('presentation', 'sdd4', 'sdd', 'StarImpress 4.0', 'StarImpress 4.0')
fmts.add('presentation', 'sti', 'sti', 'OpenOffice.org 1.0 Presentation Template', 'impress_StarOffice_XML_Impress_Template')
fmts.add('presentation', 'stp', 'stp', 'OpenDocument Presentation Template', 'impress8_template')
fmts.add('presentation', 'svg', 'svg', 'Scalable Vector Graphics', 'impress_svg_Export')
fmts.add('presentation', 'svm', 'svm', 'StarView Metafile', 'impress_svm_Export')
fmts.add('presentation', 'swf', 'swf', 'Macromedia Flash (SWF)', 'impress_flash_Export')
fmts.add('presentation', 'sxi', 'sxi', 'OpenOffice.org 1.0 Presentation', 'StarOffice XML (Impress)')
fmts.add('presentation', 'tiff', 'tiff', 'Tagged Image File Format', 'impress_tif_Export')
fmts.add('presentation', 'vor', 'vor', 'StarImpress 5.0 Template', 'StarImpress 5.0 Vorlage')
fmts.add('presentation', 'vor3', 'vor', 'StarDraw 3.0 Template (OpenOffice.org Impress)', 'StarDraw 3.0 Vorlage (StarImpress)')
fmts.add('presentation', 'vor4', 'vor', 'StarImpress 4.0 Template', 'StarImpress 4.0 Vorlage')
fmts.add('presentation', 'vor5', 'vor', 'StarDraw 5.0 Template (OpenOffice.org Impress)', 'StarDraw 5.0 Vorlage (StarImpress)')
fmts.add('presentation', 'wmf', 'wmf', 'Windows Metafile', 'impress_wmf_Export')
fmts.add('presentation', 'xhtml', 'xml', 'XHTML', 'XHTML Impress File')
fmts.add('presentation', 'xpm', 'xpm', 'X PixMap', 'impress_xpm_Export')
class Options:
def __init__(self, args):
self.stdout = False
self.showlist = False
self.format = None
self.verbose = 0
self.doctype = None
self.server = 'localhost'
self.port = '2002'
self.connection = "socket,host=%s,port=%s;urp;StarOffice.ComponentContext" % (self.server, self.port)
self.filenames = []
### Get options from the commandline
try:
opts, args = getopt.getopt (args, 'b:c:d:hlp:s:t:v',
['backend=', 'connection=', 'doctype=', 'help', 'list', 'port=', 'server=', 'stdout', 'verbose', 'version'] )
except getopt.error, exc:
print >>sys.stderr, 'unoconv: %s, try unoconv -h for a list of all the options' % str(exc)
sys.exit(255)
for opt, arg in opts:
if opt in ['-h', '--help']:
self.usage()
print
self.help()
sys.exit(1)
elif opt in ['-b', '--backend']:
self.format = arg
elif opt in ['-c', '--connection']:
self.connection = arg
elif opt in ['-d', '--doctype']:
self.doctype = arg
elif opt in ['-l', '--list']:
self.showlist = True
elif opt in ['-p', '--port']:
self.port = arg
elif opt in ['-s', '--server']:
self.server = arg
elif opt in ['--stdout']:
self.stdout = True
elif opt in ['-v', '--verbose']:
self.verbose = self.verbose + 1
elif opt in ['--version']:
self.version()
sys.exit(255)
### Enable verbosity
if self.verbose >= 3:
print >>sys.stderr, 'Verbosity set to level %d' % (self.verbose - 1)
self.filenames = args
if not self.showlist and self.doctype != 'list' and not self.filenames:
print >>sys.stderr, 'unoconv: you have to provide a filename as argument'
print >>sys.stderr, 'Try `unoconv -h\' for more information.'
sys.exit(255)
### Make it easier for people to use a doctype (first letter is enough)
if self.doctype:
for doctype in doctypes:
if self.doctype[0] == doctype[0]:
self.doctype = doctype
### Check if the user request to see the list of formats
if self.showlist or self.format == 'list':
if self.doctype:
fmts.display(self.doctype)
else:
for t in doctypes:
fmts.display(t)
sys.exit(0)
### if no format was specified, probe it or provide it
if not self.format:
l = sys.argv[0].split('2')
if len(l) == 2:
self.format = l[1]
else:
self.format = 'pdf'
def version(self):
print 'unoconv %s' % VERSION
print 'Written by Dag Wieers <dag@wieers.com>'
print 'Homepage at http://dag.wieers.com/home-made/unoconv/'
print
print 'platform %s/%s' % (os.name, sys.platform)
print 'python %s' % sys.version
print
print 'build revision $Rev$'
def usage(self):
print >>sys.stderr, 'usage: unoconv [options] file [file2 ..]'
def help(self):
print >>sys.stderr, '''Convert from and to any format supported by OpenOffice
unoconv options:
-b, --backend=format specify the output format
-c, --connection=string use a custom connection string
-d, --doctype=type specify document type (document, graphics, presentation, spreadsheet)
-l, --list list the available output formats
-p, --port specify a different port (default: 2002)
-s, --server specify a different server (default: localhost)
--stdout write output to stdout
'''
class Convertor:
def __init__(self):
context = uno.getComponentContext()
resolver = context.ServiceManager.createInstanceWithContext("com.sun.star.bridge.UnoUrlResolver", context)
unocontext = resolver.resolve("uno:%s" % op.connection)
unosvcmgr = unocontext.ServiceManager
self.desktop = unosvcmgr.createInstanceWithContext("com.sun.star.frame.Desktop", unocontext)
self.cwd = unohelper.systemPathToFileUrl( os.getcwd() )
def getformat(self, inputfn):
doctype = None
### Get the output format from mapping
if op.doctype:
outputfmt = fmts.bydoctype(op.doctype, op.format)
else:
outputfmt = fmts.byname(op.format)
if not outputfmt:
outputfmt = fmts.byextension('.'+op.format)
### If no doctype given, check list of acceptable formats for input file ext doctype
### FIXME: This should go into the for-loop to match each individual input filename
if outputfmt:
inputext = os.path.splitext(inputfn)[1]
inputfmt = fmts.byextension(inputext)
if inputfmt:
for fmt in outputfmt:
if inputfmt[0].doctype == fmt.doctype:
doctype = inputfmt[0].doctype
outputfmt = fmt
break
else:
outputfmt = outputfmt[0]
# print >>sys.stderr, 'unoconv: format `%s\' is part of multiple doctypes %s, selecting `%s\'.' % (format, [fmt.doctype for fmt in outputfmt], outputfmt[0].doctype)
else:
outputfmt = outputfmt[0]
### No format found, throw error
if not outputfmt:
if doctype:
print >>sys.stderr, 'unoconv: format [%s/%s] is not known to unoconv.' % (op.format, op.doctype)
else:
print >>sys.stderr, 'unoconv: format [%s] is not known to unoconv.' % op.format
sys.exit(1)
return outputfmt
def convert(self, inputfn):
doc = None
outputfmt = self.getformat(inputfn)
if op.verbose > 0:
print >>sys.stderr, 'Input file:', inputfn
if not os.path.exists(inputfn):
print >>sys.stderr, 'unoconv: file `%s\' does not exist.' % inputfn
exitcode = 1
return
try:
### Load inputfile
inputprops = ( PropertyValue( "Hidden" , 0 , True, 0 ), )
inputurl = unohelper.absolutize(self.cwd, unohelper.systemPathToFileUrl(inputfn))
doc = self.desktop.loadComponentFromURL( inputurl , "_blank", 0, inputprops )
if not doc:
raise UnoException("File could not be loaded by OpenOffice", None)
if op.verbose > 0:
print >>sys.stderr, 'Selected output format: %s' % outputfmt
print >>sys.stderr, 'Selected ooffice filter: %s' % outputfmt.filter
print >>sys.stderr, 'Used doctype:', outputfmt.doctype
### Write outputfile
outputprops = (
PropertyValue( "FilterName" , 0, outputfmt.filter , 0 ),
PropertyValue( "Overwrite" , 0, True , 0 ),
PropertyValue( "OutputStream", 0, OutputStream(), 0),
)
if not op.stdout:
(outputfn, ext) = os.path.splitext(inputfn)
outputfn = outputfn + '.' + outputfmt.extension
outputurl = unohelper.absolutize( self.cwd, unohelper.systemPathToFileUrl(outputfn) )
doc.storeToURL(outputurl, outputprops)
if op.verbose > 0:
print >>sys.stderr, 'Output file:', outputfn
else:
doc.storeToURL("private:stream", outputprops)
except SystemError, e:
print >>sys.stderr, "unoconv: error during conversion: %s" % e
print >>sys.stderr, "The provided document cannot be imported."
exitcode = 1
except UnoException, e:
print >>sys.stderr, "unoconv: error during conversion: %s" % e.Message
print >>sys.stderr, "The provided document cannot be imported."
exitcode = 1
# if doc:
# doc.dispose()
except IOException, e:
print >>sys.stderr, "unoconv: error during conversion: %s" % e.Message
print >>sys.stderr, "The provided document cannot be exported to %s." % outputfmt
exitcode = 1
except CannotConvertException, e:
print >>sys.stderr, "unoconv: convert error: %s" % e.Message
exitcode = 1
def main():
try:
convertor = Convertor()
### FIXME: outputfmt should be created inside class
for inputfn in op.filenames:
convertor.convert(inputfn)
except NoConnectException, e:
print >>sys.stderr, "unoconv: could not find an existing connection to Open Office at %s:%s." % (op.server, op.port)
print >>sys.stderr
if op.connection:
print >>sys.stderr, "Please start an ooffice instance on server '%s' by doing:\n\n ooffice -headless -accept=\"%s\"" % (op.server, op.connection)
else:
print >>sys.stderr, "Please start an ooffice instance on server '%s' by doing:\n\n ooffice -headless -accept=\"socket,host=localhost,port=%s;urp;\"" % (op.server, op.port)
exitcode = 1
### Main entrance
if __name__ == '__main__':
exitcode = 0
op = Options(sys.argv[1:])
try:
main()
except KeyboardInterrupt, e:
die(6, 'Exiting on user request')
sys.exit(exitcode)