Commit cf39c5b436350e36324deba70f156a7cf78bad4c

Authored by Kevin Fourie
1 parent 56fd8367

KTS-2503

"Ports in openoffice python files are incorrect"
Changed port to 8100. Also added LGPL DocumentConvertor.py which seems good.

Committed By: Kevin Fourie
Reviewed By: Conrad Vermeulen



git-svn-id: https://kt-dms.svn.sourceforge.net/svnroot/kt-dms/trunk@7394 c91229c3-7414-0410-bfa2-8a42b809f60b
bin/openoffice/DocumentConverter.py 0 → 100644
  1 +#
  2 +# PyODConverter (Python OpenDocument Converter) v0.9 - 2007-04-05
  3 +#
  4 +# This script converts a document from one office format to another by
  5 +# connecting to an OpenOffice.org instance via Python-UNO bridge.
  6 +#
  7 +# Copyright (C) 2007 Mirko Nasato <mirko@artofsolving.com>
  8 +# Licensed under the GNU LGPL v2.1 - http://www.gnu.org/licenses/lgpl.html
  9 +#
  10 +DEFAULT_OPENOFFICE_PORT = 8100
  11 +
  12 +import uno
  13 +from os.path import abspath, splitext
  14 +from com.sun.star.beans import PropertyValue
  15 +from com.sun.star.connection import NoConnectException
  16 +
  17 +FAMILY_PRESENTATION = "Presentation"
  18 +FAMILY_SPREADSHEET = "Spreadsheet"
  19 +FAMILY_TEXT = "Text"
  20 +
  21 +FAMILY_BY_EXTENSION = {
  22 + "odt": FAMILY_TEXT,
  23 + "sxw": FAMILY_TEXT,
  24 + "doc": FAMILY_TEXT,
  25 + "rtf": FAMILY_TEXT,
  26 + "txt": FAMILY_TEXT,
  27 + "wpd": FAMILY_TEXT,
  28 + "html": FAMILY_TEXT,
  29 + "ods": FAMILY_SPREADSHEET,
  30 + "sxc": FAMILY_SPREADSHEET,
  31 + "xls": FAMILY_SPREADSHEET,
  32 + "odp": FAMILY_PRESENTATION,
  33 + "sxi": FAMILY_PRESENTATION,
  34 + "ppt": FAMILY_PRESENTATION
  35 +}
  36 +
  37 +FILTER_BY_EXTENSION = {
  38 + "pdf": {
  39 + FAMILY_TEXT: "writer_pdf_Export",
  40 + FAMILY_SPREADSHEET: "calc_pdf_Export",
  41 + FAMILY_PRESENTATION: "impress_pdf_Export"
  42 + },
  43 + "html": {
  44 + FAMILY_TEXT: "HTML (StarWriter)",
  45 + FAMILY_SPREADSHEET: "HTML (StarCalc)",
  46 + FAMILY_PRESENTATION: "impress_html_Export"
  47 + },
  48 + "odt": { FAMILY_TEXT: "writer8" },
  49 + "doc": { FAMILY_TEXT: "MS Word 97" },
  50 + "rtf": { FAMILY_TEXT: "Rich Text Format" },
  51 + "txt": { FAMILY_TEXT: "Text" },
  52 + "ods": { FAMILY_SPREADSHEET: "calc8" },
  53 + "xls": { FAMILY_SPREADSHEET: "MS Excel 97" },
  54 + "odp": { FAMILY_PRESENTATION: "impress8" },
  55 + "ppt": { FAMILY_PRESENTATION: "MS PowerPoint 97" },
  56 + "swf": { FAMILY_PRESENTATION: "impress_flash_Export" }
  57 +}
  58 +
  59 +
  60 +class DocumentConversionException(Exception):
  61 +
  62 + def __init__(self, message):
  63 + self.message = message
  64 +
  65 + def __str__(self):
  66 + return self.message
  67 +
  68 +
  69 +def _unoProps(**args):
  70 + props = []
  71 + for key in args:
  72 + prop = PropertyValue()
  73 + prop.Name = key
  74 + prop.Value = args[key]
  75 + props.append(prop)
  76 + return tuple(props)
  77 +
  78 +
  79 +class DocumentConverter:
  80 +
  81 + def __init__(self, port=DEFAULT_OPENOFFICE_PORT):
  82 + localContext = uno.getComponentContext()
  83 + resolver = localContext.ServiceManager.createInstanceWithContext("com.sun.star.bridge.UnoUrlResolver", localContext)
  84 + try:
  85 + context = resolver.resolve("uno:socket,host=localhost,port=%s;urp;StarOffice.ComponentContext" % port)
  86 + except NoConnectException:
  87 + raise DocumentConversionException, "failed to connect to OpenOffice.org on port %s" % port
  88 + self.desktop = context.ServiceManager.createInstanceWithContext("com.sun.star.frame.Desktop", context)
  89 +
  90 + def convert(self, inputFile, outputFile):
  91 + inputExt = self._fileExt(inputFile)
  92 + outputExt = self._fileExt(outputFile)
  93 +
  94 + filterName = self._filterName(inputExt, outputExt)
  95 +
  96 + inputUrl = self._fileUrl(argv[1])
  97 + outputUrl = self._fileUrl(argv[2])
  98 +
  99 + document = self.desktop.loadComponentFromURL(inputUrl, "_blank", 0, _unoProps(Hidden=True, ReadOnly=True))
  100 + document.storeToURL(outputUrl, _unoProps(FilterName=filterName))
  101 + document.close(True)
  102 +
  103 + def _filterName(self, inputExt, outputExt):
  104 + try:
  105 + family = FAMILY_BY_EXTENSION[inputExt]
  106 + except KeyError:
  107 + raise DocumentConversionException, "unknown input format: '%s'" % inputExt
  108 + try:
  109 + filterByFamily = FILTER_BY_EXTENSION[outputExt]
  110 + except KeyError:
  111 + raise DocumentConversionException, "unknown output format: '%s'" % outputExt
  112 + try:
  113 + return filterByFamily[family]
  114 + except KeyError:
  115 + raise DocumentConversionException, "unsupported conversion: from '%s' to '%s'" % (inputExt, outputExt)
  116 +
  117 + def _fileExt(self, path):
  118 + ext = splitext(path)[1]
  119 + if ext is not None:
  120 + return ext[1:].lower()
  121 +
  122 + def _fileUrl(self, path):
  123 + return uno.systemPathToFileUrl(abspath(path))
  124 +
  125 +
  126 +if __name__ == "__main__":
  127 + from sys import argv, exit
  128 +
  129 + if len(argv) < 3:
  130 + print "USAGE: " + argv[0] + " <input-file> <output-file>"
  131 + exit(255)
  132 +
  133 + try:
  134 + converter = DocumentConverter()
  135 + converter.convert(argv[1], argv[2])
  136 + except DocumentConversionException, exception:
  137 + print "ERROR! " + str(exception)
  138 + exit(1)
  139 +
... ...
bin/openoffice/pdfgen.py
... ... @@ -45,7 +45,7 @@ try:
45 45 ### Get Service Manager
46 46 context = uno.getComponentContext()
47 47 resolver = context.ServiceManager.createInstanceWithContext("com.sun.star.bridge.UnoUrlResolver", context)
48   - ctx = resolver.resolve("uno:socket,host=localhost,port=2002;urp;StarOffice.ComponentContext")
  48 + ctx = resolver.resolve("uno:socket,host=localhost,port=8100;urp;StarOffice.ComponentContext")
49 49 smgr = ctx.ServiceManager
50 50  
51 51 ### Load document
... ...