odf2mht 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #!/usr/bin/python3
  2. # -*- coding: utf-8 -*-
  3. # Copyright (C) 2006 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. from __future__ import print_function
  21. from odf.odf2xhtml import ODF2XHTML
  22. import zipfile
  23. import sys
  24. #from time import gmtime, strftime
  25. from email.mime.multipart import MIMEMultipart
  26. from email.mime.nonmultipart import MIMENonMultipart
  27. from email.mime.text import MIMEText
  28. from email import encoders
  29. if sys.version_info[0]==3: unicode=str
  30. if len(sys.argv) != 2:
  31. sys.stderr.write("Usage: %s inputfile\n" % sys.argv[0])
  32. sys.exit(1)
  33. suffices = {
  34. 'wmf':('image','x-wmf'),
  35. 'png':('image','png'),
  36. 'gif':('image','gif'),
  37. 'jpg':('image','jpeg'),
  38. 'jpeg':('image','jpeg')
  39. }
  40. msg = MIMEMultipart('related',type="text/html")
  41. # msg['Subject'] = 'Subject here'
  42. # msg['From'] = '<Saved by ODT2MHT>'
  43. # msg['Date'] = strftime("%a, %d %b %Y %H:%M:%S +0000", gmtime())
  44. msg.preamble = 'This is a multi-part message in MIME format.'
  45. msg.epilogue = ''
  46. odhandler = ODF2XHTML()
  47. result = odhandler.odf2xhtml(unicode(sys.argv[1]))
  48. htmlpart = MIMEText(result,'html','us-ascii')
  49. htmlpart['Content-Location'] = 'index.html'
  50. msg.attach(htmlpart)
  51. z = zipfile.ZipFile(sys.argv[1])
  52. for file in z.namelist():
  53. if file[0:9] == 'Pictures/':
  54. suffix = file[file.rfind(".")+1:]
  55. main,sub = suffices.get(suffix,('application','octet-stream'))
  56. img = MIMENonMultipart(main,sub)
  57. img.set_payload(z.read(file))
  58. img['Content-Location'] = "" + file
  59. encoders.encode_base64(img)
  60. msg.attach(img)
  61. z.close()
  62. print (msg.as_string())
  63. # Local Variables: ***
  64. # mode: python ***
  65. # End: ***