Source

odt2org / odtfile.py

Full commit
  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
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
# -*- coding: utf-8 -*-
'''
TODO:
-----
1. [X] It fails when the document doesn't contain text. (Nick Dokos)
   To fail cleanly and advice this is not OCR software.
2. <text:p>   -> This is a typical paragraph.
     tetxo01 jskdljsd
     <text:s text:c="6">   -> This means 6 spaces
    more text (this is a continuation)
    <text:tab />     -> This is a tab
   </text:p>
'''

ERROR = False
import codecs
import zipfile
import os
import string
import re
from orgfile import ORGfile
try:
    from lxml import etree
except ImportError:
    print u'ERROR: install "nxml": http://codespeak.net/lxml/'
    ERROR = True

try:
    import OleFileIO_PL as ole
except ImportError:
    print u'ERROR: please, install "OleFileIO_PL": http://www.decalage.info/files/OleFileIO_PL-0.18.zip'
    # http://www.decalage.info/en/python/olefileio
    ERROR = True

class ODTfileError(Exception):
    def __init__(self):
        super(ODTfileError,self).__init__()

class ODTfile:
    '''This class enables the access to OpenOffice files'''
    def __init__(self,
                 filename = None):
        '''All the fields are mandatory. All the checks are done outside.
        '''
        self.ns = { 
           'office' : "urn:oasis:names:tc:opendocument:xmlns:office:1.0", 
           'text'   : "urn:oasis:names:tc:opendocument:xmlns:text:1.0",
           'table'  : "urn:oasis:names:tc:opendocument:xmlns:table:1.0",
           'draw'   : "urn:oasis:names:tc:opendocument:xmlns:drawing:1.0",
           'xlink'  : "http://www.w3.org/1999/xlink"
                  }

        self.types = { 
            '{%s}%s' % (self.ns['text'],'p') : 'paragraph',
            '{%s}%s' % (self.ns['text'],'h') : 'heading',
            '{%s}%s' % (self.ns['draw'],'frame') : 'frame',
            '{%s}%s' % (self.ns['draw'],'name') : 'framename',
            '{%s}%s' % (self.ns['text'],'outline-level') : 'outline-level',
            '{%s}%s' % (self.ns['text'],'list'): 'list',
            '{%s}%s' % (self.ns['text'],'list-item'): 'list-item',
            '{%s}%s' % (self.ns['text'],'style-name'): 'style-name',
            '{%s}%s' % (self.ns['table'],'table'): 'table',
            '{%s}%s' % (self.ns['draw'],'object-ole'): 'object-ole',
            '{%s}%s' % (self.ns['draw'],'image'): 'image',
            '{%s}%s' % (self.ns['text'],'s') : 'spaces',
            '{%s}%s' % (self.ns['text'],'tab') : 'tabs',
            '{%s}%s' % (self.ns['text'],'note') : 'note',
            '{%s}%s' % (self.ns['text'],'note-citation') : 'note-citation',
            '{%s}%s' % (self.ns['text'],'note-body') : 'note-body'
                        }
        #

        if filename != None:
            self.filename = os.path.realpath( filename )

            # Prefix
            #_tmp = os.path.split( self.orgfile.filename )[1]
            #_tmp = _tmp.replace(' ','_') 
            #_tmp = _tmp.replace('.','_')
            #self.prefix = _tmp + '_'

            # Files
            try:
                self.files = zipfile.ZipFile( self.filename, "r")
            except:
                raise ODTFileError( u"""ODT files are zip files. The file provided isn't.""" )

            if not self.isOpenOfficeFile():
                raise ODTFileError( u"""Only compatible with ODT files 1.0 specification.""" )
 
            self.xml = None
        else:
            raise ODTFileError( u"""Provided ODT file class ODTfile requires a valid filename during initialization.""" )

    def isOpenOfficeFile(self):
        '''Verifies if this is an Open Office Document'''
        # 1. Contiene el fichero "content.xml"
        _isOK = False
        if self.files.namelist().__contains__( 'content.xml'):

            # 2. Leemos el fichero 'content.xml' y vemos que tiene el tag "office:document-content"
            _data = self.files.read( 'content.xml' )
            self.xml = etree.fromstring( _data )
            _lista = self.xml.xpath('/office:document-content', 
                                namespaces= { 'office' : 'urn:oasis:names:tc:opendocument:xmlns:office:1.0'} )
            if len(_lista) == 1:
                _isOK = True
        return _isOK 

    def _get_textbody( self ):
        """Returns the body from the file
        """
        _data = self.files.read('content.xml')
        _dataxml = etree.fromstring( _data )

        _body = _dataxml.xpath('/office:document-content/office:body', 
                             namespaces= { 'office' : 'urn:oasis:names:tc:opendocument:xmlns:office:1.0'} )[0]

        return _body.xpath('office:text', 
                            namespaces= { 'office' : 'urn:oasis:names:tc:opendocument:xmlns:office:1.0'} )[0]


    def gen_list(self):
        """Creates a list that is understood by ORGfile class.
        """
        # Get all the nodes.
        _xml = self._get_textbody()
        _list = []

        for _child in _xml.iterdescendants():
            _level = 0
            for _i in _child.iterancestors():
                _level += 1
            
            _tmp = { 'child' : _child,
                     'pretext' : _child.text,
                     'posttext' : _child.tail,
                     'nesting' : _level}
            _list.append( _tmp )

        # Extract text from paragraphs, heading, spaces, tabs.

        # - Extract spaces, tabs
        _newlist = []
        for _i in _list:
            if _i.__contains__('child'):
                _child = _i['child']
                _tag = _child.tag
                if self.types.__contains__(_tag):
                    _type = self.types[_tag]
                    if _type == 'spaces':
                        _txt = self.get_spaces(_child)
                        if _i['pretext'] != None:
                            _txt = _i['pretext'] + _txt
                        if _i['posttext'] != None:
                            _txt = _txt + _i['posttext']

                        _tmp = {'txt' : _txt, 'nesting': _i['nesting']}
                        _newlist.append(_tmp)
                    elif _type == 'tabs':
                        _txt = self.get_tabs(_child)
                        if _i['pretext'] != None:
                            _txt = _i['pretext'] + _txt
                        if _i['posttext'] != None:
                            _txt = _txt + _i['posttext']
                        _tmp = {'txt' : _txt, 'nesting': _i['nesting']}
                        _newlist.append(_tmp)
                    else:
                        _newlist.append(_i)
                else:
                    _newlist.append(_i)
            else:
                _newlist.append(_i)
            
        _list = _newlist  

        # - Footnotes
        _footnotes = []
        _list = self.group_type( _list, tag = 'note')
        _newlist = []
        for _i in _list:
            if _i.__contains__('note'):
                _txt, _paragraphs = self.get_footnote(_i)
                _newlist.append( {'txt' : _txt, 
                                  'nesting' : _i['nesting']})
                
                _footnotes.append((_txt, _paragraphs))
            else:
                _newlist.append(_i)
        _list = _newlist

        # - Regroup: move spaces, tabs into the prior child.
        _newlist = []
        _tmp = u''
        for _i in xrange(len(_list)):
            _item = _list[_i]
            if _item.__contains__('child'):
                if _tmp != u'':
                    _newlist[-1].update({'txt' : _tmp})
                    _tmp = u''
                _newlist.append(_item)
            else:
                _tmp = _tmp + _item['txt']
            
        _list = _newlist        
         
        # - Getting heading and paragraph text.
        _newlist = []
        for _i in _list:
            _flag = True
            # - Heading
            try:
                if self.types[ _i['child'].tag] == 'heading':
                    _tmp = u''
                    if _i['pretext'] != None:
                        _tmp += _i['pretext']
                    if _i.__contains__('txt'):
                        if _i['txt'] != None:
                            _tmp += _i['txt']
                    if _i['posttext'] != None:
                        _tmp += _i['posttext']
                    _tags =  self.get_tags( _i['child'] )
                    #print _i['child']
                    _tags.update({'type' : 'heading'})
                    _tmp = { 'text' : _tmp,
                             'nesting': _i['nesting'],
                             'tags' : _tags}

                    _newlist.append(_tmp)
                    _flag = False
            except:
                pass 
            # - Paragraph
            try:
                if self.types[ _i['child'].tag] == 'paragraph':
                    _tmp = u''
                    if _i['pretext'] != None:
                        _tmp += _i['pretext']
                    if _i.__contains__('txt'):
                        if _i['txt'] != None:
                            _tmp += _i['txt']
                    if _i['posttext'] != None:
                        _tmp += _i['posttext']
                    _tags =  self.get_tags( _i['child'] )
                    _tags.update({'type' : 'paragraph'})
                    _tmp = { 'text' : _tmp,
                             'nesting': _i['nesting'],
                             'tags' : _tags}
                    _newlist.append(_tmp)
                    _flag = False
            except:
                pass 
            if _flag:
                _newlist.append(_i)
            
        _list = _newlist        
        # LIST
        #_list = self.group_type( _list, tag = 'list', clean= True)
        _list = self.group_children( _list, tag = 'list', clean= True)
        _newlist = []
        for _i in _list:
            if _i.__contains__('list'):
                _tmp = self.clean( _i['list'], tags = ['list'])
                _tmp1 = _i.copy()
                _tmp1['list'] = _tmp
                _newlist.append( _tmp1)
            else:
                _newlist.append( _i)
        _list = _newlist
        for _i in _list:
            print _i
        _list = self.process_enumeration(_list)

        # TABLE
        _list = self.group_type( _list, tag = 'table')

        # PRINT
        #for _i in _list:
        #    print _i
        #print _footnotes
        return _list

    def process_enumeration(self, _list):
        """
        """
        _newlist = []
        for _i in _list:
            if _i.__contains__('list'):
                #print _i,'\n'

                _tmp = self.group_type( _i['list'], 
                                        tag = 'list-item',
                                        clean = True)
  
                # Clean from 'list' and 'list-item'
                #_tmp = self.clean(_tmp, tags = ['list','list-item'])

                _tmp1 = _i.copy()
                _tmp1['list'] = _tmp

                _newlist.append( _tmp1 )
            else:
                _newlist.append( _i )
        #print '\n\n\n'        
        return _newlist

    def clean(self, _list, tags = []):
        _newlist = []
        for _i in _list:
            try:
                _tag = _i['child'].tag
                _type = self.types[_tag]
            except:
                _type = None
            if not tags.__contains__(_type):
                _newlist.append(_i)
        return _newlist
                

    def __processEnumeration__(self,
                               _child):
        '''Structure for Enumerations:
        <text:list>
           <text:list-item>
                <text:p>
        '''
        _list = []
        _level = 1
        _levels = { _child : {'type' : 'list', 'level' : 1}}
        for _i in _child.iterdescendants(): # We follow all the nested structure.
            # list-item: mismo nivel que su padre.
            if _i.tag == '{%s}%s' % (self.ns['text'],'list-item'):
                _levels.update({ _i : {'type' : 'list-item',
                                      'level' : _levels[ _i.getparent()]['level']}})
            # list: si el padre era un list-item, se le suma uno.
            if _i.tag == '{%s}%s' % (self.ns['text'],'list'):
                _levels.update({ _i : {'type' : 'list',
                                      'level' : _levels[ _i.getparent()]['level']+1}})
                
            if _i.tag == '{%s}%s' % (self.ns['text'],'p'):
                _levels.update({ _i : {'type' : 'p',
                                       'level' : _levels[ _i.getparent()]['level']}})
                _n = _levels[ _i.getparent()]['level']
                _tmp = []
                for _j in _i.itertext():
                    if _j != None:
                        
                        _tmp = _tmp + [u'%s' % _j]
                        #_list = _list + [ (_n, _tmp )]
                _list = _list + [ (_n, _tmp) ]
        return _list
    def group_recursively(self, _list, tag = 'list'):
        """Groups recursively up to making dissapear the tag
        """
        
        pass

    def has_tag(self):
        pass

    def group_children( self,
                        _list, 
                        tag = 'list',
                        clean = False ):
        """Groups one tag and all its children.
        """
        _newlist = []
        _tmp = []
        _flag = False
        _n = 0
        for _i in _list:
            #_isTag = False
            try:
                if not _flag and self.types[ _i['child'].tag ] == tag:
                    #_isTag = True
                    _flag = True
                    _n = _i['nesting']
            except:
                pass
            
            #if not _flag and _isTag:
            #print _n, _i['nesting']
            if _flag and _i['nesting'] > _n:
                _tmp.append(_i ) 
            else:
                if _tmp != []:
                    _newlist.append({tag : _tmp, 'nesting' : _n})
                    _tmp = []
                    _flag = False 
                    _n = 0
                _newlist.append(_i)
        if _flag and _tmp!=[]:
            _newlist.append({tag : _tmp, 'nesting' : _n})
        return _newlist       

    def group_type( self,
                    _list, 
                    tag = 'note', 
                    clean = False ):
        _pairs = self.extract_children(_list, tag )
        if tag == 'list':
            print '    PAIRS: ',_pairs
        # Extract _list
        _newlist = []
        _oldidx = 0
        for _pair in _pairs:
            _newlist += _list[_oldidx:_pair[0]]
            _note = _list[_pair[0]:_pair[1]]
            _posttext = None
            if _note[0]['posttext'] != None:
                _posttext = _note[0]['posttext']
                _note[0]['posttext'] = None
            _nesting = _list[_pair[0]]['nesting']
            if clean:
                _note = _note[1:]
            _newlist.append( { tag : _note,
                               'nesting' : _nesting})
            if _posttext != None:
                _newlist.append( {'txt' : _posttext,
                                  'nesting' : _nesting})
            _oldidx = _pair[1]
        _newlist += _list[_oldidx:]
        return _newlist

    def extract_children( self, _list, tag = 'note'):
        _newlist = []
        _limits = []
        _flag = False
        _nesting = 0
        #if tag == 'list-item':
        #   print '== OK ==', _limits
        for _i in xrange(len(_list)):
            _item = _list[_i]
            try:
                if self.types[ _item['child'].tag ] == tag:
                    #_test = groupchildren and _item['nesting']< _nesting
                    #if not _test:
                    if _flag:
                        _limits.append(_i)
                        _newlist.append( _limits)
                    _flag = True
                    _nesting = _item['nesting']+1
                    _limits = [_i]                    
                    _tmp = _item
                    #if tag == 'list-item':
                    #   print '== OK ==', _limits
                    #   print _item['nesting']
            except:
                pass
       
            if _flag and _item != _tmp:
                #if tag == 'list-item':
                #    print '<< OK >>', _limits
                #    print _item['nesting'], _nesting
                if _item['nesting'] < _nesting:
                    _flag = False
                    _nesting = 0
                    _limits.append(_i)

                    _newlist.append( _limits)
                    _limits = []
        if _limits != []:
            _limits.append(len(_list))
            _newlist.append( _limits)
         
        return  _newlist        

    def get_footnote(self, _note):
        _paragraphs = []
        _flag = False

        for _item in _note['note']:
            if _flag:
                _paragraphs.append(_item)
            try:
                _type = self.types[_item['child'].tag]
            except:
                _type = ''
            if _type == 'note-citation':
                _id = _item['pretext']
            elif _type == 'note-body':
                _flag = True
        _id = _id.replace('[','_')
        _id = _id.replace(']','_')
        _id = _id.replace(':','_')
        _id = u'[fn:%s] ' % _id
        #print _paragraphs
        return _id, _paragraphs

    def gen_list2(self):
        """Creates a list that is understood by ORGfile class.
        """
        _xml = self._get_textbody()
        _list = []

        for _child in _xml.getchildren():
            _tmp = self.get_newlist( [{'child' : _child,
                                       'tags' : {'nesting' : 1}}] )
            if _tmp != None:
                _list = _list + _tmp
        for _i in _list:
            print _i
        return _list


    def get_newlist(self, _list ):
        """
        """
        while self.has_children(_list):
            _newlist = []
            for _i in _list:
                if not _i.__contains__('child'):
                    _newlist.append(_i)
                else:
                    _newlist += self.analyse_child( _i)
            _list = _newlist
        return _list 

    def has_children(self, _list):
        for _i in _list:
            if _i.__contains__('child'):
                return True
        return False

    def analyse_child( self, _dict):
        """Each child will contain:
        - TAG: defines which kind of node the child is.
        - ATTRIB: dictionary defining node attributes.
        - CHILDREN: the node may contain children.
        """
        _child = _dict['child']
        _tags = _dict['tags']
        # TAG: defines the kind node of the child.
        _tag = _child.tag
        # ATTRIB: this is a dictionary defining attributes for the node.
        _dict = _child.attrib 
        # CHILDREN: it is the content of the node.
        _list = []

        _type = self.get_type(_child)
        if _tags.__contains__('type'):
            _type = _tags['type'] + [_type]
        else:
            _type = [_type]
        _newtags = self.get_tags( _child, _dict)
        for _key in _newtags.keys():
            _tags.update({_key : _newtags[_key] })
        _tags.update( {'type': _type})

        if _type[-1] == 'spaces':
            _tmp = self.get_spaces( _child )
            _list.append( {'txt' : _tmp, 'tags' : _tags })
        elif _type[-1] == 'tabs':
            _tmp = self.get_tabs( _child )
            _list.append( {'txt' : _tmp, 'tags' : _tags })

        if _child.text != None:
            _list.append( {'txt' : _child.text, 'tags' : _tags })

        _nesting = _tags['nesting'] + 1
        _tags.update({'nesting' : _nesting })
        for _i in _child.iter():
            if _i != _child:
                _childtype = self.get_type(_child)
                if _childtype != 'UNKNOWN': 
                    _list.append( {'child' : _i, 'tags' : _tags })
                else:
                    print 'WARNING: unkown: ', _child.tag
        if _child.tail != None:
            _nesting = _tags['nesting'] - 1
            _tags.update({'nesting' : _nesting })
            _list.append( {'txt' : _child.tail, 'tags' : _tags })
        
        return _list

    def get_tags(self, _child):
        """This function creates a tagged text
        """
        #print '\n\n>>>>>>> ', _child
        _dict = _child.attrib
        #print _dict
        _tags = {}
        # Style
        _tagstyle = '{%s}%s' % (self.ns['text'],'style-name')
        if _dict.__contains__(_tagstyle):
            _tagname = _child.attrib[_tagstyle]
            _tags.update({'style' : _tagname } )
        else:
            _tags.update({'style' : None} )

        # Level
        _taglevel = '{%s}%s' % (self.ns['text'],'outline-level')
        if _dict.__contains__(_taglevel):
            _level = _child.attrib[_taglevel]
            _tags.update({'level' : _level })
        else:
            _tags.update({'level' : None} )
        return _tags

    def get_type( self, _child ):
        """Gets the kind of child"""
        if self.types.__contains__(_child.tag):
            return self.types[_child.tag]
        else:
            return 'UNKNOWN'

    def getData(self, _child):
        """Process each child node.
        """
        _tag = _child.tag

        # Get text
        
        _list = []

        
        # Gets the text from frames.
        for _i in _child.getchildren():
            if is_frame(_i):
                _name = _i.attrib['{%s}%s' % (self.ns['draw'],'name')]
                if _name == None:
                    _name = u'FrameName'
                _tmp = self.__processFrame__(_i,_name)
                _list = _list + ['%s' % _tmp ]

        # Enumerations
        if _child.tag == '{%s}%s' % (self.ns['text'],'list'):
            _isEnumeration = True
            _mode = 'enumeration'
            _list = self.__processEnumeration__( _child)
        else:
            _isEnumeration = False

        # Table
        if self.is_table( _child.tag ):
            _list = self.getTable( _child )
            _mode = 'table'

        else:
            return (_level, _style, _list, _isHeading, _isParagraph, _mode, etree.tostring(_child, pretty_print=True) )

    def get_spaces( self, _child):
        """Deals with <text:s> which deals with extra spaces.
        """
        _tag = '{%s}%s' % (self.ns['text'],'c')
        if _child.attrib.__contains__(_tag):
            _value = _child.attrib[_tag]
            _tmp = ' ' * int(_value)
        else:
            _tmp = ' '
        return _tmp

    def get_tabs( self, _child):
        """Deals with <text:tab> which deals with extra spaces.
        """
        _tag = '{%s}%s' % (self.ns['text'],'tab')
        _tmp = '    '
        return _tmp

    def getTable(self,_xml):
        #<table:table-row>
        #  <table:table-cell>
        _list = []
        _rows = _xml.xpath('table:table-row',namespaces={'table':self.ns['table']})
        for _row in _rows:
            _cells = _row.xpath('table:table-cell',namespaces={'table':self.ns['table']})
            _row = []
            for _c in _cells:
                _tmp = []
                for _i in _c.getchildren():
                    if self.is_paragraph( _i ):
                        _tmp1 = []
                        for _j in _i.itertext():
                            if _j != None:
                                _tmp1 = _tmp1 + [ u'%s' % _j ]
                        _tmp = _tmp + _tmp1

                    elif self.is_enum( _i ):
                        _tmp1 = self.__processList__( _i )
                        _tmp = _tmp + _tmp1
                _row.append( _tmp )
            _list.append(_row)
        return _list

    def __processFrame__(self,_xml,_name):
        _output = u''
        #print 'ok'
        for _i in _xml.getchildren():
            if self.is_object_ole(_i ):
                _filename = _i.attrib['{%s}%s' % (self.ns['xlink'],'href')]
                _newfilename = self.__fileExtractor__( _filename )
                _output = _output + '[[file:%s][%s]]\n' % (_newfilename,_name)
            elif self.is_image(_i):
                # No hacer nada si la imagen es un Object Replacement
                _filename = _i.attrib['{%s}%s' % (self.ns['xlink'],'href')]
                if not _filename.__contains__('ObjectReplacements/'):
                    _newfilename = self.__fileExtractor__( _filename )
                    _output = _output + '[[file:%s][%s]]\n' % (_newfilename,_name)          
        return _output
    
    def __fileExtractor__(self,_filename):
        '''Extract the file an assigns a proper filename to it'''
        # All paths are relatives. We take away the beginning.
        if _filename[0:2] == './':
            _tmp = _filename[2:]
        else:
            _tmp = _filename
        
        _outdir = os.path.split( self.orgfile.filename)[0]

        # The file is extracted.
        try:
           self.files.extract(_tmp, _outdir)
        except AttributeError:
           print "ERROR: ZipFile provides 'extract' function in Python 2.6." 
           print "       Please, consider updating to PYTHON v2.6"
           raise AttributeError("Zipfile instance has no attribute 'extract'")
        _extractedfile = os.path.join(_outdir,_filename)

        # Is an OLE object.
        _toRename = False
        _ole = None
        if ole.isOleFile(_extractedfile):
            _ole = Ole( filename = _extractedfile )
            _new = _ole.extractFile()
            _ole.__close__()
            _extractedfile = _new
            _toRename = True
        
        if _toRename:
            _extractedfile = os.path.realpath(_extractedfile)
            _tmp = os.path.split(_new)
            _tmpname = _tmp[1]
            _tmpname = _tmpname.replace(' ','_')
            _new = os.path.join(_tmp[0],_tmpname )
        
            if self.isOverWriter:
                os.remove( _new )
            try:
                os.rename(_extractedfile,_new)
            except WindowsError:
                print 'ERROR: file already exists: %s' % _new
            _extractedfile = _new

        # Los ficheros se extraen a sus rutas originales
        # Crea directorios si fuera necesario.
        # - Movemos los ficheros al mismo directorio que el .org.
        # - Usamos como prefijo el nombre del fichero.
        _tmp = os.path.split( _extractedfile )
        _newname = self.prefix + _tmp[1]
        _fullnewname = os.path.join(_outdir,_newname)
        
        if os.path.isfile( _fullnewname ) and self.isOverWriter:
            os.remove( _fullnewname )  # The preexisting file is removed.
        elif os.path.isfile( _fullnewname ) and not self.isOverWriter:
            print 'WARNING - The file already exists.'
            print '  - FILENAME: %s' % _fullnewname
            print '  - Keeping both files'
            print '  - Referencing to the old one'
        else:
            pass
        
        # We try to move the file changing its name. 
        try:
            os.rename(_extractedfile,_fullnewname)
            return _newname
        except:   
            return _filename

