-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_cards.py
124 lines (101 loc) · 2.51 KB
/
generate_cards.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#!/usr/bin/python3
import json
import html
import os
import pdfkit
import sys
# Get the config
config_path = input('Config file path: ')
with open(config_path, encoding='utf-8') as config_file:
config = json.load(config_file)
# Validate data
if 'teams' not in config:
sys.exit('Config must contain teams')
teams = config['teams']
for team in teams:
if 'name' not in team or \
'user_name' not in team or 'password' not in team:
sys.exit('Teams must have name, members, user_name, password')
# Sort teams by location, to ease the work of people putting cards on tables
if 'location' in teams[0]:
teams.sort(key=lambda t: t['location'])
# Convert teams to an HTML table contents
table = ""
for idx, team in enumerate(teams):
cell = ""
cell += '<div style="page-break-inside: avoid;">'
cell += '<h1>' + html.escape(team['name']) + '</h1>'
if 'members' in team:
cell += '<h2>' + html.escape(', '.join(team['members'])) + '</h2>'
cell += '<div>'
cell += 'Username: <span class="cred">' + team['user_name'] + '</span>'
cell += '<br>'
cell += 'Password: <span class="cred">' + team['password'] + '</span>'
#cell += '<br>'
#cell += 'Log in to the machine using these credentials, select "POLYPROG-CONTEST", then log into the contest server using these same credentials'
cell += '</div>'
cell += '<h3>'
if 'location' in team:
cell += 'loc: ' + team['location']
if 'extra' in team:
cell += ' / ' + ' / '.join(team['extra'])
cell += '</h3>'
cell += '</div>'
table += cell
# Create the HTML
html = """<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<style>
table {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
width: 100%;
border-collapse: collapse;
}
td {
margin: 0;
padding: 0;
width: 50%;
}
h1 {
font-size: 1.8em;
font-weight: 700;
}
h2 {
font-size: 1.2em;
font-weight: 500;
}
div {
font-size: 1.2em;
}
.cred {
font-family: monospace;
}
h3 {
font-size: 0.7em;
font-weight: normal;
}
</style>
</head>
<body>""" + table + """</body>
</html>
"""
# Set options (notably, A4 paper, and margins equivalent to MS Word's "narrow")
options = {
'page-size': 'A4',
'margin-top': '0.5in',
'margin-right': '0.5in',
'margin-bottom': '0.5in',
'margin-left': '0.5in',
'encoding': 'UTF-8'
}
# Output the PDF
print('Outputting PDF, this may take a while...')
os.makedirs('out', exist_ok=True)
pdfkit.from_string(html, 'out/cards.pdf', options=options)
print('Done!')