diff options
author | Thorsten Behrens <tbehrens@novell.com> | 2010-06-05 00:54:14 +0200 |
---|---|---|
committer | Thorsten Behrens <tbehrens@novell.com> | 2010-06-05 00:54:14 +0200 |
commit | 538372e2884c8eb2a2e19ac128e906ff6fba2cb6 (patch) | |
tree | bf996ce05dd183127f878f8671296a0872da6f6c | |
parent | 7c1d177f3a28751f964810a2cc1a71565490acd7 (diff) |
Cleanup, split out opc module, added license etc
-rw-r--r-- | examples/ooxml-strict/dumpsample.py | 373 | ||||
-rw-r--r-- | examples/ooxml-strict/opc.py | 316 |
2 files changed, 352 insertions, 337 deletions
diff --git a/examples/ooxml-strict/dumpsample.py b/examples/ooxml-strict/dumpsample.py index 87f9b88..36b5fc4 100644 --- a/examples/ooxml-strict/dumpsample.py +++ b/examples/ooxml-strict/dumpsample.py @@ -1,348 +1,47 @@ +# 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 re import sys -import xml.sax -import pyxb.utils.domutils as domutils -import pyxb.utils.saxdom +import StringIO + import pyxb.binding.saxer -import re +import opc import dml.dml import pml.pml import props.props import sml.sml import wml.wml -import os -import zipfile -import StringIO -from exceptions import TypeError -import xml.etree.ElementTree as ElemTree - -# 3-tuples of media type, root schema uri, and relationship type uri -# (contains all xml-ish file types of ooxml) -xml_type_map = [ - ("application/vnd.openxmlformats-officedocument.wordprocessingml.comments+xml", - "http://schemas.openxmlformats.org/wordprocessingml/2006/main", - "http://schemas.openxmlformats.org/officeDocument/2006/relationships/comments"), - - ("application/vnd.openxmlformats-officedocument.wordprocessingml.settings+xml", - "http://schemas.openxmlformats.org/wordprocessingml/2006/main", - "http://schemas.openxmlformats.org/officeDocument/2006/relationships/settings"), - - ("application/vnd.openxmlformats-officedocument.wordprocessingml.endnotes+xml", - "http://schemas.openxmlformats.org/wordprocessingml/2006/main", - "http://schemas.openxmlformats.org/officeDocument/2006/relationships/endnotes"), - - ("application/vnd.openxmlformats-officedocument.wordprocessingml.fontTable+xml", - "http://schemas.openxmlformats.org/wordprocessingml/2006/main", - "http://schemas.openxmlformats.org/officeDocument/2006/relationships/fontTable"), - - ("application/vnd.openxmlformats-officedocument.wordprocessingml.footer+xml", - "http://schemas.openxmlformats.org/wordprocessingml/2006/main", - "http://schemas.openxmlformats.org/officeDocument/2006/relationships/footer"), - - ("application/vnd.openxmlformats-officedocument.wordprocessingml.footnotes+xml", - "http://schemas.openxmlformats.org/wordprocessingml/2006/main", - "http://schemas.openxmlformats.org/officeDocument/2006/relationships/footnotes"), - - ("application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml", - "http://schemas.openxmlformats.org/wordprocessingml/2006/main", - "http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument"), - - ("application/vnd.openxmlformats-officedocument.wordprocessingml.document.glossary+xml", - "http://schemas.openxmlformats.org/wordprocessingml/2006/main", - "http://schemas.openxmlformats.org/officeDocument/2006/relationships/glossaryDocument"), - - ("application/vnd.openxmlformats-officedocument.wordprocessingml.header+xml", - "http://schemas.openxmlformats.org/wordprocessingml/2006/main", - "http://schemas.openxmlformats.org/officeDocument/2006/relationships/header"), - - ("application/vnd.openxmlformats-officedocument.wordprocessingml.numbering+xml", - "http://schemas.openxmlformats.org/wordprocessingml/2006/main", - "http://schemas.openxmlformats.org/officeDocument/2006/relationships/numbering"), - - ("application/vnd.openxmlformats-officedocument.wordprocessingml.styles+xml", - "http://schemas.openxmlformats.org/wordprocessingml/2006/main", - "http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles"), - - ("application/vnd.openxmlformats-officedocument.wordprocessingml.webSettings+xml", - "http://schemas.openxmlformats.org/wordprocessingml/2006/main", - "http://schemas.openxmlformats.org/officeDocument/2006/relationships/webSettings"), - - ("application/vnd.openxmlformats-officedocument.spreadsheetml.calcChain+xml", - "http://schemas.openxmlformats.org/spreadsheetml/2006/main", - "http://schemas.openxmlformats.org/officeDocument/2006/relationships/calcChain"), - - ("application/vnd.openxmlformats-officedocument.spreadsheetml.chartsheet+xml", - "http://schemas.openxmlformats.org/spreadsheetml/2006/main", - "http://schemas.openxmlformats.org/officeDocument/2006/relationships/chartsheet"), - - ("application/vnd.openxmlformats-officedocument.spreadsheetml.comments+xml", - "http://schemas.openxmlformats.org/spreadsheetml/2006/main", - "http://schemas.openxmlformats.org/officeDocument/2006/relationships/comments"), - - ("application/vnd.openxmlformats-officedocument.spreadsheetml.connections+xml", - "http://schemas.openxmlformats.org/spreadsheetml/2006/main", - "http://schemas.openxmlformats.org/officeDocument/2006/relationships/connections"), - - ("application/xml", - "http://schemas.openxmlformats.org/spreadsheetml/2006/main", - "http://schemas.openxmlformats.org/officeDocument/2006/relationships/xmlMaps"), - - ("application/vnd.openxmlformats-officedocument.spreadsheetml.dialogsheet+xml", - "http://schemas.openxmlformats.org/spreadsheetml/2006/main", - "http://schemas.openxmlformats.org/officeDocument/2006/relationships/dialogsheet"), - - ("application/vnd.openxmlformats-officedocument.drawing+xml", - "http://schemas.openxmlformats.org/drawingml/2006/spreadsheetDrawing", - "http://schemas.openxmlformats.org/officeDocument/2006/relationships/drawing"), - - ("application/vnd.openxmlformats-officedocument.spreadsheetml.externalLink+xml", - "http://schemas.openxmlformats.org/spreadsheetml/2006/main", - "http://schemas.openxmlformats.org/officeDocument/2006/relationships/externalLink"), - - ("application/vnd.openxmlformats-officedocument.spreadsheetml.sheetMetadata+xml", - "http://schemas.openxmlformats.org/spreadsheetml/2006/main", - "http://schemas.openxmlformats.org/officeDocument/2006/relationships/sheetMetadata"), - - ("application/vnd.openxmlformats-officedocument.spreadsheetml.pivotTable+xml", - "http://schemas.openxmlformats.org/spreadsheetml/2006/main", - "http://schemas.openxmlformats.org/officeDocument/2006/relationships/pivotTable"), - - ("application/vnd.openxmlformats-officedocument.spreadsheetml.pivotCacheDefinition+xml", - "http://schemas.openxmlformats.org/spreadsheetml/2006/main", - "http://schemas.openxmlformats.org/officeDocument/2006/relationships/pivotCacheDefinition"), - - ("application/vnd.openxmlformats-officedocument.spreadsheetml.pivotCacheRecords+xml", - "http://schemas.openxmlformats.org/spreadsheetml/2006/main", - "http://schemas.openxmlformats.org/officeDocument/2006/relationships/"), - - ("application/vnd.openxmlformats-officedocument.spreadsheetml.queryTable+xml", - "http://schemas.openxmlformats.org/spreadsheetml/2006/main", - "http://schemas.openxmlformats.org/officeDocument/2006/relationships/queryTable"), - - ("application/vnd.openxmlformats-officedocument.spreadsheetml.sharedStrings+xml", - "http://schemas.openxmlformats.org/spreadsheetml/2006/main", - "http://schemas.openxmlformats.org/officeDocument/2006/relationships/sharedStrings"), - - ("application/vnd.openxmlformats-officedocument.spreadsheetml.revisionHeaders+xml", - "http://schemas.openxmlformats.org/spreadsheetml/2006/main", - "http://schemas.openxmlformats.org/officeDocument/2006/relationships/revisionHeaders"), - - ("application/vnd.openxmlformats-officedocument.spreadsheetml.revisionLog+xml", - "http://schemas.openxmlformats.org/spreadsheetml/2006/main", - "http://schemas.openxmlformats.org/officeDocument/2006/relationships/revisionLog"), - - ("application/vnd.openxmlformats-officedocument.spreadsheetml.userNames+xml", - "http://schemas.openxmlformats.org/spreadsheetml/2006/main", - "http://schemas.openxmlformats.org/officeDocument/2006/relationships/usernames"), - - ("application/vnd.openxmlformats-officedocument.spreadsheetml.tableSingleCells+xml", - "http://schemas.openxmlformats.org/spreadsheetml/2006/main", - "http://schemas.openxmlformats.org/officeDocument/2006/relationships/tableSingleCells"), - - ("application/vnd.openxmlformats-officedocument.spreadsheetml.styles+xml", - "http://schemas.openxmlformats.org/spreadsheetml/2006/mains", - "http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles"), - - ("application/vnd.openxmlformats-officedocument.spreadsheetml.table+xml", - "http://schemas.openxmlformats.org/spreadsheetml/2006/main", - "http://schemas.openxmlformats.org/officeDocument/2006/relationships/table"), - - ("application/vnd.openxmlformats-officedocument.spreadsheetml.volatileDependencies+xml", - "http://schemas.openxmlformats.org/spreadsheetml/2006/main", - "http://schemas.openxmlformats.org/officeDocument/2006/relationships/volatileDependencies"), - - ("application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml", - "http://schemas.openxmlformats.org/spreadsheetml/2006/main", - "http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet"), - - ("application/vnd.openxmlformats-officedocument.presentationml.commentAuthors+xml", - "http://schemas.openxmlformats.org/presentationml/2006/main", - "http://schemas.openxmlformats.org/officeDocument/2006/relationships/commentAuthors"), - - ("application/vnd.openxmlformats-officedocument.presentationml.comments+xml", - "http://schemas.openxmlformats.org/presentationml/2006/main", - "http://schemas.openxmlformats.org/officeDocument/2006/relationships/comments"), - - ("application/vnd.openxmlformats-officedocument.presentationml.handoutMaster+xml", - "http://schemas.openxmlformats.org/presentationml/2006/main", - "http://schemas.openxmlformats.org/officeDocument/2006/relationships/handoutMaster"), - - ("application/vnd.openxmlformats-officedocument.presentationml.notesMaster+xml", - "http://schemas.openxmlformats.org/presentationml/2006/main", - "http://schemas.openxmlformats.org/officeDocument/2006/relationships/notesMaster"), - - ("application/vnd.openxmlformats-officedocument.presentationml.notesSlide+xml", - "http://schemas.openxmlformats.org/presentationml/2006/main", - "http://schemas.openxmlformats.org/officeDocument/2006/relationships/notesSlide"), - - ("application/vnd.openxmlformats-officedocument.presentationml.presentation.main+xml", - "http://schemas.openxmlformats.org/presentationml/2006/main", - "http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument"), - - ("application/vnd.openxmlformats-officedocument.presentationml.presProps+xml", - "http://schemas.openxmlformats.org/presentationml/2006/main", - "http://schemas.openxmlformats.org/officeDocument/2006/relationships/presProps"), - - ("application/vnd.openxmlformats-officedocument.presentationml.slide+xml", - "http://schemas.openxmlformats.org/presentationml/2006/main", - "http://schemas.openxmlformats.org/officeDocument/2006/relationships/slide"), - - ("application/vnd.openxmlformats-officedocument.presentationml.slideLayout+xml", - "http://schemas.openxmlformats.org/presentationml/2006/main", - "http://schemas.openxmlformats.org/officeDocument/2006/relationships/slideLayout"), - - ("application/vnd.openxmlformats-officedocument.presentationml.slideMaster+xml", - "http://schemas.openxmlformats.org/presentationml/2006/main", - "http://schemas.openxmlformats.org/officeDocument/2006/relationships/slideMaster"), - - ("application/vnd.openxmlformats-officedocument.presentationml.slideUpdateInfo+xml", - "http://schemas.openxmlformats.org/presentationml/2006/main", - "http://schemas.openxmlformats.org/officeDocument/2006/relationships/slideUpdateInfo"), - - ("application/vnd.openxmlformats-officedocument.presentationml.viewProps+xml", - "http://schemas.openxmlformats.org/presentationml/2006/main", - "http://schemas.openxmlformats.org/officeDocument/2006/relationships/viewProps"), - - ("application/vnd.openxmlformats-officedocument.drawingml.chart+xml", - "http://schemas.openxmlformats.org/drawingml/2006/chart", - "http://schemas.openxmlformats.org/officeDocument/2006/relationships/chart"), - - ("application/vnd.openxmlformats-officedocument.drawingml.chartshapes+xml", - "http://schemas.openxmlformats.org/drawingml/2006/chart", - "http://schemas.openxmlformats.org/officeDocument/2006/relationships/chartUserShapes"), - - ("application/vnd.openxmlformats-officedocument.drawingml.diagramColors+xml", - "http://schemas.openxmlformats.org/drawingml/2006/diagram", - "http://schemas.openxmlformats.org/officeDocument/2006/relationships/diagramColors"), - - ("application/vnd.openxmlformats-officedocument.drawingml.diagramData+xml", - "http://schemas.openxmlformats.org/drawingml/2006/diagram", - "http://schemas.openxmlformats.org/officeDocument/2006/relationships/diagramData"), - - ("application/vnd.openxmlformats-officedocument.drawingml.diagramLayout+xml", - "http://schemas.openxmlformats.org/drawingml/2006/diagram", - "http://schemas.openxmlformats.org/officeDocument/2006/relationships/diagramLayout"), - - ("application/vnd.openxmlformats-officedocument.drawingml.diagramStyle+xml", - "http://schemas.openxmlformats.org/drawingml/2006/diagram", - "http://schemas.openxmlformats.org/officeDocument/2006/relationships/diagramQuickStyle"), - - ("application/vnd.openxmlformats-officedocument.theme+xml", - "http://schemas.openxmlformats.org/drawingml/2006/main", - "http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme"), - - ("application/vnd.openxmlformats-officedocument.themeOverride+xml", - "http://schemas.openxmlformats.org/drawingml/2006/main", - "http://schemas.openxmlformats.org/officeDocument/2006/relationships/themeOverride"), - - ("application/vnd.openxmlformats-officedocument.presentationml.tableStyles+xml", - "http://schemas.openxmlformats.org/drawingml/2006/main", - "http://schemas.openxmlformats.org/officeDocument/2006/relationships/tableStyles"), - - ("application/xml", - "http://schemas.openxmlformats.org/officeDocument/2006/additionalCharacteristics", - "http://schemas.openxmlformats.org/officeDocument/2006/relationships/customXml"), - - ("application/xml", - "http://schemas.openxmlformats.org/officeDocument/2006/bibliography", - "http://schemas.openxmlformats.org/officeDocument/2006/relationships/customXml"), - - ("application/vnd.openxmlformats-officedocument.customXmlProperties+xml", - "http://schemas.openxmlformats.org/officeDocument/2006/customXmlDataProps", - "http://schemas.openxmlformats.org/officeDocument/2006/relationships/customXmlProps"), - - ("application/vnd.openxmlformats-package.digital-signature-xmlsignature+xml", - "http://schemas.openxmlformats.org/package/2006/digital-signature", - "http://schemas.openxmlformats.org/package/2006/relationships/digital-signature/signature"), - -# no schema -# ("application/vnd.openxmlformats-package.core-properties+xml", -# "http://schemas.openxmlformats.org/package/2006/metadata/core-properties", -# "http://schemas.openxmlformats.org/officedocument/2006/relationships/metadata/core-properties"), - - ("application/vnd.openxmlformats-officedocument.custom-properties+xml", - "http://schemas.openxmlformats.org/officeDocument/2006/custom-properties", - "http://schemas.openxmlformats.org/officeDocument/2006/relationships/custom-properties"), - - ("application/vnd.openxmlformats-officedocument.extended-properties+xml", - "http://schemas.openxmlformats.org/officeDocument/2006/extended-properties", - "http://schemas.openxmlformats.org/officeDocument/2006/relationships/extended-properties") -] - -def createContentTypeMap (xml): - defaultMap={} - overrideMap={} - tree = ElemTree.XML(xml) - ns=lambda s: "{http://schemas.openxmlformats.org/package/2006/content-types}"+s - for elem in tree.findall(ns("Default")): - defaultMap[elem.get("Extension")] = elem.get("ContentType") - for elem in tree.findall(ns("Override")): - overrideMap[elem.get("PartName")] = elem.get("ContentType") - return (defaultMap, overrideMap) - -def validateType (content_types, path, type_str): - extension = os.path.splitext(path)[1] - if isinstance(type_str,str): - comp = lambda s,pat: s != pat - else: - # assume regular expression - comp = lambda s,pat: re.match(pat,s) != None - - if path in content_types[1]: - if comp(content_types[1][path], type_str): - raise TypeError("Wrong content type for %s" % path) - elif extension in content_types[0]: - if comp(content_types[0][extension], type_str): - raise TypeError("Wrong content type for %s" % path) - -def findContentType (content_types, path): - extension = os.path.splitext(path)[1] - if path in content_types[1]: - return content_types[1][path] - elif extension in content_types[0]: - return content_types[0][extension] - -def readFragment (package, content_types, path, type_str): - validateType(content_types, path, type_str) - return ElemTree.XML(package.read(path)) - -def readFragmentIfXml (package, content_types, path, type_map): - media_type = findContentType(content_types, "/"+path) - print "media type for fragment %s is %s" % (fragment, media_type) - for elem in type_map: - if elem[0] == media_type: - print "matching media type found, schema root is %s, relationship type uri is %s" % (elem[1], elem[2]) - saxer = pyxb.binding.saxer.make_parser(location_base=path) - handler = saxer.getContentHandler() - handler.__trace = True - saxer.parse(StringIO.StringIO(package.read(path))) - sax_instance = handler.rootObject() - - for i in sax_instance.iterateBinding([re.compile('.*CT_R'), re.compile('.*rsidR.*')]): - i() - print sax_instance.toxml() - print - i() - print sax_instance.toxml() - print - i() - print sax_instance.toxml() - print - - return None if len(sys.argv) > 1: - package = zipfile.ZipFile(sys.argv[1]) - - # populate media type maps - (defaultMap,overrideMap) = createContentTypeMap( - package.read("[Content_Types].xml")) - - # scan all contained files, load those matching one of xml_type_map's - # xml media types - for fragment in package.namelist(): - readFragmentIfXml(package, - (defaultMap,overrideMap), - fragment, - xml_type_map) + package = opc.OPCPackage(sys.argv[1]) + + for fragment in package.files(): + saxer = pyxb.binding.saxer.make_parser(location_base=fragment) + handler = saxer.getContentHandler() + saxer.parse(StringIO.StringIO(package.read(fragment))) + sax_instance = handler.rootObject() + + for i in sax_instance.iterateBinding([re.compile('.*CT_R'), re.compile('.*rsidR.*')]): + i() + print sax_instance.toxml() + print + i() + print sax_instance.toxml() + print + i() + print sax_instance.toxml() + print diff --git a/examples/ooxml-strict/opc.py b/examples/ooxml-strict/opc.py new file mode 100644 index 0000000..ba96ffa --- /dev/null +++ b/examples/ooxml-strict/opc.py @@ -0,0 +1,316 @@ +# 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 +import os +import zipfile +import xml.sax +import xml.etree.ElementTree as ElemTree + +# 3-tuples of media type, root schema uri, and relationship type uri +# (contains all xml-ish file types of ooxml) +xml_type_map = [ + ("application/vnd.openxmlformats-officedocument.wordprocessingml.comments+xml", + "http://schemas.openxmlformats.org/wordprocessingml/2006/main", + "http://schemas.openxmlformats.org/officeDocument/2006/relationships/comments"), + + ("application/vnd.openxmlformats-officedocument.wordprocessingml.settings+xml", + "http://schemas.openxmlformats.org/wordprocessingml/2006/main", + "http://schemas.openxmlformats.org/officeDocument/2006/relationships/settings"), + + ("application/vnd.openxmlformats-officedocument.wordprocessingml.endnotes+xml", + "http://schemas.openxmlformats.org/wordprocessingml/2006/main", + "http://schemas.openxmlformats.org/officeDocument/2006/relationships/endnotes"), + + ("application/vnd.openxmlformats-officedocument.wordprocessingml.fontTable+xml", + "http://schemas.openxmlformats.org/wordprocessingml/2006/main", + "http://schemas.openxmlformats.org/officeDocument/2006/relationships/fontTable"), + + ("application/vnd.openxmlformats-officedocument.wordprocessingml.footer+xml", + "http://schemas.openxmlformats.org/wordprocessingml/2006/main", + "http://schemas.openxmlformats.org/officeDocument/2006/relationships/footer"), + + ("application/vnd.openxmlformats-officedocument.wordprocessingml.footnotes+xml", + "http://schemas.openxmlformats.org/wordprocessingml/2006/main", + "http://schemas.openxmlformats.org/officeDocument/2006/relationships/footnotes"), + + ("application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml", + "http://schemas.openxmlformats.org/wordprocessingml/2006/main", + "http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument"), + + ("application/vnd.openxmlformats-officedocument.wordprocessingml.document.glossary+xml", + "http://schemas.openxmlformats.org/wordprocessingml/2006/main", + "http://schemas.openxmlformats.org/officeDocument/2006/relationships/glossaryDocument"), + + ("application/vnd.openxmlformats-officedocument.wordprocessingml.header+xml", + "http://schemas.openxmlformats.org/wordprocessingml/2006/main", + "http://schemas.openxmlformats.org/officeDocument/2006/relationships/header"), + + ("application/vnd.openxmlformats-officedocument.wordprocessingml.numbering+xml", + "http://schemas.openxmlformats.org/wordprocessingml/2006/main", + "http://schemas.openxmlformats.org/officeDocument/2006/relationships/numbering"), + + ("application/vnd.openxmlformats-officedocument.wordprocessingml.styles+xml", + "http://schemas.openxmlformats.org/wordprocessingml/2006/main", + "http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles"), + + ("application/vnd.openxmlformats-officedocument.wordprocessingml.webSettings+xml", + "http://schemas.openxmlformats.org/wordprocessingml/2006/main", + "http://schemas.openxmlformats.org/officeDocument/2006/relationships/webSettings"), + + ("application/vnd.openxmlformats-officedocument.spreadsheetml.calcChain+xml", + "http://schemas.openxmlformats.org/spreadsheetml/2006/main", + "http://schemas.openxmlformats.org/officeDocument/2006/relationships/calcChain"), + + ("application/vnd.openxmlformats-officedocument.spreadsheetml.chartsheet+xml", + "http://schemas.openxmlformats.org/spreadsheetml/2006/main", + "http://schemas.openxmlformats.org/officeDocument/2006/relationships/chartsheet"), + + ("application/vnd.openxmlformats-officedocument.spreadsheetml.comments+xml", + "http://schemas.openxmlformats.org/spreadsheetml/2006/main", + "http://schemas.openxmlformats.org/officeDocument/2006/relationships/comments"), + + ("application/vnd.openxmlformats-officedocument.spreadsheetml.connections+xml", + "http://schemas.openxmlformats.org/spreadsheetml/2006/main", + "http://schemas.openxmlformats.org/officeDocument/2006/relationships/connections"), + + ("application/xml", + "http://schemas.openxmlformats.org/spreadsheetml/2006/main", + "http://schemas.openxmlformats.org/officeDocument/2006/relationships/xmlMaps"), + + ("application/vnd.openxmlformats-officedocument.spreadsheetml.dialogsheet+xml", + "http://schemas.openxmlformats.org/spreadsheetml/2006/main", + "http://schemas.openxmlformats.org/officeDocument/2006/relationships/dialogsheet"), + + ("application/vnd.openxmlformats-officedocument.drawing+xml", + "http://schemas.openxmlformats.org/drawingml/2006/spreadsheetDrawing", + "http://schemas.openxmlformats.org/officeDocument/2006/relationships/drawing"), + + ("application/vnd.openxmlformats-officedocument.spreadsheetml.externalLink+xml", + "http://schemas.openxmlformats.org/spreadsheetml/2006/main", + "http://schemas.openxmlformats.org/officeDocument/2006/relationships/externalLink"), + + ("application/vnd.openxmlformats-officedocument.spreadsheetml.sheetMetadata+xml", + "http://schemas.openxmlformats.org/spreadsheetml/2006/main", + "http://schemas.openxmlformats.org/officeDocument/2006/relationships/sheetMetadata"), + + ("application/vnd.openxmlformats-officedocument.spreadsheetml.pivotTable+xml", + "http://schemas.openxmlformats.org/spreadsheetml/2006/main", + "http://schemas.openxmlformats.org/officeDocument/2006/relationships/pivotTable"), + + ("application/vnd.openxmlformats-officedocument.spreadsheetml.pivotCacheDefinition+xml", + "http://schemas.openxmlformats.org/spreadsheetml/2006/main", + "http://schemas.openxmlformats.org/officeDocument/2006/relationships/pivotCacheDefinition"), + + ("application/vnd.openxmlformats-officedocument.spreadsheetml.pivotCacheRecords+xml", + "http://schemas.openxmlformats.org/spreadsheetml/2006/main", + "http://schemas.openxmlformats.org/officeDocument/2006/relationships/"), + + ("application/vnd.openxmlformats-officedocument.spreadsheetml.queryTable+xml", + "http://schemas.openxmlformats.org/spreadsheetml/2006/main", + "http://schemas.openxmlformats.org/officeDocument/2006/relationships/queryTable"), + + ("application/vnd.openxmlformats-officedocument.spreadsheetml.sharedStrings+xml", + "http://schemas.openxmlformats.org/spreadsheetml/2006/main", + "http://schemas.openxmlformats.org/officeDocument/2006/relationships/sharedStrings"), + + ("application/vnd.openxmlformats-officedocument.spreadsheetml.revisionHeaders+xml", + "http://schemas.openxmlformats.org/spreadsheetml/2006/main", + "http://schemas.openxmlformats.org/officeDocument/2006/relationships/revisionHeaders"), + + ("application/vnd.openxmlformats-officedocument.spreadsheetml.revisionLog+xml", + "http://schemas.openxmlformats.org/spreadsheetml/2006/main", + "http://schemas.openxmlformats.org/officeDocument/2006/relationships/revisionLog"), + + ("application/vnd.openxmlformats-officedocument.spreadsheetml.userNames+xml", + "http://schemas.openxmlformats.org/spreadsheetml/2006/main", + "http://schemas.openxmlformats.org/officeDocument/2006/relationships/usernames"), + + ("application/vnd.openxmlformats-officedocument.spreadsheetml.tableSingleCells+xml", + "http://schemas.openxmlformats.org/spreadsheetml/2006/main", + "http://schemas.openxmlformats.org/officeDocument/2006/relationships/tableSingleCells"), + + ("application/vnd.openxmlformats-officedocument.spreadsheetml.styles+xml", + "http://schemas.openxmlformats.org/spreadsheetml/2006/mains", + "http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles"), + + ("application/vnd.openxmlformats-officedocument.spreadsheetml.table+xml", + "http://schemas.openxmlformats.org/spreadsheetml/2006/main", + "http://schemas.openxmlformats.org/officeDocument/2006/relationships/table"), + + ("application/vnd.openxmlformats-officedocument.spreadsheetml.volatileDependencies+xml", + "http://schemas.openxmlformats.org/spreadsheetml/2006/main", + "http://schemas.openxmlformats.org/officeDocument/2006/relationships/volatileDependencies"), + + ("application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml", + "http://schemas.openxmlformats.org/spreadsheetml/2006/main", + "http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet"), + + ("application/vnd.openxmlformats-officedocument.presentationml.commentAuthors+xml", + "http://schemas.openxmlformats.org/presentationml/2006/main", + "http://schemas.openxmlformats.org/officeDocument/2006/relationships/commentAuthors"), + + ("application/vnd.openxmlformats-officedocument.presentationml.comments+xml", + "http://schemas.openxmlformats.org/presentationml/2006/main", + "http://schemas.openxmlformats.org/officeDocument/2006/relationships/comments"), + + ("application/vnd.openxmlformats-officedocument.presentationml.handoutMaster+xml", + "http://schemas.openxmlformats.org/presentationml/2006/main", + "http://schemas.openxmlformats.org/officeDocument/2006/relationships/handoutMaster"), + + ("application/vnd.openxmlformats-officedocument.presentationml.notesMaster+xml", + "http://schemas.openxmlformats.org/presentationml/2006/main", + "http://schemas.openxmlformats.org/officeDocument/2006/relationships/notesMaster"), + + ("application/vnd.openxmlformats-officedocument.presentationml.notesSlide+xml", + "http://schemas.openxmlformats.org/presentationml/2006/main", + "http://schemas.openxmlformats.org/officeDocument/2006/relationships/notesSlide"), + + ("application/vnd.openxmlformats-officedocument.presentationml.presentation.main+xml", + "http://schemas.openxmlformats.org/presentationml/2006/main", + "http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument"), + + ("application/vnd.openxmlformats-officedocument.presentationml.presProps+xml", + "http://schemas.openxmlformats.org/presentationml/2006/main", + "http://schemas.openxmlformats.org/officeDocument/2006/relationships/presProps"), + + ("application/vnd.openxmlformats-officedocument.presentationml.slide+xml", + "http://schemas.openxmlformats.org/presentationml/2006/main", + "http://schemas.openxmlformats.org/officeDocument/2006/relationships/slide"), + + ("application/vnd.openxmlformats-officedocument.presentationml.slideLayout+xml", + "http://schemas.openxmlformats.org/presentationml/2006/main", + "http://schemas.openxmlformats.org/officeDocument/2006/relationships/slideLayout"), + + ("application/vnd.openxmlformats-officedocument.presentationml.slideMaster+xml", + "http://schemas.openxmlformats.org/presentationml/2006/main", + "http://schemas.openxmlformats.org/officeDocument/2006/relationships/slideMaster"), + + ("application/vnd.openxmlformats-officedocument.presentationml.slideUpdateInfo+xml", + "http://schemas.openxmlformats.org/presentationml/2006/main", + "http://schemas.openxmlformats.org/officeDocument/2006/relationships/slideUpdateInfo"), + + ("application/vnd.openxmlformats-officedocument.presentationml.viewProps+xml", + "http://schemas.openxmlformats.org/presentationml/2006/main", + "http://schemas.openxmlformats.org/officeDocument/2006/relationships/viewProps"), + + ("application/vnd.openxmlformats-officedocument.drawingml.chart+xml", + "http://schemas.openxmlformats.org/drawingml/2006/chart", + "http://schemas.openxmlformats.org/officeDocument/2006/relationships/chart"), + + ("application/vnd.openxmlformats-officedocument.drawingml.chartshapes+xml", + "http://schemas.openxmlformats.org/drawingml/2006/chart", + "http://schemas.openxmlformats.org/officeDocument/2006/relationships/chartUserShapes"), + + ("application/vnd.openxmlformats-officedocument.drawingml.diagramColors+xml", + "http://schemas.openxmlformats.org/drawingml/2006/diagram", + "http://schemas.openxmlformats.org/officeDocument/2006/relationships/diagramColors"), + + ("application/vnd.openxmlformats-officedocument.drawingml.diagramData+xml", + "http://schemas.openxmlformats.org/drawingml/2006/diagram", + "http://schemas.openxmlformats.org/officeDocument/2006/relationships/diagramData"), + + ("application/vnd.openxmlformats-officedocument.drawingml.diagramLayout+xml", + "http://schemas.openxmlformats.org/drawingml/2006/diagram", + "http://schemas.openxmlformats.org/officeDocument/2006/relationships/diagramLayout"), + + ("application/vnd.openxmlformats-officedocument.drawingml.diagramStyle+xml", + "http://schemas.openxmlformats.org/drawingml/2006/diagram", + "http://schemas.openxmlformats.org/officeDocument/2006/relationships/diagramQuickStyle"), + + ("application/vnd.openxmlformats-officedocument.theme+xml", + "http://schemas.openxmlformats.org/drawingml/2006/main", + "http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme"), + + ("application/vnd.openxmlformats-officedocument.themeOverride+xml", + "http://schemas.openxmlformats.org/drawingml/2006/main", + "http://schemas.openxmlformats.org/officeDocument/2006/relationships/themeOverride"), + + ("application/vnd.openxmlformats-officedocument.presentationml.tableStyles+xml", + "http://schemas.openxmlformats.org/drawingml/2006/main", + "http://schemas.openxmlformats.org/officeDocument/2006/relationships/tableStyles"), + + ("application/xml", + "http://schemas.openxmlformats.org/officeDocument/2006/additionalCharacteristics", + "http://schemas.openxmlformats.org/officeDocument/2006/relationships/customXml"), + + ("application/xml", + "http://schemas.openxmlformats.org/officeDocument/2006/bibliography", + "http://schemas.openxmlformats.org/officeDocument/2006/relationships/customXml"), + + ("application/vnd.openxmlformats-officedocument.customXmlProperties+xml", + "http://schemas.openxmlformats.org/officeDocument/2006/customXmlDataProps", + "http://schemas.openxmlformats.org/officeDocument/2006/relationships/customXmlProps"), + + ("application/vnd.openxmlformats-package.digital-signature-xmlsignature+xml", + "http://schemas.openxmlformats.org/package/2006/digital-signature", + "http://schemas.openxmlformats.org/package/2006/relationships/digital-signature/signature"), + + ("application/vnd.openxmlformats-officedocument.custom-properties+xml", + "http://schemas.openxmlformats.org/officeDocument/2006/custom-properties", + "http://schemas.openxmlformats.org/officeDocument/2006/relationships/custom-properties"), + + ("application/vnd.openxmlformats-officedocument.extended-properties+xml", + "http://schemas.openxmlformats.org/officeDocument/2006/extended-properties", + "http://schemas.openxmlformats.org/officeDocument/2006/relationships/extended-properties") +] + +def createContentTypeMap (xml): + defaultMap={} + overrideMap={} + tree = ElemTree.XML(xml) + ns=lambda s: "{http://schemas.openxmlformats.org/package/2006/content-types}"+s + for elem in tree.findall(ns("Default")): + defaultMap[elem.get("Extension")] = elem.get("ContentType") + for elem in tree.findall(ns("Override")): + overrideMap[elem.get("PartName")] = elem.get("ContentType") + return (defaultMap, overrideMap) + +class OPCPackage (zipfile.ZipFile): + """Specialization of ZipFile for operating on OPC packages. + + Class provides high-level functionality like <<give me all xml files of a + certain mimetype>>, and makes sure the OPC packaging constraints are + maintained. + """ + def __init__ (self, file, mode="r"): + zipfile.ZipFile.__init__(self, file, mode) + self.defaultMap, self.overrideMap = createContentTypeMap( + self.read("[Content_Types].xml")) + + def _findContentType (self, path): + """Lookup path in content type map, return mediatype""" + extension = os.path.splitext(path)[1] + if path in self.overrideMap: + return self.overrideMap[path] + elif extension in self.defaultMap: + return self.defaultMap[extension] + + def files (self, mimetypes=None): + """Iterator subsequently returning all files from the package, + that match the given list of mimetypes.""" + + if mimetypes is None: + mimetypes = xml_type_map + else: + mimetypes = [(i,"http://dummyschema/","http://dummyrelation/") for i in mimetype] + + # scan all contained files, yield those matching one of + # xml_type_map's xml media types + for fragment in self.namelist(): + mimetype = self._findContentType("/"+fragment) + if not mimetype is None: + for elem in mimetypes: + if elem[0] == mimetype: + yield fragment |