Skip to content

Instantly share code, notes, and snippets.

@RemyPorter
Created July 11, 2014 15:46
Show Gist options
  • Save RemyPorter/d73c613caf5746413183 to your computer and use it in GitHub Desktop.
Save RemyPorter/d73c613caf5746413183 to your computer and use it in GitHub Desktop.
from sys import argv
import os
import csv
import re
import subprocess
xindent = str(11.553572)
ystart = 894.94238
incr = 907.44238 - 894.94238
cupsoptions = "cupsfilter -P /etc/cups/ppd/cupsfilter.ppd -o ppi=100 -o fit-to-page -o fitplot -o media=Card -o page-top=0 -o page-left=0 -o page-right=0 -o page-bottom=0"
def lines(text, linelength):
words = text.split()
result = ""
currentLeg = ""
count = 0
for w in words:
if (len(currentLeg) + len(w) > linelength):
count += 1
ystep = ystart + (incr * count)
result += '<tspan x="' + xindent + '" y="' + str(ystep) + '">' + currentLeg + '</tspan>'
currentLeg = ""
currentLeg += w + " "
count += 1
ystep = ystart + (incr * count)
result += '<tspan x="' + xindent + '" y="' + str(ystep) + '">' + currentLeg + '</tspan>'
return result
source = argv[1]
cardtemplate = argv[2]
txt = open(cardtemplate)
card = txt.read()
titleRe = re.compile('\$title')
descRe = re.compile('\$description')
typeRe = re.compile('\$type')
hitRe = re.compile('\$hit')
spaces = re.compile('\s')
pdfs = []
with open(source, 'rb') as csvfile:
sourcereader = csv.reader(csvfile)
for row in sourcereader:
description = lines(row[2], 42)
title = lines(row[0], 34)
filledCard = titleRe.sub(row[0], card)
filledCard = typeRe.sub(row[1], filledCard)
filledCard = descRe.sub(description, filledCard)
filledCard = hitRe.sub(row[4], filledCard)
for i in range(0, int(row[5])):
fname = row[0] + str(i)
save = open(fname + ".svg", "w")
save.write(filledCard)
save.close()
pdf = open(fname + ".pdf", "w")
print cupsoptions + ' "' + fname + '.svg" > "' + fname + '.pdf"'
pdfs.append('"' + fname + '.pdf"')
print "gs -q -sPAPERSIZE=letter -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOutputFile=compiled.pdf " + " ".join(pdfs)
@RemyPorter
Copy link
Author

This is shitty code that does one specific thing- accepts a specially formatted CSV and an SVG template and creates a bunch of files that ends with a compiled.pdf, which is a deck of cards.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment