1# usage: runpy.sh fixtures/grid-snap/make.py <port> <out.odt> (office saving ODF 1.2 Extended) 2import sys, uno 3from unohelp import connect, desktop, pv 4 5port, out = int(sys.argv[1]), sys.argv[2] 6d = desktop(connect(port)) 7doc = d.loadComponentFromURL("private:factory/swriter", "_blank", 0, (pv("Hidden", True),)) 8ps = doc.StyleFamilies.getByName("PageStyles") 9specs = [("G_snap_on", True, 2), ("G_snap_off", False, 2), ("G_lines_off", False, 1)] 10for name, snap, mode in specs: 11 s = doc.createInstance("com.sun.star.style.PageStyle") 12 ps.insertByName(name, s) 13 s.GridMode = mode; s.StandardPageMode = True; s.GridSnapToChars = snap 14text = doc.Text 15cur = text.createTextCursor() 16PB = uno.getConstantByName("com.sun.star.text.ControlCharacter.PARAGRAPH_BREAK") 17for name, *_ in specs: 18 text.insertString(cur, "grid " + name, False) 19 text.insertControlCharacter(cur, PB, False) 20en = text.createEnumeration() 21for name, *_ in specs: 22 en.nextElement().PageDescName = name 23doc.storeToURL(uno.systemPathToFileUrl(out), (pv("FilterName", "writer8"),)) 24doc.close(True) 25 26# base-width suppresses standard-mode on export and the importer does not infer it, so add it by hand 27import zipfile, shutil 28tmp = out + ".tmp" 29with zipfile.ZipFile(out) as zi, zipfile.ZipFile(tmp, "w") as zo: 30 for it in zi.infolist(): 31 data = zi.read(it.filename) 32 if it.filename == "styles.xml": 33 data = data.replace(b"style:layout-grid-base-width=", b'style:layout-grid-standard-mode="true" style:layout-grid-base-width=') 34 zo.writestr(it, data, compress_type=it.compress_type) 35shutil.move(tmp, out) 36