OOoUtils.py 16.4 KB
Newer Older
Kevin Deldycke's avatar
Kevin Deldycke committed
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
##############################################################################
#
# Copyright (c) 2003-2005 Nexedi SARL and Contributors. All Rights Reserved.
#                         Kevin DELDYCKE    <kevin@nexedi.com>
#                         Guillaume MICHON  <guillaume@nexedi.com>
#
# WARNING: This program as such is intended to be used by professional
# programmers who take the whole responsability of assessing all potential
# consequences resulting from its eventual inadequacies and bugs
# End users who are looking for a ready-to-use solution with commercial
# garantees and support are strongly adviced to contract a Free Software
# Service Company
#
# This program is Free Software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# 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 General Public License for more details.
#
# You should have received a copy of the GNU 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.
#
##############################################################################

from Products.PythonScripts.Utility import allow_class
from ZPublisher.HTTPRequest import FileUpload
from xml.dom.ext.reader import PyExpat
from xml.dom import Node
from AccessControl import ClassSecurityInfo
35 36 37
from Globals import InitializeClass, get_request
from zipfile import ZipFile, ZIP_DEFLATED
from StringIO import StringIO
Kevin Deldycke's avatar
Kevin Deldycke committed
38 39
from zLOG import LOG
import imghdr
40
import random
Kevin Deldycke's avatar
Kevin Deldycke committed
41 42 43 44 45



class CorruptedOOoFile(Exception): pass

46 47 48 49 50 51 52 53 54 55 56 57
OOo_mimeType_dict = {
  'sxw' : 'application/vnd.sun.xml.writer',
  'stw' : 'application/vnd.sun.xml.writer.template',
  'sxg' : 'application/vnd.sun.xml.writer.global',
  'sxc' : 'application/vnd.sun.xml.calc',
  'stc' : 'application/vnd.sun.xml.calc.template',
  'sxi' : 'application/vnd.sun.xml.impress',
  'sti' : 'application/vnd.sun.xml.impress.template',
  'sxd' : 'application/vnd.sun.xml.draw',
  'std' : 'application/vnd.sun.xml.draw.template',
  'sxm' : 'application/vnd.sun.xml.math',
}
Kevin Deldycke's avatar
Kevin Deldycke committed
58

59 60 61 62 63 64 65 66 67
class OOoBuilder:
  """
  Tool that allows to reinject new files in a ZODB OOo document.
  """
  # Declarative security
  security = ClassSecurityInfo()

  security.declarePrivate('__init__')
  def __init__(self, document):
68 69 70 71 72 73
    if hasattr(document, 'data') :
      self._document = StringIO(document.data)
    elif hasattr(document, 'read') :
      self._document = document
    else :
      self._document = StringIO(document)
74
    self._image_count = 0    
75 76 77 78 79 80 81 82 83 84 85 86 87

  security.declarePublic('replace')
  def replace(self, filename, stream):
    """
    Replaces the content of filename by stream in the archive.
    Creates a new file if filename was not already there.
    """
    try:
      zf = ZipFile(self._document, mode='a', compression=ZIP_DEFLATED)
    except RuntimeError:
      zf = ZipFile(self._document, mode='a')
    zf.writestr(filename, stream)
    zf.close()
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
  
  security.declarePublic('extract')
  def extract(self, filename):
    """
    Extracts a file from the archive
    """
    try:
      zf = ZipFile(self._document, mode='r', compression=ZIP_DEFLATED)
    except RuntimeError:
      zf = ZipFile(self._document, mode='r')
    return zf.read(filename)
  
  security.declarePublic('getMimeType')
  def getMimeType(self):
    return self.extract('mimetype')

  security.declarePublic('prepareContentXml')
  def prepareContentXml(self) :
    """
      extracts content.xml text and prepare it :
        - add tal namespace
        - indent the xml
    """
    import pprint
    content_xml = self.extract('content.xml')
    reader = PyExpat.Reader()
    document = reader.fromString(content_xml)
    document_element = document.documentElement
    from xml.dom.ext import PrettyPrint
    output = StringIO()
    PrettyPrint(document_element, output)
    return output.getvalue().replace(
      "office:version='1.0'",
      """ xmlns:tal='http://xml.zope.org/namespaces/tal'
          xmlns:i18n='http://xml.zope.org/namespaces/i18n'
          xmlns:metal='http://xml.zope.org/namespaces/metal'
          tal:attributes='dummy python:request.RESPONSE.setHeader("Content-Type", "text/html;; charset=utf-8")'
         office:version='1.0'""")
