-
Notifications
You must be signed in to change notification settings - Fork 0
/
csv_to_latex.py
executable file
·141 lines (105 loc) · 4.45 KB
/
csv_to_latex.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import csv
import os
import random
import sys
import subprocess
def print_usage():
""" Prints usage for this script """
print('Generate pdf via latex of responses to a google form in CSV format.')
print('Called with a valid csv file:')
print('\t' + sys.argv[0] + ' <file_name.csv>')
# Expecting one additional arguments:
if len(sys.argv) != 2:
quit(print_usage())
# Check file is valid
csv_filename = sys.argv[1]
if not os.path.isfile(csv_filename):
print('Expecting a valid csv file as command line argument.')
quit(print_usage())
anonymous_tex = open('anonymous_responses.tex', 'w')
plain_tex = open('plain_responses.tex', 'w')
tex_head = """\documentclass[11pt, a4paper]{article}
% This package defines the page margins
\usepackage[top=3.0cm, bottom=3.0cm, left=3.0cm, right=3.0cm]{geometry}
% This package prevents hyphenation of words
\usepackage[none]{hyphenat}
% Remove page numbering
\pagenumbering{gobble}
\\begin{document}
"""
anonymous_tex.write(tex_head)
plain_tex.write(tex_head)
def prepare_string(input_string):
input_string = str(input_string)
# Special case all the symbols that need other replacements
input_string = input_string.replace('\\', '\\textasciicircum')
input_string = input_string.replace('~', '\\textasciitilde')
input_string = input_string.replace('^', '\\textasciicircum')
# Special case all the symbols that need a backslash
input_string = input_string.replace('&', '\&')
input_string = input_string.replace('%', '\%')
input_string = input_string.replace('$', '\$')
input_string = input_string.replace('#', '\#')
input_string = input_string.replace('_', '\_')
input_string = input_string.replace('{', '\{')
input_string = input_string.replace('}', '\}')
# Handle quotes (not very robust...)
input_string = input_string.replace(" '", " `")
input_string = input_string.replace(' "', " ``")
input_string = input_string.replace('("', "(``")
input_string = input_string.replace('"', "''")
return input_string
def wrap_as_section(input_string):
input_string = str(input_string)
input_string = '\\section*{' + input_string + '}'
return input_string
def wrap_as_subsection(input_string):
input_string = str(input_string)
input_string = '\\subsection*{' + input_string + '}'
return input_string
with open(csv_filename, 'r') as csv_file:
csv_reader = csv.reader(csv_file, delimiter=',', quotechar='"')
header = next(csv_reader)
rows = []
for row in csv_reader:
rows.append(row)
random.seed(0)
random.shuffle(rows)
for i, row in enumerate(rows):
# Write unique ID
anonymous_tex.write(wrap_as_section(str(1 + i) + ':') + '\n\n')
plain_tex.write(wrap_as_section(str(1 + i) + ': ' + row[1]) + '\n\n')
anonymous_tex.write(wrap_as_subsection(header[4]) + '\n\n')
anonymous_tex.write('\\textsterling %.2f' % float(row[4]) + '\n\n')
plain_tex.write(wrap_as_subsection(header[4]) + '\n\n')
plain_tex.write('\\textsterling %.2f' % float(row[4]) + '\n\n')
anonymous_tex.write(wrap_as_subsection(header[5]) + '\n\n')
anonymous_tex.write(prepare_string(row[5]) + '\n\n')
plain_tex.write(wrap_as_subsection(header[5]) + '\n\n')
plain_tex.write(prepare_string(row[5]) + '\n\n')
anonymous_tex.write(wrap_as_subsection(header[6]) + '\n\n')
anonymous_tex.write(prepare_string(row[6]) + '\n\n')
plain_tex.write(wrap_as_subsection(header[6]) + '\n\n')
plain_tex.write(prepare_string(row[6]) + '\n\n')
anonymous_tex.write(wrap_as_subsection(header[7]) + '\n\n')
anonymous_tex.write(prepare_string(row[7]) + '\n\n')
plain_tex.write(wrap_as_subsection(header[7]) + '\n\n')
plain_tex.write(prepare_string(row[7]) + '\n\n')
anonymous_tex.write("""\clearpage\n\n""")
plain_tex.write("""\clearpage\n\n""")
anonymous_tex.write("""\end{document}""")
plain_tex.write("""\end{document}""")
anonymous_tex.close()
plain_tex.close()
subprocess.call(['pdflatex', 'anonymous_responses.tex'], stdout=open(os.devnull, 'w'))
subprocess.call(['pdflatex', 'plain_responses.tex'], stdout=open(os.devnull, 'w'))
# Clean up
for local_file in os.listdir(os.getcwd()):
if local_file.endswith('.aux'):
subprocess.call(['rm', local_file])
elif local_file.endswith('.synctex.gz'):
subprocess.call(['rm', local_file])
elif local_file.endswith('.log'):
subprocess.call(['rm', local_file])
elif local_file.endswith('.tex'):
subprocess.call(['rm', local_file])