ODFPY  1.2.0
opendocument.py
Go to the documentation of this file.
1 # -*- coding: utf-8 -*-
2 # Copyright (C) 2006-2010 Søren Roug, European Environment Agency
3 #
4 # This library is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU Lesser General Public
6 # License as published by the Free Software Foundation; either
7 # version 2.1 of the License, or (at your option) any later version.
8 #
9 # This library is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 # Lesser General Public License for more details.
13 #
14 # You should have received a copy of the GNU Lesser General Public
15 # License along with this library; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 #
18 # Contributor(s):
19 #
20 # Copyright (C) 2014 Georges Khaznadar <georgesk@debian.org>
21 # migration to Python3, JavaDOC comments and automatic
22 # build of documentation
23 #
24 
25 __doc__="""Use OpenDocument to generate your documents."""
26 
27 import zipfile, time, uuid, sys, mimetypes, copy, os.path
28 
29 # to allow Python3 to access modules in the same path
30 sys.path.append(os.path.dirname(__file__))
31 
32 # using BytesIO provides a cleaner interface than StringIO
33 # with both Python2 and Python3: the programmer must care to
34 # convert strings or unicode to bytes, which is valid for Python 2 and 3.
35 from io import StringIO, BytesIO
36 
37 from odf.namespaces import *
38 import odf.manifest as manifest
39 import odf.meta as meta
40 from odf.office import *
41 import odf.element as element
42 from odf.attrconverters import make_NCName
43 from xml.sax.xmlreader import InputSource
44 from odf.odfmanifest import manifestlist
45 
46 if sys.version_info[0] == 3:
47  unicode=str # unicode function does not exist
48 
49 __version__= TOOLSVERSION
50 
51 _XMLPROLOGUE = u"<?xml version='1.0' encoding='UTF-8'?>\n"
52 
53 
61 UNIXPERMS = 2175008768
62 
63 IS_FILENAME = 0
64 IS_IMAGE = 1
65 # We need at least Python 2.2
66 assert sys.version_info[0]>=2 and sys.version_info[1] >= 2
67 
68 #sys.setrecursionlimit(100)
69 #The recursion limit is set conservative so mistakes like
70 # s=content() s.addElement(s) won't eat up too much processor time.
71 
72 
75 odmimetypes = {
76  u'application/vnd.oasis.opendocument.text': u'.odt',
77  u'application/vnd.oasis.opendocument.text-template': u'.ott',
78  u'application/vnd.oasis.opendocument.graphics': u'.odg',
79  u'application/vnd.oasis.opendocument.graphics-template': u'.otg',
80  u'application/vnd.oasis.opendocument.presentation': u'.odp',
81  u'application/vnd.oasis.opendocument.presentation-template': u'.otp',
82  u'application/vnd.oasis.opendocument.spreadsheet': u'.ods',
83  u'application/vnd.oasis.opendocument.spreadsheet-template': u'.ots',
84  u'application/vnd.oasis.opendocument.chart': u'.odc',
85  u'application/vnd.oasis.opendocument.chart-template': u'.otc',
86  u'application/vnd.oasis.opendocument.image': u'.odi',
87  u'application/vnd.oasis.opendocument.image-template': u'.oti',
88  u'application/vnd.oasis.opendocument.formula': u'.odf',
89  u'application/vnd.oasis.opendocument.formula-template': u'.otf',
90  u'application/vnd.oasis.opendocument.text-master': u'.odm',
91  u'application/vnd.oasis.opendocument.text-web': u'.oth',
92 }
93 
94 
99 
106  def __init__(self, filename, mediatype, content=None):
107  assert(type(filename)==type(u""))
108  assert(type(mediatype)==type(u""))
109  assert(type(content)==type(b"") or content == None)
110 
111  self.mediatype = mediatype
112  self.filename = filename
113  self.content = content
114 
115 
123  thumbnail = None
124 
125 
131  def __init__(self, mimetype, add_generator=True):
132  assert(type(mimetype)==type(u""))
133  assert(isinstance(add_generator,True.__class__))
134 
135  self.mimetype = mimetype
136  self.childobjects = []
137  self._extra = []
138  self.folder = u"" # Always empty for toplevel documents
139  self.topnode = Document(mimetype=self.mimetype)
140  self.topnode.ownerDocument = self
141 
142  self.clear_caches()
143 
144  self.Pictures = {}
145  self.meta = Meta()
146  self.topnode.addElement(self.meta)
147  if add_generator:
148  self.meta.addElement(meta.Generator(text=TOOLSVERSION))
149  self.scripts = Scripts()
150  self.topnode.addElement(self.scripts)
152  self.topnode.addElement(self.fontfacedecls)
154  self.topnode.addElement(self.settings)
155  self.styles = Styles()
156  self.topnode.addElement(self.styles)
158  self.topnode.addElement(self.automaticstyles)
160  self.topnode.addElement(self.masterstyles)
161  self.body = Body()
162  self.topnode.addElement(self.body)
163 
164  def rebuild_caches(self, node=None):
165  if node is None: node = self.topnode
166  self.build_caches(node)
167  for e in node.childNodes:
168  if e.nodeType == element.Node.ELEMENT_NODE:
169  self.rebuild_caches(e)
170 
171 
175  def clear_caches(self):
176  self.element_dict = {}
177  self._styles_dict = {}
178  self._styles_ooo_fix = {}
179 
180 
185  def build_caches(self, elt):
186  # assert(isinstance(elt, element.Element))
187  # why do I need this more intricated assertion?
188  # with Python3, the type of elt pops out as odf.element.Element
189  # in one test ???
190  import odf.element
191  assert(isinstance(elt, element.Element) or isinstance(elt, odf.element.Element) )
192 
193  if elt.qname not in self.element_dict:
194  self.element_dict[elt.qname] = []
195  self.element_dict[elt.qname].append(elt)
196  if elt.qname == (STYLENS, u'style'):
197  self.__register_stylename(elt) # Add to style dictionary
198  styleref = elt.getAttrNS(TEXTNS,u'style-name')
199  if styleref is not None and styleref in self._styles_ooo_fix:
200  elt.setAttrNS(TEXTNS,u'style-name', self._styles_ooo_fix[styleref])
201 
202 
209  def __register_stylename(self, elt):
210  assert(isinstance(elt, element.Element))
211 
212  name = elt.getAttrNS(STYLENS, u'name')
213  if name is None:
214  return
215  if elt.parentNode.qname in ((OFFICENS,u'styles'), (OFFICENS,u'automatic-styles')):
216  if name in self._styles_dict:
217  newname = u'M'+name # Rename style
218  self._styles_ooo_fix[name] = newname
219  # From here on all references to the old name will refer to the new one
220  name = newname
221  elt.setAttrNS(STYLENS, u'name', name)
222  self._styles_dict[name] = elt
223 
224 
234  def toXml(self, filename=u''):
235  assert(type(filename)==type(u""))
236 
237  result=None
238  xml=StringIO()
239  if sys.version_info[0]==2:
240  xml.write(_XMLPROLOGUE)
241  else:
242  xml.write(_XMLPROLOGUE)
243  self.body.toXml(0, xml)
244  if not filename:
245  result=xml.getvalue()
246  else:
247  f=codecs.open(filename,'w', encoding='utf-8')
248  f.write(xml.getvalue())
249  f.close()
250  return result
251 
252 
257  def xml(self):
258  self.__replaceGenerator()
259  xml=StringIO()
260  if sys.version_info[0]==2:
261  xml.write(_XMLPROLOGUE)
262  else:
263  xml.write(_XMLPROLOGUE)
264  self.topnode.toXml(0, xml)
265  return xml.getvalue().encode("utf-8")
266 
267 
268 
273  def contentxml(self):
274  xml=StringIO()
275  xml.write(_XMLPROLOGUE)
276  x = DocumentContent()
277  x.write_open_tag(0, xml)
278  if self.scripts.hasChildNodes():
279  self.scripts.toXml(1, xml)
280  if self.fontfacedecls.hasChildNodes():
281  self.fontfacedecls.toXml(1, xml)
282  a = AutomaticStyles()
283  stylelist = self._used_auto_styles([self.styles, self.automaticstyles, self.body])
284  if len(stylelist) > 0:
285  a.write_open_tag(1, xml)
286  for s in stylelist:
287  s.toXml(2, xml)
288  a.write_close_tag(1, xml)
289  else:
290  a.toXml(1, xml)
291  self.body.toXml(1, xml)
292  x.write_close_tag(0, xml)
293  return xml.getvalue().encode("utf-8")
294 
295 
301  def __manifestxml(self):
302  xml=StringIO()
303  xml.write(_XMLPROLOGUE)
304  self.manifest.toXml(0,xml)
305  result=xml.getvalue()
306  assert(type(result)==type(u""))
307  return result
308 
309 
314  def metaxml(self):
315  self.__replaceGenerator()
316  x = DocumentMeta()
317  x.addElement(self.meta)
318  xml=StringIO()
319  xml.write(_XMLPROLOGUE)
320  x.toXml(0,xml)
321  result=xml.getvalue()
322  assert(type(result)==type(u""))
323  return result
324 
325 
330  def settingsxml(self):
331  x = DocumentSettings()
332  x.addElement(self.settings)
333  xml=StringIO()
334  if sys.version_info[0]==2:
335  xml.write(_XMLPROLOGUE)
336  else:
337  xml.write(_XMLPROLOGUE)
338  x.toXml(0,xml)
339  result=xml.getvalue()
340  assert(type(result)==type(u""))
341  return result
342 
343 
350  def _parseoneelement(self, top, stylenamelist):
351  for e in top.childNodes:
352  if e.nodeType == element.Node.ELEMENT_NODE:
353  for styleref in (
354  (CHARTNS,u'style-name'),
355  (DRAWNS,u'style-name'),
356  (DRAWNS,u'text-style-name'),
357  (PRESENTATIONNS,u'style-name'),
358  (STYLENS,u'data-style-name'),
359  (STYLENS,u'list-style-name'),
360  (STYLENS,u'page-layout-name'),
361  (STYLENS,u'style-name'),
362  (TABLENS,u'default-cell-style-name'),
363  (TABLENS,u'style-name'),
364  (TEXTNS,u'style-name') ):
365  if e.getAttrNS(styleref[0],styleref[1]):
366  stylename = e.getAttrNS(styleref[0],styleref[1])
367  if stylename not in stylenamelist:
368  # due to the polymorphism of e.getAttrNS(),
369  # a unicode type is enforced for elements
370  stylenamelist.append(unicode(stylename))
371  stylenamelist = self._parseoneelement(e, stylenamelist)
372  return stylenamelist
373 
374 
381  def _used_auto_styles(self, segments):
382  stylenamelist = []
383  for top in segments:
384  stylenamelist = self._parseoneelement(top, stylenamelist)
385  stylelist = []
386  for e in self.automaticstyles.childNodes:
387  if e.getAttrNS(STYLENS,u'name') in stylenamelist:
388  stylelist.append(e)
389 
390  # check the type of the returned data
391  ok=True
392  for e in stylelist: ok = ok and isinstance(e, element.Element)
393  assert(ok)
394 
395  return stylelist
396 
397 
402  def stylesxml(self):
403  xml=StringIO()
404  xml.write(_XMLPROLOGUE)
405  x = DocumentStyles()
406  x.write_open_tag(0, xml)
407  if self.fontfacedecls.hasChildNodes():
408  self.fontfacedecls.toXml(1, xml)
409  self.styles.toXml(1, xml)
410  a = AutomaticStyles()
411  a.write_open_tag(1, xml)
412  for s in self._used_auto_styles([self.masterstyles]):
413  s.toXml(2, xml)
414  a.write_close_tag(1, xml)
415  if self.masterstyles.hasChildNodes():
416  self.masterstyles.toXml(1, xml)
417  x.write_close_tag(0, xml)
418  result = xml.getvalue()
419 
420  assert(type(result)==type(u""))
421 
422  return result
423 
424 
436  def addPicture(self, filename, mediatype=None, content=None):
437  if content is None:
438  if mediatype is None:
439  mediatype, encoding = mimetypes.guess_type(filename)
440  if mediatype is None:
441  mediatype = u''
442  try: ext = filename[filename.rindex(u'.'):]
443  except: ext=u''
444  else:
445  ext = mimetypes.guess_extension(mediatype)
446  manifestfn = u"Pictures/%s%s" % (uuid.uuid4().hex.upper(), ext)
447  self.Pictures[manifestfn] = (IS_FILENAME, filename, mediatype)
448  content=b"" # this value is only use by the assert further
449  filename=u"" # this value is only use by the assert further
450  else:
451  manifestfn = filename
452  self.Pictures[manifestfn] = (IS_IMAGE, content, mediatype)
453 
454  assert(type(filename)==type(u""))
455  assert(type(content) == type(b""))
456 
457  return manifestfn
458 
459 
470  def addPictureFromFile(self, filename, mediatype=None):
471  if mediatype is None:
472  mediatype, encoding = mimetypes.guess_type(filename)
473  if mediatype is None:
474  mediatype = u''
475  try: ext = filename[filename.rindex(u'.'):]
476  except ValueError: ext=u''
477  else:
478  ext = mimetypes.guess_extension(mediatype)
479  manifestfn = u"Pictures/%s%s" % (uuid.uuid4().hex.upper(), ext)
480  self.Pictures[manifestfn] = (IS_FILENAME, filename, mediatype)
481 
482  assert(type(filename)==type(u""))
483  assert(type(mediatype)==type(u""))
484 
485  return manifestfn
486 
487 
498  def addPictureFromString(self, content, mediatype):
499  assert(type(content)==type(b""))
500  assert(type(mediatype)==type(u""))
501 
502  ext = mimetypes.guess_extension(mediatype)
503  manifestfn = u"Pictures/%s%s" % (uuid.uuid4().hex.upper(), ext)
504  self.Pictures[manifestfn] = (IS_IMAGE, content, mediatype)
505  return manifestfn
506 
507 
513  def addThumbnail(self, filecontent=None):
514  assert(type(filecontent)==type(b""))
515 
516  if filecontent is None:
517  import thumbnail
518  self.thumbnail = thumbnail.thumbnail()
519  else:
520  self.thumbnail = filecontent
521 
522 
530  def addObject(self, document, objectname=None):
531  assert(isinstance(document, OpenDocument))
532  assert(type(objectname)==type(u"") or objectname == None)
533 
534  self.childobjects.append(document)
535  if objectname is None:
536  document.folder = u"%s/Object %d" % (self.folder, len(self.childobjects))
537  else:
538  document.folder = objectname
539  return u".%s" % document.folder
540 
541 
547  def _savePictures(self, anObject, folder):
548  assert(isinstance(anObject, OpenDocument))
549  assert(type(folder)==type(u""))
550 
551  hasPictures = False
552  for arcname, picturerec in anObject.Pictures.items():
553  what_it_is, fileobj, mediatype = picturerec
554  self.manifest.addElement(manifest.FileEntry(fullpath=u"%s%s" % ( folder ,arcname), mediatype=mediatype))
555  hasPictures = True
556  if what_it_is == IS_FILENAME:
557  self._z.write(fileobj, arcname, zipfile.ZIP_STORED)
558  else:
559  zi = zipfile.ZipInfo(str(arcname), self._now)
560  zi.compress_type = zipfile.ZIP_STORED
561  zi.external_attr = UNIXPERMS
562  self._z.writestr(zi, fileobj)
563  # According to section 17.7.3 in ODF 1.1, the pictures folder should not have a manifest entry
564 # if hasPictures:
565 # self.manifest.addElement(manifest.FileEntry(fullpath="%sPictures/" % folder, mediatype=""))
566  # Look in subobjects
567  subobjectnum = 1
568  for subobject in anObject.childobjects:
569  self._savePictures(subobject, u'%sObject %d/' % (folder, subobjectnum))
570  subobjectnum += 1
571 
572 
579  def __replaceGenerator(self):
580  for m in self.meta.childNodes[:]:
581  if m.qname == (METANS, u'generator'):
582  self.meta.removeChild(m)
583  self.meta.addElement(meta.Generator(text=TOOLSVERSION))
584 
585 
594  def save(self, outputfile, addsuffix=False):
595 
596  if outputfile == u'-':
597  outputfp = zipfile.ZipFile(sys.stdout,"w")
598  else:
599  if addsuffix:
600  outputfile = outputfile + odmimetypes.get(self.mimetype,u'.xxx')
601  outputfp = zipfile.ZipFile(outputfile, "w")
602  self.__zipwrite(outputfp)
603  outputfp.close()
604 
605 
611  def write(self, outputfp):
612  zipoutputfp = zipfile.ZipFile(outputfp,"w")
613  self.__zipwrite(zipoutputfp)
614 
615 
621  def __zipwrite(self, outputfp):
622  assert(isinstance(outputfp, zipfile.ZipFile))
623 
624  self._z = outputfp
625  self._now = time.localtime()[:6]
626  self.manifest = manifest.Manifest()
627 
628  # Write mimetype
629  zi = zipfile.ZipInfo('mimetype', self._now)
630  zi.compress_type = zipfile.ZIP_STORED
631  zi.external_attr = UNIXPERMS
632  self._z.writestr(zi, self.mimetype.encode("utf-8"))
633 
634  self._saveXmlObjects(self,u"")
635 
636  # Write pictures
637  self._savePictures(self,u"")
638 
639  # Write the thumbnail
640  if self.thumbnail is not None:
641  self.manifest.addElement(manifest.FileEntry(fullpath=u"Thumbnails/", mediatype=u''))
642  self.manifest.addElement(manifest.FileEntry(fullpath=u"Thumbnails/thumbnail.png", mediatype=u''))
643  zi = zipfile.ZipInfo(u"Thumbnails/thumbnail.png", self._now)
644  zi.compress_type = zipfile.ZIP_DEFLATED
645  zi.external_attr = UNIXPERMS
646  self._z.writestr(zi, self.thumbnail)
647 
648  # Write any extra files
649  for op in self._extra:
650  if op.filename == u"META-INF/documentsignatures.xml": continue # Don't save signatures
651  self.manifest.addElement(manifest.FileEntry(fullpath=op.filename, mediatype=op.mediatype))
652  if sys.version_info[0]==3:
653  zi = zipfile.ZipInfo(op.filename, self._now)
654  else:
655  zi = zipfile.ZipInfo(op.filename.encode('utf-8'), self._now)
656  zi.compress_type = zipfile.ZIP_DEFLATED
657  zi.external_attr = UNIXPERMS
658  if op.content is not None:
659  self._z.writestr(zi, op.content)
660  # Write manifest
661  zi = zipfile.ZipInfo(u"META-INF/manifest.xml", self._now)
662  zi.compress_type = zipfile.ZIP_DEFLATED
663  zi.external_attr = UNIXPERMS
664  self._z.writestr(zi, self.__manifestxml() )
665  del self._z
666  del self._now
667  del self.manifest
668 
669 
670 
676  def _saveXmlObjects(self, anObject, folder):
677  assert(isinstance(anObject, OpenDocument))
678  assert(type(folder)==type(u""))
679 
680  if self == anObject:
681  self.manifest.addElement(manifest.FileEntry(fullpath=u"/", mediatype=anObject.mimetype))
682  else:
683  self.manifest.addElement(manifest.FileEntry(fullpath=folder, mediatype=anObject.mimetype))
684  # Write styles
685  self.manifest.addElement(manifest.FileEntry(fullpath=u"%sstyles.xml" % folder, mediatype=u"text/xml"))
686  zi = zipfile.ZipInfo(u"%sstyles.xml" % folder, self._now)
687  zi.compress_type = zipfile.ZIP_DEFLATED
688  zi.external_attr = UNIXPERMS
689  self._z.writestr(zi, anObject.stylesxml().encode("utf-8") )
690 
691  # Write content
692  self.manifest.addElement(manifest.FileEntry(fullpath=u"%scontent.xml" % folder, mediatype=u"text/xml"))
693  zi = zipfile.ZipInfo(u"%scontent.xml" % folder, self._now)
694  zi.compress_type = zipfile.ZIP_DEFLATED
695  zi.external_attr = UNIXPERMS
696  self._z.writestr(zi, anObject.contentxml() )
697 
698  # Write settings
699  if anObject.settings.hasChildNodes():
700  self.manifest.addElement(manifest.FileEntry(fullpath=u"%ssettings.xml" % folder, mediatype=u"text/xml"))
701  zi = zipfile.ZipInfo(u"%ssettings.xml" % folder, self._now)
702  zi.compress_type = zipfile.ZIP_DEFLATED
703  zi.external_attr = UNIXPERMS
704  self._z.writestr(zi, anObject.settingsxml() )
705 
706  # Write meta
707  if self == anObject:
708  self.manifest.addElement(manifest.FileEntry(fullpath=u"meta.xml", mediatype=u"text/xml"))
709  zi = zipfile.ZipInfo(u"meta.xml", self._now)
710  zi.compress_type = zipfile.ZIP_DEFLATED
711  zi.external_attr = UNIXPERMS
712  self._z.writestr(zi, anObject.metaxml() )
713 
714  # Write subobjects
715  subobjectnum = 1
716  for subobject in anObject.childobjects:
717  self._saveXmlObjects(subobject, u'%sObject %d/' % (folder, subobjectnum))
718  subobjectnum += 1
719 
720 # Document's DOM methods
721 
728  def createElement(self, elt):
729  assert(isinstance(elt, element.Element))
730 
731  # this old code is ambiguous: is 'element' the module or is it the
732  # local variable? To disambiguate this, the local variable has been
733  # renamed to 'elt'
734  #return element(check_grammar=False)
735  return elt(check_grammar=False)
736 
737 
743  def createTextNode(self, data):
744  assert(type(data)==type(u""))
745 
746  return element.Text(data)
747 
748 
754  def createCDATASection(self, data):
755  assert(type(data)==type(u""))
756 
757  return element.CDATASection(cdata)
758 
759 
764  def getMediaType(self):
765  assert (type(self.mimetype)==type(u""))
766 
767  return self.mimetype
768 
769 
775  def getStyleByName(self, name):
776  assert(type(name)==type(u""))
777 
778  ncname = make_NCName(name)
779  if self._styles_dict == {}:
780  self.rebuild_caches()
781  result=self._styles_dict.get(ncname, None)
782 
783  assert(isinstance(result, element.Element))
784  return result
785 
786 
793  def getElementsByType(self, elt):
794  import types
795  assert(isinstance (elt, types.FunctionType))
796 
797  obj = elt(check_grammar=False)
798  assert (isinstance(obj, element.Element))
799 
800  if self.element_dict == {}:
801  self.rebuild_caches()
802 
803  # This previous code was ambiguous
804  # was "element" the module name or the local variable?
805  # the local variable is renamed to "elt" to disambiguate the code
806  #return self.element_dict.get(obj.qname, [])
807 
808  result=self.element_dict.get(obj.qname, [])
809 
810  ok=True
811  for e in result: ok = ok and isinstance(e, element.Element)
812  assert(ok)
813 
814  return result
815 
816 # Convenience functions
817 
823  doc = OpenDocument(u'application/vnd.oasis.opendocument.chart')
824  doc.chart = Chart()
825  doc.body.addElement(doc.chart)
826  return doc
827 
828 
834  doc = OpenDocument(u'application/vnd.oasis.opendocument.graphics')
835  doc.drawing = Drawing()
836  doc.body.addElement(doc.drawing)
837  return doc
838 
839 
845  doc = OpenDocument(u'application/vnd.oasis.opendocument.image')
846  doc.image = Image()
847  doc.body.addElement(doc.image)
848  return doc
849 
850 
856  doc = OpenDocument(u'application/vnd.oasis.opendocument.presentation')
857  doc.presentation = Presentation()
858  doc.body.addElement(doc.presentation)
859  return doc
860 
861 
867  doc = OpenDocument(u'application/vnd.oasis.opendocument.spreadsheet')
868  doc.spreadsheet = Spreadsheet()
869  doc.body.addElement(doc.spreadsheet)
870  return doc
871 
872 
878  doc = OpenDocument(u'application/vnd.oasis.opendocument.text')
879  doc.text = Text()
880  doc.body.addElement(doc.text)
881  return doc
882 
883 
889  doc = OpenDocument(u'application/vnd.oasis.opendocument.text-master')
890  doc.text = Text()
891  doc.body.addElement(doc.text)
892  return doc
893 
894 
902 def __loadxmlparts(z, manifest, doc, objectpath):
903  assert(isinstance(z, zipfile.ZipFile))
904  assert(type(manifest)==type(dict()))
905  assert(isinstance(doc, OpenDocument))
906  assert(type(objectpath)==type(u""))
907 
908  from load import LoadParser
909  from xml.sax import make_parser, handler
910 
911  for xmlfile in (objectpath+u'settings.xml', objectpath+u'meta.xml', objectpath+u'content.xml', objectpath+u'styles.xml'):
912  if xmlfile not in manifest:
913  continue
914 
917  from xml.sax._exceptions import SAXParseException
918 
919  try:
920  xmlpart = z.read(xmlfile).decode("utf-8")
921  doc._parsing = xmlfile
922 
923  parser = make_parser()
924  parser.setFeature(handler.feature_namespaces, 1)
925  parser.setContentHandler(LoadParser(doc))
926  parser.setErrorHandler(handler.ErrorHandler())
927 
928  inpsrc = InputSource()
929 
934  xmlpart=__fixXmlPart(xmlpart)
935 
936  inpsrc.setByteStream(BytesIO(xmlpart.encode("utf-8")))
937  parser.parse(inpsrc)
938  del doc._parsing
939  except KeyError as v: pass
940  except SAXParseException:
941  print (u"====== SAX FAILED TO PARSE ==========\n", xmlpart)
942 
943 
951 def __fixXmlPart(xmlpart):
952  result=xmlpart
953  requestedPrefixes = (u'meta', u'config', u'dc', u'style',
954  u'svg', u'fo',u'draw', u'table',u'form')
955  for prefix in requestedPrefixes:
956  if u' xmlns:{prefix}'.format(prefix=prefix) not in xmlpart:
957 
963  try:
964  pos=result.index(u" xmlns:")
965  toInsert=u' xmlns:{prefix}="urn:oasis:names:tc:opendocument:xmlns:{prefix}:1.0"'.format(prefix=prefix)
966  result=result[:pos]+toInsert+result[pos:]
967  except:
968  pass
969  return result
970 
971 
972 
979 def __detectmimetype(zipfd, odffile):
980  assert(isinstance(zipfd, zipfile.ZipFile))
981 
982  try:
983  mimetype = zipfd.read('mimetype').decode("utf-8")
984  return mimetype
985  except:
986  pass
987  # Fall-through to next mechanism
988  manifestpart = zipfd.read('META-INF/manifest.xml')
989  manifest = manifestlist(manifestpart)
990  for mentry,mvalue in manifest.items():
991  if mentry == "/":
992  assert(type(mvalue['media-type'])==type(u""))
993  return mvalue['media-type']
994 
995  # Fall-through to last mechanism
996  return u'application/vnd.oasis.opendocument.text'
997 
998 
1005 def load(odffile):
1006  z = zipfile.ZipFile(odffile)
1007  mimetype = __detectmimetype(z, odffile)
1008  doc = OpenDocument(mimetype, add_generator=False)
1009 
1010  # Look in the manifest file to see if which of the four files there are
1011  manifestpart = z.read('META-INF/manifest.xml')
1012  manifest = manifestlist(manifestpart)
1013  __loadxmlparts(z, manifest, doc, u'')
1014  for mentry,mvalue in manifest.items():
1015  if mentry[:9] == u"Pictures/" and len(mentry) > 9:
1016  doc.addPicture(mvalue['full-path'], mvalue['media-type'], z.read(mentry))
1017  elif mentry == u"Thumbnails/thumbnail.png":
1018  doc.addThumbnail(z.read(mentry))
1019  elif mentry in (u'settings.xml', u'meta.xml', u'content.xml', u'styles.xml'):
1020  pass
1021  # Load subobjects into structure
1022  elif mentry[:7] == u"Object " and len(mentry) < 11 and mentry[-1] == u"/":
1023  subdoc = OpenDocument(mvalue['media-type'], add_generator=False)
1024  doc.addObject(subdoc, u"/" + mentry[:-1])
1025  __loadxmlparts(z, manifest, subdoc, mentry)
1026  elif mentry[:7] == u"Object ":
1027  pass # Don't load subobjects as opaque objects
1028  else:
1029  if mvalue['full-path'][-1] == u'/':
1030  doc._extra.append(OpaqueObject(mvalue['full-path'], mvalue['media-type'], None))
1031  else:
1032  doc._extra.append(OpaqueObject(mvalue['full-path'], mvalue['media-type'], z.read(mentry)))
1033  # Add the SUN junk here to the struct somewhere
1034  # It is cached data, so it can be out-of-date
1035  z.close()
1036  b = doc.getElementsByType(Body)
1037  if mimetype[:39] == u'application/vnd.oasis.opendocument.text':
1038  doc.text = b[0].firstChild
1039  elif mimetype[:43] == u'application/vnd.oasis.opendocument.graphics':
1040  doc.graphics = b[0].firstChild
1041  elif mimetype[:47] == u'application/vnd.oasis.opendocument.presentation':
1042  doc.presentation = b[0].firstChild
1043  elif mimetype[:46] == u'application/vnd.oasis.opendocument.spreadsheet':
1044  doc.spreadsheet = b[0].firstChild
1045  elif mimetype[:40] == u'application/vnd.oasis.opendocument.chart':
1046  doc.chart = b[0].firstChild
1047  elif mimetype[:40] == u'application/vnd.oasis.opendocument.image':
1048  doc.image = b[0].firstChild
1049  elif mimetype[:42] == u'application/vnd.oasis.opendocument.formula':
1050  doc.formula = b[0].firstChild
1051 
1052  return doc
1053 
1054 # vim: set expandtab sw=4 :
def addObject(self, document, objectname=None)
Adds an object (subdocument).
def FontFaceDecls(args)
Definition: office.py:71
just a record to bear a filename, a mediatype and a bytes content
Definition: opendocument.py:98
def DocumentSettings(version="1.2", args)
Definition: office.py:59
def OpenDocumentDrawing()
Creates a drawing document.
def createTextNode(self, data)
Method to create a text node.
def Image(args)
Definition: draw.py:125
def Document(version="1.2", args)
Definition: office.py:50
A class to hold the content of an OpenDocument document Use the xml method to write the XML source to...
def OpenDocumentSpreadsheet()
Creates a spreadsheet document.
def addPicture(self, filename, mediatype=None, content=None)
Add a picture It uses the same convention as OOo, in that it saves the picture in the zipfile in the ...
def Presentation(args)
Definition: office.py:86
def toXml(self, filename=u'')
converts the document to a valid Xml format.
def OpenDocumentPresentation()
Creates a presentation document.
def MasterStyles(args)
Definition: office.py:80
def metaxml(self)
Generates the meta.xml file.
def contentxml(self)
Generates the content.xml file.
def _saveXmlObjects(self, anObject, folder)
save xml objects of an opendocument to some folder
def addPictureFromString(self, content, mediatype)
Add a picture from contents given as a Byte string.
def __zipwrite(self, outputfp)
Write the document to an open file pointer This is where the real work is done.
def getStyleByName(self, name)
Finds a style object based on the name.
def __register_stylename(self, elt)
Register a style.
def rebuild_caches(self, node=None)
def AutomaticStyles(args)
Definition: office.py:32
def stylesxml(self)
Generates the styles.xml file.
def OpenDocumentImage()
Creates an image document.
def addThumbnail(self, filecontent=None)
Add a fixed thumbnail The thumbnail in the library is big, so this is pretty useless.
def save(self, outputfile, addsuffix=False)
Save the document under the filename.
def load(odffile)
Load an ODF file into memory.
def clear_caches(self)
Clears internal caches.
def DocumentStyles(version="1.2", args)
Definition: office.py:62
def DocumentMeta(version="1.2", args)
Definition: office.py:56
def Chart(args)
Definition: chart.py:31
def __manifestxml(self)
Generates the manifest.xml file; The self.manifest isn&#39;t avaible unless the document is being saved...
def write(self, outputfp)
User API to write the ODF file to an open file descriptor Writes the ZIP format.
def addPictureFromFile(self, filename, mediatype=None)
Add a picture It uses the same convention as OOo, in that it saves the picture in the zipfile in the ...
def build_caches(self, elt)
Builds internal caches; called from element.py.
def OpenDocumentText()
Creates a text document.
def OpenDocumentTextMaster()
Creates a text master document.
def getMediaType(self)
Returns the media type.
def _savePictures(self, anObject, folder)
saves pictures contained in an object
def OpenDocumentChart()
Creates a chart document.
def __replaceGenerator(self)
Removes a previous &#39;generator&#39; stance and declares TOOLSVERSION as the new generator.
def Settings(args)
Definition: office.py:95
def manifestlist(manifestxml)
Definition: odfmanifest.py:95
def Drawing(args)
Definition: office.py:65
def Styles(args)
Definition: office.py:101
def __init__(self, filename, mediatype, content=None)
the constructor
def _used_auto_styles(self, segments)
Loop through the masterstyles elements, and find the automatic styles that are used.
def _parseoneelement(self, top, stylenamelist)
Finds references to style objects in master-styles and add the style name to the style list if not al...
Definition: meta.py:1
def createCDATASection(self, data)
Method to create a CDATA section.
def xml(self)
Generates the full document as an XML "file".
Creates a arbitrary element and is intended to be subclassed not used on its own. ...
Definition: element.py:299
def Spreadsheet(args)
Definition: office.py:98
def Text(args)
Definition: form.py:104
def createElement(self, elt)
Inconvenient interface to create an element, but follows XML-DOM.
def __init__(self, mimetype, add_generator=True)
the constructor
def Scripts(args)
Definition: office.py:92
def Body(args)
Definition: office.py:38
def getElementsByType(self, elt)
Gets elements based on the type, which is function from text.py, draw.py etc.
def Meta(args)
Definition: office.py:83
def settingsxml(self)
Generates the settings.xml file.
def DocumentContent(version="1.2", args)
Definition: office.py:53