126

127
  security.declarePublic('addImage')
128 129 130 131 132 133 134 135 136 137
  def addImage(self, image, format='png'):
    """
    Add an image to the current document and return its id
    """
    count = self._image_count
    self._image_count += 1
    name = "Picture/%s.%s" % (count, format)
    self.replace(name, image)
    return "#%s" % name

138 139 140 141 142 143 144 145 146 147 148 149 150 151
  security.declarePublic('render')
  def render(self, name='', extension='sxw'):
    """
    returns the OOo document
    """
    request = get_request()
    request.response.setHeader('Content-type', OOo_mimeType_dict.get(extension, 'application/vnd.sun.xml.writer'))
    if name:
      request.response.setHeader('Content-Disposition', 'attachment; filename=%s.%s' % (name, extension))
    self._document.seek(0)
    return self._document.read()
    
InitializeClass(OOoBuilder)
allow_class(OOoBuilder)
Kevin Deldycke's avatar
Kevin Deldycke committed
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171

class OOoParser:
  """
    General purpose tools to parse and handle OpenOffice v1.x documents.
  """


  # Declarative security
  security = ClassSecurityInfo()


  security.declarePrivate('__init__')
  def __init__(self):
    # Create the PyExpat reader
    self.reader = PyExpat.Reader()
    self.oo_content_dom = None
    self.oo_styles_dom  = None
    self.oo_files = {}
    self.pictures = {}
    self.ns = {}
Kevin Deldycke's avatar
Kevin Deldycke committed
172
    self.filename = None
Kevin Deldycke's avatar
Kevin Deldycke committed
173 174


175
  security.declareProtected('Import/Export objects', 'openFile')
176
  def openFile(self, file_descriptor):
Kevin Deldycke's avatar
Kevin Deldycke committed
177 178 179 180 181
    """
      Load all files in the zipped OpenOffice document
    """
    # Try to unzip the Open Office doc
    try:
182
      oo_unzipped = ZipFile(file_descriptor, mode="r")
Kevin Deldycke's avatar
Kevin Deldycke committed
183 184 185 186 187 188
    except:
      raise CorruptedOOoFile
    # Test the integrity of the file
    if oo_unzipped.testzip() != None:
      raise CorruptedOOoFile

Kevin Deldycke's avatar
Kevin Deldycke committed
189 190 191
    # Get the filename
    self.filename = file_descriptor.filename

Kevin Deldycke's avatar
Kevin Deldycke committed
192 193 194
    # List and load the content of the zip file
    for name in oo_unzipped.namelist():
      self.oo_files[name] = oo_unzipped.read(name)
195
    oo_unzipped.close()
Kevin Deldycke's avatar
Kevin Deldycke committed
196 197 198 199 200 201 202 203 204 205 206 207 208 209

    # Get the main content and style definitions
    self.oo_content_dom = self.reader.fromString(self.oo_files["content.xml"])
    self.oo_styles_dom  = self.reader.fromString(self.oo_files["styles.xml"])

    # Create a namespace table
    doc_ns = self.oo_styles_dom.getElementsByTagName("office:document-styles")
    for i in range(doc_ns[0].attributes.length):
        if doc_ns[0].attributes.item(i).nodeType == Node.ATTRIBUTE_NODE:
            name = doc_ns[0].attributes.item(i).name
            if name[:5] == "xmlns":
                self.ns[name[6:]] = doc_ns[0].attributes.item(i).value


Kevin Deldycke's avatar
Kevin Deldycke committed
210 211 212 213 214 215 216 217
  security.declarePublic('getFilename')
  def getFilename(self):
    """
      Return the name of the OpenOffice file
    """
    return self.filename


