You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

67 lines
1.7 KiB

4 years ago
  1. #!/home/alpcentaur/ProjektA/PrototypeWebApp/venv/bin/python3.5
  2. # $Id: rst2odt_prepstyles.py 5839 2009-01-07 19:09:28Z dkuhlman $
  3. # Author: Dave Kuhlman <dkuhlman@rexx.com>
  4. # Copyright: This module has been placed in the public domain.
  5. """
  6. Fix a word-processor-generated styles.odt for odtwriter use: Drop page size
  7. specifications from styles.xml in STYLE_FILE.odt.
  8. """
  9. #
  10. # Author: Michael Schutte <michi@uiae.at>
  11. from lxml import etree
  12. import sys
  13. import zipfile
  14. from tempfile import mkstemp
  15. import shutil
  16. import os
  17. NAMESPACES = {
  18. "style": "urn:oasis:names:tc:opendocument:xmlns:style:1.0",
  19. "fo": "urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0"
  20. }
  21. def prepstyle(filename):
  22. zin = zipfile.ZipFile(filename)
  23. styles = zin.read("styles.xml")
  24. root = etree.fromstring(styles)
  25. for el in root.xpath("//style:page-layout-properties",
  26. namespaces=NAMESPACES):
  27. for attr in el.attrib:
  28. if attr.startswith("{%s}" % NAMESPACES["fo"]):
  29. del el.attrib[attr]
  30. tempname = mkstemp()
  31. zout = zipfile.ZipFile(os.fdopen(tempname[0], "w"), "w",
  32. zipfile.ZIP_DEFLATED)
  33. for item in zin.infolist():
  34. if item.filename == "styles.xml":
  35. zout.writestr(item, etree.tostring(root))
  36. else:
  37. zout.writestr(item, zin.read(item.filename))
  38. zout.close()
  39. zin.close()
  40. shutil.move(tempname[1], filename)
  41. def main():
  42. args = sys.argv[1:]
  43. if len(args) != 1:
  44. print >> sys.stderr, __doc__
  45. print >> sys.stderr, "Usage: %s STYLE_FILE.odt\n" % sys.argv[0]
  46. sys.exit(1)
  47. filename = args[0]
  48. prepstyle(filename)
  49. if __name__ == '__main__':
  50. main()
  51. # vim:tw=78:sw=4:sts=4:et: