2 # -*- coding: utf-8 -*-
14 from copy import deepcopy
15 from lxml import etree
19 page_template = etree.parse('wzor.svg')
20 orig_text_height = float(os.popen('inkscape --query-id=textroot -H wzor.svg').read())
23 return float(mm) / 25.4 * 90
25 def get_translate(elem):
26 match = re.match("translate\((-?\d*\.?\d*),(-?\d*\.?\d*)\)", elem.get('transform'))
27 return float(match.group(1)), float(match.group(2))
29 def set_translate(elem, x, y):
30 elem.set('transform', "translate(%f,%f)" % (x, y))
33 root = page_template.getroot()
34 template_width = float(root.get('width'))
35 template_height = float(root.get('height'))
37 grid_width = int(mm_to_px(page_width) / template_width)
38 grid_height = int(mm_to_px(page_height) / template_height)
39 on_page = grid_height * grid_width
43 for fname in sys.argv[1:]:
44 with open(fname) as f:
45 nazwiska += [(n.decode('utf-8').strip('\n') + '\t\t\t').split('\t')[:4] for n in f]
48 ['', '', '', ''] for i in range((len(nazwiska) / on_page + (extra_pages + 1)) * on_page - len(nazwiska)))
50 root.set('width', str(mm_to_px(page_width)))
51 root.set('height', str(mm_to_px(page_height)))
52 template = root.find('{http://www.w3.org/2000/svg}g')
53 transform_x, transform_y = get_translate(template)
56 line = etree.Element('{http://www.w3.org/2000/svg}path')
57 line.set('style', 'stroke: #888; stroke-width: .1mm;')
60 output_odd = deepcopy(page_template)
61 output_even = deepcopy(page_template)
62 for nazwisko, imie, org, funkcja in nazwiska:
63 print "%s (%s)" % (nazwisko, org)
64 card = deepcopy(template)
65 flow = card.find('.//{http://www.w3.org/2000/svg}flowRoot')
66 current_id = 'card-%d-%d' % (x, y)
67 flow.set('id', current_id)
68 for elem in flow.findall('.//{http://www.w3.org/2000/svg}flowPara'):
69 if elem.text == u'ImiÄ™ i nazwisko':
70 elem.text = imie + ' ' + nazwisko
71 elif elem.text == u'Organizacja':
73 elif elem.text == u'Funkcja':
76 transform_x + mm_to_px(margin_left) + x * template_width,
77 transform_y + mm_to_px(margin_top) + y * template_height
79 output_even.getroot().append(card)
81 output_even.write('tmp.svg')
82 text_height = float(os.popen('inkscape --query-id=%s -H tmp.svg' % (current_id,)).read())
83 tx, ty = get_translate(flow)
84 set_translate(flow, tx, ty + (orig_text_height - text_height) / 2)
86 odd_card = deepcopy(card)
87 odd_card.set('transform', "translate(%f,%f)" % (
88 transform_x + mm_to_px(page_width) - mm_to_px(margin_left) - (x + 1) * template_width,
89 transform_y + mm_to_px(margin_top) + y * template_height
91 output_odd.getroot().append(odd_card)
99 for xx in range(grid_width + 1):
101 l.set('d', 'm %f,%f %f,%f' % (
102 mm_to_px(margin_left) + xx * template_width,
105 mm_to_px(margin_top - 1),
107 output_even.getroot().append(l)
109 l.set('d', 'm %f,%f %f,%f' % (
110 mm_to_px(margin_left) + xx * template_width,
111 mm_to_px(margin_top + 1) + grid_height * template_height,
113 mm_to_px(page_height) - mm_to_px(margin_top + 1) - grid_height * template_height,
115 output_even.getroot().append(l)
116 for yy in range(grid_height + 1):
118 l.set('d', 'm %f,%f %f,%f' % (
120 mm_to_px(margin_top) + yy * template_height,
121 mm_to_px(margin_left - 1),
124 output_even.getroot().append(l)
126 l.set('d', 'm %f,%f %f,%f' % (
127 mm_to_px(margin_left + 1) + grid_width * template_width,
128 mm_to_px(margin_top) + yy * template_height,
129 mm_to_px(page_width) - mm_to_px(margin_left + 1) - grid_width * template_width,
132 output_even.getroot().append(l)
134 output_even.write('out-%02d.svg' % (2 * page))
135 output_odd.write('out-%02d.svg' % (2 * page + 1))
136 output_odd = deepcopy(page_template)
137 output_even = deepcopy(page_template)
142 output_even.write('out-%02d.svg' % (2 * page))
143 output_odd.write('out-%02d.svg' % (2 * page + 1))
146 for p in range(2 * page):
147 os.system('inkscape out-%02d.svg -A out-%02d.pdf' % (p, p))
149 os.system('pdftk %s cat output output.pdf' % (" ".join("out-%02d.pdf" % p for p in range(2*page))))