218 219
  security.declarePublic('getPicturesMapping')
  def getPicturesMapping(self):
Kevin Deldycke's avatar
Kevin Deldycke committed
220 221 222 223 224 225 226 227 228 229 230 231
    """
      Return a dictionnary of all pictures in the document
    """
    if len(self.pictures) <= 0:
      for file_name in self.oo_files:
        raw_data = self.oo_files[file_name]
        pict_type = imghdr.what(None, raw_data)
        if pict_type != None:
          self.pictures[file_name] = raw_data
    return self.pictures


232 233
  security.declarePublic('getContentDom')
  def getContentDom(self):
Kevin Deldycke's avatar
Kevin Deldycke committed
234 235 236 237 238 239
    """
      Return the DOM tree of the main OpenOffice content
    """
    return self.oo_content_dom


240 241
  security.declarePublic('getSpreadsheetsDom')
  def getSpreadsheetsDom(self, include_embedded=False):
242 243 244 245
    """
      Return a list of DOM tree spreadsheets (optionnaly included embedded ones)
    """
    spreadsheets = []
246
    spreadsheets = self.getPlainSpreadsheetsDom()
247
    if include_embedded == True:
248
      spreadsheets += self.getEmbeddedSpreadsheetsDom()
249 250 251
    return spreadsheets


252
  security.declarePublic('getSpreadsheetsMapping')
253
  def getSpreadsheetsMapping(self, include_embedded=False, no_empty_lines=False, normalize=True):
254 255 256
    """
      Return a list of table-like spreadsheets (optionnaly included embedded ones)
    """
257
    tables = {}
258
    tables = self.getPlainSpreadsheetsMapping(no_empty_lines, normalize)
259
    if include_embedded == True:
260
      embedded_tables = self.getEmbeddedSpreadsheetsMapping(no_empty_lines, normalize)
261 262
      tables = self._getTableListUnion(tables, embedded_tables)
    return tables
263 264


265 266
  security.declarePublic('getPlainSpreadsheetsDom')
  def getPlainSpreadsheetsDom(self):
267 268 269 270 271 272 273 274 275 276
    """
      Retrieve every spreadsheets from the document and get they DOM tree
    """
    spreadsheets = []
    # List all spreadsheets
    for table in self.oo_content_dom.getElementsByTagName("table:table"):
      spreadsheets.append(table)
    return spreadsheets


277
  security.declarePublic('getPlainSpreadsheetsMapping')
278
  def getPlainSpreadsheetsMapping(self, no_empty_lines=False, normalize=True):
279 280 281
    """
      Return a list of plain spreadsheets from the document and transform them as table
    """
282
    tables = {}
283
    for spreadsheet in self.getPlainSpreadsheetsDom():
284
      new_table = self.getSpreadsheetMapping(spreadsheet, no_empty_lines, normalize)
285
      if new_table != None:
286
        tables = self._getTableListUnion(tables, new_table)
287 288 289
    return tables


290 291
  security.declarePublic('getEmbeddedSpreadsheetsDom')
  def getEmbeddedSpreadsheetsDom(self):
Kevin Deldycke's avatar
Kevin Deldycke committed
292 293 294 295 296 297 298 299 300 301 302
    """
      Return a list of existing embedded spreadsheets in the file as DOM tree
    """
    spreadsheets = []
    # List all embedded spreadsheets
    emb_objects = self.oo_content_dom.getElementsByTagName("draw:object")
    for embedded in emb_objects:
      document = embedded.getAttributeNS(self.ns["xlink"], "href")
      if document:
        try:
          object_content = self.reader.fromString(self.oo_files[document[3:] + '/content.xml'])
303 304
          for table in object_content.getElementsByTagName("table:table"):
            spreadsheets.append(table)
Kevin Deldycke's avatar
Kevin Deldycke committed
305 306 307 308 309
        except:
          pass
    return spreadsheets


310
  security.declarePublic('getEmbeddedSpreadsheetsMapping')
311
  def getEmbeddedSpreadsheetsMapping(self, no_empty_lines=False, normalize=True):
