odtmerge.py 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # Copyright (C) 2008 Søren Roug, European Environment Agency
  4. #
  5. # This is free software. You may redistribute it under the terms
  6. # of the Apache license and the GNU General Public License Version
  7. # 2 or at your option any later version.
  8. #
  9. # This program 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
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public
  15. # License along with this program; if not, write to the Free Software
  16. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. #
  18. # Contributor(s): Ramiro Batista da Luz
  19. #
  20. # Inspired by ods2odt.py
  21. #
  22. import sys, getopt
  23. import zipfile, xml.dom.minidom
  24. from odf.opendocument import OpenDocumentText, load
  25. from odf.element import Text
  26. from odf.text import P
  27. def usage():
  28. sys.stderr.write("Usage: %s -o outputfile inputfile [inputfile2 inputfile3 ...]\n" % sys.argv[0])
  29. def merge(inputfile, textdoc):
  30. inputtextdoc = load(inputfile)
  31. # Need to make a copy of the list because addElement unlinks from the original
  32. for meta in inputtextdoc.meta.childNodes[:]:
  33. textdoc.meta.addElement(meta)
  34. for font in inputtextdoc.fontfacedecls.childNodes[:]:
  35. textdoc.fontfacedecls.addElement(font)
  36. for style in inputtextdoc.styles.childNodes[:]:
  37. textdoc.styles.addElement(style)
  38. for autostyle in inputtextdoc.automaticstyles.childNodes[:]:
  39. textdoc.automaticstyles.addElement(autostyle)
  40. for scripts in inputtextdoc.scripts.childNodes[:]:
  41. textdoc.scripts.addElement(scripts)
  42. for settings in inputtextdoc.settings.childNodes[:]:
  43. textdoc.settings.addElement(settings)
  44. for masterstyles in inputtextdoc.masterstyles.childNodes[:]:
  45. textdoc.masterstyles.addElement(masterstyles)
  46. for body in inputtextdoc.body.childNodes[:]:
  47. textdoc.body.addElement(body)
  48. textdoc.Pictures.update(inputtextdoc.Pictures)
  49. return textdoc
  50. if __name__ == "__main__":
  51. try:
  52. opts, args = getopt.getopt(sys.argv[1:], "o:", ["output="])
  53. except getopt.GetoptError:
  54. usage()
  55. sys.exit(2)
  56. outputfile = None
  57. for o, a in opts:
  58. if o in ("-o", "--output"):
  59. outputfile = a
  60. if outputfile is None:
  61. usage()
  62. sys.exit(2)
  63. if len(args) < 2:
  64. usage()
  65. sys.exit(2)
  66. inputfiles = args[1:]
  67. textdoc = OpenDocumentText()
  68. for inputfile in inputfiles:
  69. textdoc = merge(inputfile, textdoc)
  70. textdoc.save(outputfile)