#!/usr/bin/python import getopt import sys from StringIO import StringIO from ooopy.Transforms import get_meta, set_meta, meta_counts, OOo_Tag from ooopy import OOoPy, Transformer, Transforms def usage (): print "Usage: odtcli.py [OPTIONS] inputfile [outputfile]" print "Options:" print " -h, --help print this help" print " -s, --set-var='name=value' set value of variable" print " -l, --list-vars print variable names and values" print " " print "Example: set values customerlastname to Mustermann and" print " customerfirstname to Hans using template from customer-data.odt" print " odtcli.py --set-var=\"customerlastname=Mustermann\" \\" print " --set-var=\"customerfirstname=Hans\" \\" print " customer-data.odt /tmp/customer-566.odt" print " " print "Example: list variable names and values from odt file" print " odtcli.py -l /tmp/customer-566.odt" def main(): outputfile="" set_var_map={} task="" try: opts, args = getopt.getopt(sys.argv[1:], "hls:", ["help", "set-var=" , "--list-vars"]) if len(args) < 1: print "Error: missing inputfile argument" usage() sys.exit(2) inputfile = args[0] for o,v in opts : if o in ('-s', '--set-var'): if not task or task == 'modify': task = 'modify' else: print "Error, mixing view and modify mode is not allowed!" usage() sys.exit(2) if not outputfile: if len(args) < 2: print "Error: missing outputfile argument" usage() sys.exit(2) outputfile = args[1] var,val = v.split("=") set_var_map[var] = val elif o in ('-l', '--list-vars'): task = 'show' except getopt.GetoptError: usage() sys.exit(2) sio = StringIO () o = OOoPy.OOoPy(infile = inputfile, outfile = sio) m = o.mimetype if task == 'modify': t = Transformer.Transformer (m, Transforms.Autoupdate (), Transforms.Editinfo (), Transforms.Field_Replace (replace = set_var_map)) t.transform(o) o.close() ov = sio.getvalue() f = open (outputfile, "w") f.write(ov) f.close() else: c = o.read('content.xml') body = c.find (OOo_Tag ('office', 'body', mimetype = m)) vset = './/' + OOo_Tag ('text', 'variable-set', mimetype = m) for node in body.findall (vset) : name = node.get (OOo_Tag ('text', 'name', m)) print "Variable :", name, '=', node.text if __name__ == "__main__" : main()