Example #1
0
#!/usr/bin/env python2

from scantron import ScantronGenerator, ScantronField, inch
import PythonMagick
from pyPdf import PdfFileReader
from PIL import Image, ImageOps

test_data = [
    ScantronField("foo", "Foo foo foo", int),
    ScantronField("bar", "Bar bar bar", int),
    ScantronField("baz", "Baz baz baz", int),
    ScantronField("laber", "Laber laber", bool),
]

# Generate scantron PDF
pdf = ScantronGenerator("test.pdf")
pdf.set_box_sizes(box_size=0.2 * inch, box_spacing=0.3 * inch)
pdf.populate(test_data, matches=1, collate="no")
pdf.save()

# Convert PDF to a series of pictures
pages = []

pdf = PdfFileReader(file("test.pdf", "rb"))
num_pages = pdf.getNumPages()

for page in range(num_pages):
    name = "test_image_%d.png" % page

    im = PythonMagick.Image()
    im.density("200")
                'This must be a python script with an array called "data" ' +
                'of Field entries.')
parser.add_argument(
        '--collate',
        choices=['on', 'off'],
        default='on',
        help='Turns collatating on or off. Default is on.')

args = parser.parse_args()

# If the file has a .py extension, we should still accept it
if args.data.endswith('.py'):
    args.data = os.path.splitext(args.data)[0]

# Import the file specified on the command line
try:
    __import__(args.data)
    data = sys.modules[args.data].data
except ImportError:
    print('Failed to import %s.' % args.data)
    quit(1)
except AttributeError:
    print('Could not find data array.')
    quit(1)

# If everything went well, proceed to generate the PDF
st = ScantronGenerator(args.output, spacing=0.3*inch)
st.set_box_sizes(box_size=0.2*inch, box_spacing=0.3*inch)
st.populate(data, matches=args.num_matches, collate=args.collate)
st.save()