ods2odt.py 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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):
  19. #
  20. # This script converts a spreadsheet to a text file. I.e. it copies
  21. # the sheets and turns them into tables in the textfile
  22. # Note: Copy of images does not work?
  23. #
  24. import sys, getopt
  25. from odf.opendocument import OpenDocumentText, load
  26. from odf.table import Table
  27. from odf.text import P
  28. def usage():
  29. sys.stderr.write("Usage: %s [-o outputfile] inputfile\n" % sys.argv[0])
  30. if __name__ == "__main__":
  31. try:
  32. opts, args = getopt.getopt(sys.argv[1:], "o:", ["output="])
  33. except getopt.GetoptError:
  34. usage()
  35. sys.exit(2)
  36. outputfile = None
  37. for o, a in opts:
  38. if o in ("-o", "--output"):
  39. outputfile = a
  40. if len(args) != 1:
  41. usage()
  42. sys.exit(2)
  43. inputfile = args[0]
  44. if outputfile is None:
  45. outputfile = inputfile[:inputfile.rfind('.')] + ".odt"
  46. spreadsheetdoc = load(inputfile)
  47. textdoc = OpenDocumentText()
  48. # Need to make a copy of the list because addElement unlinks from the original
  49. for meta in spreadsheetdoc.meta.childNodes[:]:
  50. textdoc.meta.addElement(meta)
  51. for font in spreadsheetdoc.fontfacedecls.childNodes[:]:
  52. textdoc.fontfacedecls.addElement(font)
  53. for style in spreadsheetdoc.styles.childNodes[:]:
  54. textdoc.styles.addElement(style)
  55. for autostyle in spreadsheetdoc.automaticstyles.childNodes[:]:
  56. textdoc.automaticstyles.addElement(autostyle)
  57. for sheet in spreadsheetdoc.getElementsByType(Table):
  58. textdoc.text.addElement(sheet)
  59. textdoc.text.addElement(P())
  60. textdoc.Pictures = spreadsheetdoc.Pictures
  61. textdoc.save(outputfile)