#=================================0
class Ole:
    def __init__(self,filename=None):
        self.filename = filename
        self._data = ole.OleFileIO(self.filename)
        self.files = self.__getFiles__()
        self._dict = { '.*AcroExch\.Document\.[0-9]+.*' : { 'ext':'.pdf','name':'CONTENTS'},
                '.*Word\.Document\.[0-9]+.*' : { 'ext':'.doc','name': None},
                '.*Excel\.Sheet\.[0-9]+.*'   : { 'ext':'.xls','name': None},
                '.*PowerPoint\.Show\.[0-9]+.*'   : { 'ext':'.ppt','name': None},
                       }
    
    def __guessFormat__(self):
        _data = self.__getItem__(0)
        #print _data
        _KEY = None
        for _key in self._dict.keys():
            _kernel = re.compile(  _key )
            _tmp = _kernel.findall( _data )
            if len(_tmp) == 1:
                _KEY = _key
        return _KEY

    def __getFiles__(self):
        return self._data.listdir()
    
    def __getItem__(self,_idx):
        _tmp = self._data.openstream( self.files[_idx] )
        _data = _tmp.read()
        return _data
        
    def __extractFile__(self,_idx):
        # Leemos el fichero
        _data = self.__getItem__(_idx)
        _fp = open('filaneme.txt','wb')
        _fp.write( _data )
        _fp.close()
    
    def extractFile(self):
        _key = self.__guessFormat__()
        if _key == None:
            _tmp = self._data.openstream( self.files[0] )
            _kernel = re.compile('[a-zA-Z]+\.[a-zA-Z]+\.[0-9]+')
            _tmp = _tmp.read()
            print 'ERROR: It is not configured for: %s' % _kernel.findall( _tmp )[0]
            #print self.files
            #print _tmp
            return None
        else:
            if self._dict[_key]['name'] != None:
                _tmp = self._data.openstream( self._dict[_key]['name'] )
                _file = os.path.splitext(self.filename)
                _ext = self._dict[ _key ]['ext']
                _filename = _file[0] + _ext
                #_tmp = self.outfile +_ext
                #_tmp = os.path.join( self.outdir, _tmp)
                _fp = open( _filename, 'wb')
                _fp.write( _tmp.read() )
                _fp.close()
    #            print self.filename
                del(_tmp)
                self._data.fp.close()
                os.remove(self.filename)
                return _filename
            else:
                # Símplemente se renombra
                #print self.files
                #print self.__getItem__(7)
                _file = os.path.splitext(self.filename)
                _ext = self._dict[ _key ]['ext']
                _filename = _file[0] + _ext                
                self._data.fp.close()
                try:
                    os.rename(self.filename,_filename)
                except WindowsError:
                    print 'WARNING: the file already exists: %s' % _filename
                return _filename

    def __close__(self):
        self._data.fp.close()

class FileError(Exception):
    def __init__(self, 
                 message,
                 filename ):
        self.filename = filename
        self.message = message

    def __str__(self):
        return u'%s: %s' % ( self.message, self.filename )



if __name__ == '__main__':
    pass