diff options
author | Cédric Bosdonnat <cedricbosdo@openoffice.org> | 2010-06-17 19:03:49 +0200 |
---|---|---|
committer | Thorsten Behrens <tbehrens@novell.com> | 2010-06-17 22:37:41 +0200 |
commit | 3808d880ab7ceb0ed618d2e3960cb40e31fdb10f (patch) | |
tree | ce80e929d1262257a3494e0696371910bdc66232 | |
parent | a42dcb45c32a2488990cae7bf0aa55f755b7d7eb (diff) |
XLSX sample
-rw-r--r-- | examples/ooxml-strict/demo_refmode.py | 11 | ||||
-rw-r--r-- | examples/ooxml-strict/test-refmode.xlsx | bin | 0 -> 8266 bytes | |||
-rw-r--r-- | examples/ooxml-strict/xls_sample.py | 53 |
3 files changed, 64 insertions, 0 deletions
diff --git a/examples/ooxml-strict/demo_refmode.py b/examples/ooxml-strict/demo_refmode.py new file mode 100644 index 0000000..2776a99 --- /dev/null +++ b/examples/ooxml-strict/demo_refmode.py @@ -0,0 +1,11 @@ +import re +from opc import ALL_OOXML as ALL_OOXML + +# use default list of xml mimetypes in opc +mimetypes = ALL_OOXML + +# always iterate twice over a matched element +iterations = lambda i: 1 + +# always use the same regexp regardless of mimetype +worklist = lambda i: [re.compile('.*CT_CalcPr'), re.compile('.*refMode.*')] diff --git a/examples/ooxml-strict/test-refmode.xlsx b/examples/ooxml-strict/test-refmode.xlsx Binary files differnew file mode 100644 index 0000000..f39e0d9 --- /dev/null +++ b/examples/ooxml-strict/test-refmode.xlsx diff --git a/examples/ooxml-strict/xls_sample.py b/examples/ooxml-strict/xls_sample.py new file mode 100644 index 0000000..b0be89d --- /dev/null +++ b/examples/ooxml-strict/xls_sample.py @@ -0,0 +1,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 |