Kevin Deldycke's avatar
Kevin Deldycke committed
312
    """
313
      Return a list of embedded spreadsheets in the document as table
Kevin Deldycke's avatar
Kevin Deldycke committed
314
    """
315
    tables = {}
316
    for spreadsheet in self.getEmbeddedSpreadsheetsDom():
317
      new_table = self.getSpreadsheetMapping(spreadsheet, no_empty_lines, normalize)
Kevin Deldycke's avatar
Kevin Deldycke committed
318
      if new_table != None:
319
        tables = self._getTableListUnion(tables, new_table)
Kevin Deldycke's avatar
Kevin Deldycke committed
320 321 322
    return tables


323
  security.declarePublic('getSpreadsheetMapping')
324
  def getSpreadsheetMapping(self, spreadsheet=None, no_empty_lines=False, normalize=True):
Kevin Deldycke's avatar
Kevin Deldycke committed
325 326
    """
      This method convert an OpenOffice spreadsheet to a simple table.
327
      This code is based on the oo2pt tool (http://cvs.sourceforge.net/viewcvs.py/collective/CMFReportTool/oo2pt).
Kevin Deldycke's avatar
Kevin Deldycke committed
328
    """
329
    if spreadsheet == None or spreadsheet.nodeName != 'table:table':
Kevin Deldycke's avatar
Kevin Deldycke committed
330 331
      return None

332
    table = []
Kevin Deldycke's avatar
Kevin Deldycke committed
333

334 335 336
    # Get the table name
    table_name = spreadsheet.getAttributeNS(self.ns["table"], "name")

337 338
    # Scan table and store usable informations
    for line in spreadsheet.getElementsByTagName("table:table-row"):
339 340 341 342 343 344

      # TODO : to the same as cell about abusive repeated lines

      line_group_found = line.getAttributeNS(self.ns["table"], "number-rows-repeated")
      if not line_group_found:
        lines_to_repeat = 1
345
      else:
346
        lines_to_repeat = int(line_group_found)
347

348
      for i in range(lines_to_repeat):
349 350
        table_line = []

351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
        # Get all cells
        cells = line.getElementsByTagName("table:table-cell")
        cell_index_range = range(len(cells))

        for cell_index in cell_index_range:
          cell = cells[cell_index]

          # If the cell as no child, cells have no content
          # And if the cell is the last of the row, we don't need to add it to the line
          # So we can go to the next line (= exit this cells loop)
          #
          # I must do this test because sometimes the following cell group
          #   can be found in OOo documents : <table:table-cell table:number-columns-repeated='246'/>
          # This is bad because it create too much irrevelent content that slow down the process
          # So it's a good idea to break the loop in this case
          if cell.childNodes.length == 0 and cell_index == cell_index_range[-1]:
            break

          # Handle cells group
          cell_group_found = cell.getAttributeNS(self.ns["table"], "number-columns-repeated")
          if not cell_group_found:
            cells_to_repeat = 1
373
          else:
374
            cells_to_repeat = int(cell_group_found)
375

376 377 378
          # Ungroup repeated cells
          for j in range(cells_to_repeat):
            # Get the cell content
379 380 381 382 383 384 385 386 387 388
            cell_text = None
            text_tags = cell.getElementsByTagName("text:p")
            for text in text_tags:
              for k in range(text.childNodes.length):
                child = text.childNodes[k]
                if child.nodeType == Node.TEXT_NODE:
                  if cell_text == None:
                    cell_text = ''
                  cell_text += child.nodeValue

389
            # Add the cell to the line
390
            table_line.append(cell_text)
391

Kevin Deldycke's avatar
Kevin Deldycke committed
392 393 394 395 396 397 398 399 400
        # Delete empty lines if needed
        if no_empty_lines:
          empty_cell = 0
          for table_cell in table_line:
            if table_cell == None:
              empty_cell += 1
          if empty_cell == len(table_line):
            table_line = None

401
        # Add the line to the table
Kevin Deldycke's avatar
Kevin Deldycke committed
402 403
        if table_line != None:
          table.append(table_line)
