forked from mirkonasato/pyodconverter
-
Notifications
You must be signed in to change notification settings - Fork 3
/
DocumentConverter.py
449 lines (394 loc) · 14.2 KB
/
DocumentConverter.py
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
#
# PyODConverter (Python OpenDocument Converter) v1.5 - 2013-01-07
#
# This script converts a document from one office format to another by
# connecting to an OpenOffice.org instance via Python-UNO bridge.
#
# Copyright (C) 2008-2013 Mirko Nasato
# Licensed under the GNU LGPL v2.1 - http://www.gnu.org/licenses/lgpl-2.1.html
# - or any later version.
#
DEFAULT_OPENOFFICE_PORT = 2002
import uno
from os.path import abspath, isfile, splitext
from com.sun.star.awt import Size
from com.sun.star.beans import PropertyValue
from com.sun.star.view.PaperFormat import USER
from com.sun.star.view.PaperOrientation import PORTRAIT, LANDSCAPE
from com.sun.star.task import ErrorCodeIOException
from com.sun.star.connection import NoConnectException
FAMILY_TEXT = "Text"
FAMILY_WEB = "Web"
FAMILY_SPREADSHEET = "Spreadsheet"
FAMILY_PRESENTATION = "Presentation"
FAMILY_DRAWING = "Drawing"
#---------------------#
# Configuration Start #
#---------------------#
'''
See http://www.openoffice.org/api/docs/common/ref/com/sun/star/view/PaperFormat.html
'''
PAPER_SIZE_MAP = {
"A5": Size(14800,21000),
"A4": Size(21000,29700),
"A3": Size(29700,42000),
"LETTER": Size(21590,27940),
"LEGAL": Size(21590,35560),
"TABLOID": Size(27900,43200)
}
'''
See http://www.openoffice.org/api/docs/common/ref/com/sun/star/view/PaperOrientation.html
'''
PAPER_ORIENTATION_MAP = {
"PORTRAIT": PORTRAIT,
"LANDSCAPE": LANDSCAPE
}
'''
See http://wiki.services.openoffice.org/wiki/Framework/Article/Filter
most formats are auto-detected; only those requiring options are defined here
'''
IMPORT_FILTER_MAP = {
"txt": {
"FilterName": "Text (encoded)",
"FilterOptions": "utf8"
},
"csv": {
"FilterName": "Text - txt - csv (StarCalc)",
"FilterOptions": "44,34,0"
}
}
'''
The filter options to export PDF files can be viewed on URL below
http://wiki.openoffice.org/wiki/API/Tutorials/PDF_export#General_properties
'''
EXPORT_FILTER_MAP = {
"pdf": {
FAMILY_TEXT: {
"FilterName": "writer_pdf_Export",
"FilterData": {
"IsSkipEmptyPages": True
},
"Overwrite": True
},
FAMILY_WEB: {
"FilterName": "writer_web_pdf_Export",
"FilterData": {
"IsSkipEmptyPages": True
},
"Overwrite": True
},
FAMILY_SPREADSHEET: {
"FilterName": "calc_pdf_Export",
"FilterData": {
"IsSkipEmptyPages": True
},
"Overwrite": True
},
FAMILY_PRESENTATION: {
"FilterName": "impress_pdf_Export",
"FilterData": {
"IsSkipEmptyPages": True
},
"Overwrite": True
},
FAMILY_DRAWING: {
"FilterName": "draw_pdf_Export",
"FilterData": {
"IsSkipEmptyPages": True
},
"Overwrite": True
}
},
"html": {
FAMILY_TEXT: {
"FilterName": "HTML (StarWriter)",
"Overwrite": True
},
FAMILY_SPREADSHEET: {
"FilterName": "HTML (StarCalc)",
"Overwrite": True
},
FAMILY_PRESENTATION: {
"FilterName": "impress_html_Export",
"Overwrite": True
}
},
"odt": {
FAMILY_TEXT: {
"FilterName": "writer8",
"Overwrite": True
},
FAMILY_WEB: {
"FilterName": "writerweb8_writer",
"Overwrite": True
}
},
"doc": {
FAMILY_TEXT: {
"FilterName": "MS Word 97",
"Overwrite": True
}
},
"docx": {
FAMILY_TEXT: {
"FilterName": "MS Word 2007 XML",
"Overwrite": True
}
},
"rtf": {
FAMILY_TEXT: {
"FilterName": "Rich Text Format",
"Overwrite": True
}
},
"txt": {
FAMILY_TEXT: {
"FilterName": "Text",
"FilterOptions": "utf8",
"Overwrite": True
}
},
"ods": {
FAMILY_SPREADSHEET: {
"FilterName": "calc8",
"Overwrite": True
}
},
"xls": {
FAMILY_SPREADSHEET: {
"FilterName": "MS Excel 97",
"Overwrite": True
}
},
"csv": {
FAMILY_SPREADSHEET: {
"FilterName": "Text - txt - csv (StarCalc)",
"FilterOptions": "44,34,0",
"Overwrite": True
}
},
"odp": {
FAMILY_PRESENTATION: {
"FilterName": "impress8",
"Overwrite": True
}
},
"ppt": {
FAMILY_PRESENTATION: {
"FilterName": "MS PowerPoint 97",
"Overwrite": True
}
},
"pptx": {
FAMILY_PRESENTATION: {
"FilterName": "Impress MS PowerPoint 2007 XML",
"Overwrite": True
}
},
"swf": {
FAMILY_DRAWING: {
"FilterName": "draw_flash_Export",
"Overwrite": True
},
FAMILY_PRESENTATION: {
"FilterName": "impress_flash_Export",
"Overwrite": True
}
},
"png": {
FAMILY_PRESENTATION: {
"FilterName": "impress_png_Export",
"Overwrite": True
},
FAMILY_DRAWING: {
"FilterName": "draw_png_Export",
"Overwrite": True
}
},
"gif": {
FAMILY_PRESENTATION: {
"FilterName": "impress_gif_Export",
"Overwrite": True
},
FAMILY_DRAWING: {
"FilterName": "draw_gif_Export",
"Overwrite": True
}
},
"jpg": {
FAMILY_PRESENTATION: {
"FilterName": "impress_jpg_Export",
"Overwrite": True
},
FAMILY_DRAWING: {
"FilterName": "draw_jpg_Export",
"Overwrite": True
}
}
}
PAGE_STYLE_OVERRIDE_PROPERTIES = {
FAMILY_SPREADSHEET: {
#--- Scale options: uncomment 1 of the 3 ---
# a) 'Reduce / enlarge printout': 'Scaling factor'
"PageScale": 100,
# b) 'Fit print range(s) to width / height': 'Width in pages' and 'Height in pages'
#"ScaleToPagesX": 1, "ScaleToPagesY": 1000,
# c) 'Fit print range(s) on number of pages': 'Fit print range(s) on number of pages'
#"ScaleToPages": 1,
"PrintGrid": False
}
}
IMAGES_MEDIA_TYPE = {
"png": "image/png",
"jpeg": "image/jpeg",
"jpg": "image/jpeg",
"gif": "image/gif"
}
#-------------------#
# Configuration End #
#-------------------#
class DocumentConversionException(Exception):
def _get_message(self):
return self._message
def _set_message(self, message):
self._message = message
message = property(_get_message, _set_message)
class DocumentConverter:
def __init__(self, port=DEFAULT_OPENOFFICE_PORT):
localContext = uno.getComponentContext()
resolver = localContext.ServiceManager.createInstanceWithContext("com.sun.star.bridge.UnoUrlResolver", localContext)
try:
self.context = resolver.resolve("uno:socket,host=localhost,port=%s;urp;StarOffice.ComponentContext" % port)
except NoConnectException:
raise DocumentConversionException, "failed to connect to OpenOffice.org on port %s" % port
self.desktop = self.context.ServiceManager.createInstanceWithContext("com.sun.star.frame.Desktop", self.context)
def convert(self, inputFile, outputFile, paperSize, paperOrientation):
if PAPER_SIZE_MAP.has_key(paperSize) is False:
raise Exception("The paper size given doesn't exist.")
else:
paperSize = PAPER_SIZE_MAP[paperSize]
if PAPER_ORIENTATION_MAP.has_key(paperOrientation) is False:
raise Exception("The paper orientation given doesn't exist.")
else:
paperOrientation = PAPER_ORIENTATION_MAP[paperOrientation]
inputUrl = self._toFileUrl(inputFile)
outputUrl = self._toFileUrl(outputFile)
loadProperties = { "Hidden": True }
inputExt = self._getFileExt(inputFile)
outputExt = self._getFileExt(outputFile);
if IMPORT_FILTER_MAP.has_key(inputExt):
loadProperties.update(IMPORT_FILTER_MAP[inputExt])
document = self.desktop.loadComponentFromURL(inputUrl, "_blank", 0, self._toProperties(loadProperties))
try:
document.refresh()
except AttributeError:
pass
family = self._detectFamily(document)
try:
'''
If you wish convert a document to an image, so each page needs be converted to a individual image.
'''
if IMAGES_MEDIA_TYPE.has_key(outputExt):
drawPages = document.getDrawPages()
pagesTotal = drawPages.getCount()
mediaType = IMAGES_MEDIA_TYPE[outputExt]
fileBasename = self._getFileBasename(outputUrl)
graphicExport = self.context.ServiceManager.createInstanceWithContext("com.sun.star.drawing.GraphicExportFilter", self.context)
for pageIndex in xrange(pagesTotal):
page = drawPages.getByIndex(pageIndex)
fileName = "%s-%d.%s" % (self._getFileBasename(outputUrl), pageIndex, outputExt)
graphicExport.setSourceDocument( page )
props = {
"MediaType": mediaType,
"URL": fileName
}
graphicExport.filter( self._toProperties( props ) )
else:
self._overridePageStyleProperties(document, family)
storeProperties = self._getStoreProperties(document, outputExt)
printConfigs = {
'AllSheets': True,
'Size': paperSize,
'PaperFormat': USER,
'PaperOrientation': paperOrientation
}
document.setPrinter( self._toProperties( printConfigs ) )
document.storeToURL(outputUrl, self._toProperties(storeProperties))
finally:
document.close(True)
def _overridePageStyleProperties(self, document, family):
if PAGE_STYLE_OVERRIDE_PROPERTIES.has_key(family):
styleFamilies = document.getStyleFamilies()
if styleFamilies.hasByName('PageStyles'):
properties = PAGE_STYLE_OVERRIDE_PROPERTIES[family]
pageStyles = styleFamilies.getByName('PageStyles')
for styleName in pageStyles.getElementNames():
pageStyle = pageStyles.getByName(styleName)
for name, value in properties.items():
pageStyle.setPropertyValue(name, value)
def _getStoreProperties(self, document, outputExt):
family = self._detectFamily(document)
try:
propertiesByFamily = EXPORT_FILTER_MAP[outputExt]
except KeyError:
raise DocumentConversionException, "unknown output format: '%s'" % outputExt
try:
return propertiesByFamily[family]
except KeyError:
raise DocumentConversionException, "unsupported conversion: from '%s' to '%s'" % (family, outputExt)
def _detectFamily(self, document):
if document.supportsService("com.sun.star.text.WebDocument"):
return FAMILY_WEB
if document.supportsService("com.sun.star.text.GenericTextDocument"):
# must be TextDocument or GlobalDocument
return FAMILY_TEXT
if document.supportsService("com.sun.star.sheet.SpreadsheetDocument"):
return FAMILY_SPREADSHEET
if document.supportsService("com.sun.star.presentation.PresentationDocument"):
return FAMILY_PRESENTATION
if document.supportsService("com.sun.star.drawing.DrawingDocument"):
return FAMILY_DRAWING
raise DocumentConversionException, "unknown document family: %s" % document
def _getFileExt(self, path):
ext = splitext(path)[1]
if ext is not None:
return ext[1:].lower()
def _getFileBasename(self, path):
name = splitext(path)[0]
if name is not None:
return name
def _toFileUrl(self, path):
return uno.systemPathToFileUrl(abspath(path))
def _toProperties(self, options):
props = []
for key in options:
if isinstance(options[key], dict):
property = PropertyValue(key, 0, uno.Any("[]com.sun.star.beans.PropertyValue", (self._toProperties(options[key]))), 0)
else:
property = PropertyValue(key, 0, options[key], 0)
props.append(property)
return tuple(props)
def _dump(self, obj):
for attr in dir(obj):
print "obj.%s = %s\n" % (attr, getattr(obj, attr))
if __name__ == "__main__":
from sys import exit
from optparse import OptionParser
parser = OptionParser(usage="usage: python %prog [options] <input-file> <output-file>", version="%prog 1.5")
parser.add_option("-s", "--paper-size", default="A4", action="store", type="string", dest="paper_size", help="defines the paper size to converter that can be A3, A4, A5.")
parser.add_option("-o", "--paper-orientation", default="PORTRAIT", action="store", type="string", dest="paper_orientation", help="defines the paper orientation to converter that can be PORTRAIT or LANDSCAPE.")
(options, args) = parser.parse_args()
if len(args) != 2:
parser.error("wrong number of arguments")
if not isfile(args[0]):
print "No such input file: %s" % args[0]
exit(1)
try:
converter = DocumentConverter()
converter.convert(args[0], args[1], options.paper_size, options.paper_orientation)
except DocumentConversionException, exception:
print "ERROR! " + str(exception)
exit(1)
except ErrorCodeIOException, exception:
print "ERROR! ErrorCodeIOException %d" % exception.ErrCode
exit(1)