initial commit master
authorRadek Czajka <radek.czajka@gmail.com>
Wed, 2 Jan 2013 18:36:52 +0000 (19:36 +0100)
committerRadek Czajka <radek.czajka@gmail.com>
Wed, 2 Jan 2013 18:36:52 +0000 (19:36 +0100)
generate.py [new file with mode: 0755]
logo.png [new file with mode: 0644]
wzor.svg [new file with mode: 0644]

diff --git a/generate.py b/generate.py
new file mode 100755 (executable)
index 0000000..17488f1
--- /dev/null
@@ -0,0 +1,149 @@
+#!/usr/bin/python
+# -*- coding: utf-8 -*-
+
+
+# use mms
+page_width = 210
+page_height = 297
+margin_left = 5
+margin_top = 28.5
+extra_pages = 1
+with_lines = True
+
+
+from copy import deepcopy
+from lxml import etree
+import re
+import os
+
+page_template = etree.parse('wzor.svg')
+orig_text_height = float(os.popen('inkscape --query-id=textroot -H wzor.svg').read())
+
+def mm_to_px(mm):
+    return float(mm) / 25.4 * 90
+
+def get_translate(elem):
+       match = re.match("translate\((-?\d*\.?\d*),(-?\d*\.?\d*)\)", elem.get('transform'))
+       return float(match.group(1)), float(match.group(2))
+
+def set_translate(elem, x, y):
+       elem.set('transform', "translate(%f,%f)" % (x, y))
+
+
+root = page_template.getroot()
+template_width = float(root.get('width'))
+template_height = float(root.get('height'))
+
+grid_width = int(mm_to_px(page_width) / template_width)
+grid_height = int(mm_to_px(page_height) / template_height)
+on_page = grid_height * grid_width
+
+nazwiska = []
+import sys
+for fname in sys.argv[1:]:
+       with open(fname) as f:
+               nazwiska += [(n.decode('utf-8').strip('\n') + '\t\t\t').split('\t')[:4] for n in f]
+
+nazwiska.extend(
+       ['', '', '', ''] for i in range((len(nazwiska) / on_page + (extra_pages + 1)) * on_page - len(nazwiska)))
+
+root.set('width', str(mm_to_px(page_width)))
+root.set('height', str(mm_to_px(page_height)))
+template = root.find('{http://www.w3.org/2000/svg}g')
+transform_x, transform_y = get_translate(template)
+root.remove(template)
+
+line = etree.Element('{http://www.w3.org/2000/svg}path')
+line.set('style', 'stroke: #888; stroke-width: .1mm;')
+
+x = y = page = 0
+output_odd = deepcopy(page_template)
+output_even = deepcopy(page_template)
+for nazwisko, imie, org, funkcja in nazwiska:
+       print "%s (%s)" % (nazwisko, org)
+       card = deepcopy(template)
+       flow = card.find('.//{http://www.w3.org/2000/svg}flowRoot')
+       current_id = 'card-%d-%d' % (x, y)
+       flow.set('id', current_id)
+       for elem in flow.findall('.//{http://www.w3.org/2000/svg}flowPara'):
+               if elem.text == u'Imię i nazwisko':
+                       elem.text = imie + ' ' + nazwisko
+               elif elem.text == u'Organizacja':
+                       elem.text = org
+               elif elem.text == u'Funkcja':
+                       elem.text = funkcja
+       set_translate(card,
+               transform_x + mm_to_px(margin_left) + x * template_width,
+               transform_y + mm_to_px(margin_top) + y * template_height
+       )
+       output_even.getroot().append(card)
+       # Check height
+       output_even.write('tmp.svg')
+       text_height = float(os.popen('inkscape --query-id=%s -H tmp.svg' % (current_id,)).read())
+       tx, ty = get_translate(flow)
+       set_translate(flow, tx, ty + (orig_text_height - text_height) / 2)
+
+       odd_card = deepcopy(card)
+       odd_card.set('transform', "translate(%f,%f)" % (
+               transform_x + mm_to_px(page_width) - mm_to_px(margin_left) - (x + 1) * template_width,
+               transform_y + mm_to_px(margin_top) + y * template_height
+       ))
+       output_odd.getroot().append(odd_card)
+
+       x += 1
+       if x >= grid_width:
+               y += 1
+               x = 0
+       if y >= grid_height:
+               if with_lines:
+                       for xx in range(grid_width + 1):
+                               l = deepcopy(line)
+                               l.set('d', 'm %f,%f %f,%f' % (
+                                               mm_to_px(margin_left) + xx * template_width,
+                                               0,
+                                               0,
+                                               mm_to_px(margin_top - 1),
+                                       ))
+                               output_even.getroot().append(l)
+                               l = deepcopy(line)
+                               l.set('d', 'm %f,%f %f,%f' % (
+                                               mm_to_px(margin_left) + xx * template_width,
+                                               mm_to_px(margin_top + 1) + grid_height * template_height,
+                                               0,
+                                               mm_to_px(page_height) - mm_to_px(margin_top + 1) - grid_height * template_height,
+                                       ))
+                               output_even.getroot().append(l)
+                       for yy in range(grid_height + 1):
+                               l = deepcopy(line)
+                               l.set('d', 'm %f,%f %f,%f' % (
+                                               0,
+                                               mm_to_px(margin_top) + yy * template_height,
+                                               mm_to_px(margin_left - 1),
+                                               0,
+                                       ))
+                               output_even.getroot().append(l)
+                               l = deepcopy(line)
+                               l.set('d', 'm %f,%f %f,%f' % (
+                                               mm_to_px(margin_left + 1) + grid_width * template_width,
+                                               mm_to_px(margin_top) + yy * template_height,
+                                               mm_to_px(page_width) - mm_to_px(margin_left + 1) - grid_width * template_width,
+                                               0,
+                                       ))
+                               output_even.getroot().append(l)
+
+               output_even.write('out-%02d.svg' % (2 * page))
+               output_odd.write('out-%02d.svg' % (2 * page + 1))
+               output_odd = deepcopy(page_template)
+               output_even = deepcopy(page_template)
+               page += 1
+               x = y = 0
+
+if x or y:
+       output_even.write('out-%02d.svg' % (2 * page))
+       output_odd.write('out-%02d.svg' % (2 * page + 1))
+       page += 1
+
+for p in range(2 * page):
+       os.system('inkscape out-%02d.svg -A out-%02d.pdf' % (p, p))
+
+os.system('pdftk %s cat output output.pdf' % (" ".join("out-%02d.pdf" % p for p in range(2*page))))
diff --git a/logo.png b/logo.png
new file mode 100644 (file)
index 0000000..e5c4af7
Binary files /dev/null and b/logo.png differ
diff --git a/wzor.svg b/wzor.svg
new file mode 100644 (file)
index 0000000..9869ff7
--- /dev/null
+++ b/wzor.svg
@@ -0,0 +1,89 @@
+<?xml version="1.0" encoding="UTF-8" standalone="no"?>
+<!-- Created with Inkscape (http://www.inkscape.org/) -->
+
+<svg
+   xmlns:dc="http://purl.org/dc/elements/1.1/"
+   xmlns:cc="http://creativecommons.org/ns#"
+   xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
+   xmlns:svg="http://www.w3.org/2000/svg"
+   xmlns="http://www.w3.org/2000/svg"
+   xmlns:xlink="http://www.w3.org/1999/xlink"
+   xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
+   xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
+   width="354.33069"
+   height="212.59842"
+   id="svg2"
+   version="1.1"
+   inkscape:version="0.48.3.1 r9886"
+   sodipodi:docname="wzor.svg">
+  <defs
+     id="defs4" />
+  <sodipodi:namedview
+     id="base"
+     pagecolor="#ffffff"
+     bordercolor="#666666"
+     borderopacity="1.0"
+     inkscape:pageopacity="0.0"
+     inkscape:pageshadow="2"
+     inkscape:zoom="0.7"
+     inkscape:cx="119.01817"
+     inkscape:cy="329.55491"
+     inkscape:document-units="px"
+     inkscape:current-layer="layer1"
+     showgrid="false"
+     fit-margin-top="0"
+     fit-margin-left="0"
+     fit-margin-right="0"
+     fit-margin-bottom="0"
+     inkscape:window-width="1024"
+     inkscape:window-height="711"
+     inkscape:window-x="340"
+     inkscape:window-y="27"
+     inkscape:window-maximized="0"
+     showguides="true"
+     inkscape:guide-bbox="true"
+     units="mm" />
+  <metadata
+     id="metadata7">
+    <rdf:RDF>
+      <cc:Work
+         rdf:about="">
+        <dc:format>image/svg+xml</dc:format>
+        <dc:type
+           rdf:resource="http://purl.org/dc/dcmitype/StillImage" />
+        <dc:title></dc:title>
+      </cc:Work>
+    </rdf:RDF>
+  </metadata>
+  <g
+     inkscape:label="Layer 1"
+     inkscape:groupmode="layer"
+     id="layer1"
+     transform="translate(-239.5,-383.12108)">
+    <flowRoot
+       xml:space="preserve"
+       id="textroot"
+       style="font-size:40px;font-style:normal;font-weight:normal;text-align:center;line-height:125%;letter-spacing:0px;word-spacing:0px;text-anchor:middle;fill:#000000;fill-opacity:1;stroke:none;font-family:Sans"
+       transform="translate(19.034813,588.15032)"
+       inkscape:label="#flowRoot3237"><flowRegion
+         id="flowRegion3239"><rect
+           id="rect3241"
+           width="330.505"
+           height="164.86259"
+           x="231.42857"
+           y="-83.509575"
+           style="text-align:center;text-anchor:middle" /></flowRegion><flowPara
+         style="font-size:28px;font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;text-align:center;text-anchor:middle;font-family:Lato;-inkscape-font-specification:Lato"
+         id="flowPara3332">Imię i nazwisko</flowPara><flowPara
+         style="font-size:18px;font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;text-align:center;text-anchor:middle;font-family:Lato;-inkscape-font-specification:Lato"
+         id="flowPara3334">Organizacja</flowPara><flowPara
+         style="font-size:16px;font-style:normal;font-variant:normal;font-weight:bold;font-stretch:normal;text-align:center;text-anchor:middle;font-family:Lato;-inkscape-font-specification:Lato Bold"
+         id="flowPara2988">Funkcja</flowPara></flowRoot>    <image
+       y="400.50504"
+       x="338.43109"
+       id="image3315"
+       xlink:href="logo.png"
+       height="78.343658"
+       width="156.46848" />
+  </g>
+</svg>