404

405
    # Reduce the table to the minimum
406 407 408 409 410 411 412 413 414 415
    new_table = self._getReducedTable(table)

    # Get a homogenized table
    if normalize:
      table_size = self._getTableSizeDict(new_table)
      new_table = self._getNormalizedBoundsTable( table  = new_table
                                                , width  = table_size['width']
                                                , height = table_size['height']
                                                )
    return {table_name: new_table}
Kevin Deldycke's avatar
Kevin Deldycke committed
416 417


418 419
  security.declarePrivate('_getReducedTable')
  def _getReducedTable(self, table):
Kevin Deldycke's avatar
Kevin Deldycke committed
420
    """
421
      Reduce the table to its minimum size
Kevin Deldycke's avatar
Kevin Deldycke committed
422 423 424 425 426
    """
    empty_lines = 0
    no_more_empty_lines = 0

    # Eliminate all empty cells at the ends of lines and columns
427
    # Browse the table starting from the bottom for easy empty lines count
428
    for line in range(len(table)-1, -1, -1):
Kevin Deldycke's avatar
Kevin Deldycke committed
429
      empty_cells = 0
430
      line_content = table[line]
Kevin Deldycke's avatar
Kevin Deldycke committed
431
      for cell in range(len(line_content)-1, -1, -1):
432
        if line_content[cell] in ('', None):
Kevin Deldycke's avatar
Kevin Deldycke committed
433 434 435
          empty_cells += 1
        else:
          break
436

Kevin Deldycke's avatar
Kevin Deldycke committed
437 438 439 440
      if (not no_more_empty_lines) and (empty_cells == len(line_content)):
        empty_lines += 1
      else:
        line_size = len(line_content) - empty_cells
441
        table[line] = line_content[:line_size]
Kevin Deldycke's avatar
Kevin Deldycke committed
442 443
        no_more_empty_lines = 1

444 445 446
    table_height = len(table) - empty_lines

    return table[:table_height]
Kevin Deldycke's avatar
Kevin Deldycke committed
447

448 449 450 451 452 453

  security.declarePrivate('_getTableSizeDict')
  def _getTableSizeDict(self, table):
    """
      Get table dimension as dictionnary contain both height and width
    """
Kevin Deldycke's avatar
Kevin Deldycke committed
454
    max_cols = 0
455 456 457 458
    for line_index in range(len(table)):
      line = table[line_index]
      if len(line) > max_cols:
        max_cols = len(line)
Kevin Deldycke's avatar
Kevin Deldycke committed
459

460 461 462
    return { 'width' : max_cols
           , 'height': len(table)
           }
Kevin Deldycke's avatar
Kevin Deldycke committed
463 464


465 466
  security.declarePrivate('_getNormalizedBoundsTable')
  def _getNormalizedBoundsTable(self, table, width=0, height=0):
Kevin Deldycke's avatar
Kevin Deldycke committed
467
    """
468
      Add necessary cells and lines to obtain given bounds
Kevin Deldycke's avatar
Kevin Deldycke committed
469
    """
470 471
    while height > len(table):
      table.append([])
Kevin Deldycke's avatar
Kevin Deldycke committed
472
    for line in range(height):
473 474 475 476
      while width > len(table[line]):
        table[line].append(None)
    return table

Kevin Deldycke's avatar
Kevin Deldycke committed
477

478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494
  security.declarePrivate('_getTableListUnion')
  def _getTableListUnion(self, list1, list2):
    """
      Coerce two dict containing tables structures.
      We need to use this method because a OpenOffice document can hold
        several embedded spreadsheets with the same id. This explain the
        use of random suffix in such extreme case.
    """
    for list2_key in list2.keys():
      # Generate a new table ID if needed
      new_key = list2_key
      while new_key in list1.keys():
        new_key = list2_key + '_' + str(random.randint(1000,9999))
      list1[new_key] = list2[list2_key]
    return list1


495

Kevin Deldycke's avatar
Kevin Deldycke committed
496 497
InitializeClass(OOoParser)
allow_class(OOoParser)