summaryrefslogtreecommitdiff
path: root/examples/ooxml-strict/xls_sample.py
blob: b0be89dd2f88bc4c5a0199a0c2c5f3790d8d3e06 (plain)
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
# Copyright 2010, Thorsten Behrens, Novell Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain a
# copy of the License at:
#
#            http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

import sys, os, StringIO

import pyxb.binding.saxer

import opc
import dml.dml, pml.pml, props.props, sml.sml, wml.wml

if len(sys.argv) < 3:
    print "Usage: xls_sample.py <worklist.py> <input_file> <output_dir>"
    sys.exit(1)
else:    
    exec "import "+sys.argv[1]+" as worklist"
    inFile = sys.argv[2]
    (inFileName,inFileExt) = os.path.splitext(os.path.basename(inFile))
    outDir = sys.argv[3]
    
    package = opc.OPCPackage(inFile)
    iteration=1
    
    for (fragment, mimetype, schema, reltype) in package.files(worklist.mimetypes):
        if fragment == "xl/workbook.xml":
            saxer = pyxb.binding.saxer.make_parser(location_base=fragment)
            handler = saxer.getContentHandler()
            saxer.parse(StringIO.StringIO(package.read(fragment)))
            sax_instance = handler.rootObject()
            
    
            for contentIter in sax_instance.iterateBinding(worklist.worklist(mimetype)):
                # iterate content n times
                for i in range(worklist.iterations(mimetype)):
                    contentIter()
                    currOutFile = outDir+"/"+inFileName+str(iteration)+inFileExt
                    if os.system("cp "+inFile+" "+currOutFile) == 0:
                        outPackage = opc.OPCPackage(currOutFile,"a")
                        print sax_instance.toxml()
                        outPackage.writestr(fragment,sax_instance.toxml())
                        outPackage.close()
                        iteration += 1
                        print "Written "+currOutFile