comparison env/bin/rst2odt_prepstyles.py @ 0:4f3585e2f14b draft default tip

"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
author shellac
date Mon, 22 Mar 2021 18:12:50 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:4f3585e2f14b
1 #!/Users/cmdms/OneDrive-UOB/Development/Projects/2021/sam-consensus-v3/env/bin/python3
2
3 # $Id: rst2odt_prepstyles.py 8346 2019-08-26 12:11:32Z milde $
4 # Author: Dave Kuhlman <dkuhlman@rexx.com>
5 # Copyright: This module has been placed in the public domain.
6
7 """
8 Fix a word-processor-generated styles.odt for odtwriter use: Drop page size
9 specifications from styles.xml in STYLE_FILE.odt.
10 """
11
12 # Author: Michael Schutte <michi@uiae.at>
13
14 from __future__ import print_function
15
16 from lxml import etree
17 import sys
18 import zipfile
19 from tempfile import mkstemp
20 import shutil
21 import os
22
23 NAMESPACES = {
24 "style": "urn:oasis:names:tc:opendocument:xmlns:style:1.0",
25 "fo": "urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0"
26 }
27
28
29 def prepstyle(filename):
30
31 zin = zipfile.ZipFile(filename)
32 styles = zin.read("styles.xml")
33
34 root = etree.fromstring(styles)
35 for el in root.xpath("//style:page-layout-properties",
36 namespaces=NAMESPACES):
37 for attr in el.attrib:
38 if attr.startswith("{%s}" % NAMESPACES["fo"]):
39 del el.attrib[attr]
40
41 tempname = mkstemp()
42 zout = zipfile.ZipFile(os.fdopen(tempname[0], "w"), "w",
43 zipfile.ZIP_DEFLATED)
44
45 for item in zin.infolist():
46 if item.filename == "styles.xml":
47 zout.writestr(item, etree.tostring(root))
48 else:
49 zout.writestr(item, zin.read(item.filename))
50
51 zout.close()
52 zin.close()
53 shutil.move(tempname[1], filename)
54
55
56 def main():
57 args = sys.argv[1:]
58 if len(args) != 1:
59 print(__doc__, file=sys.stderr)
60 print("Usage: %s STYLE_FILE.odt\n" % sys.argv[0], file=sys.stderr)
61 sys.exit(1)
62 filename = args[0]
63 prepstyle(filename)
64
65
66 if __name__ == '__main__':
67 main()