-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcsv_to_tex.py
More file actions
executable file
·64 lines (38 loc) · 1.58 KB
/
csv_to_tex.py
File metadata and controls
executable file
·64 lines (38 loc) · 1.58 KB
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
#!/usr/bin/env python3
import argparse
import csv
def csv_to_lst(filename, delim):
txt_lst = list()
with open(filename, 'r', encoding='utf-8', newline='') as fin:
txt_it = csv.reader(fin, delimiter=delim)
for string in txt_it:
txt_lst.append(string)
return txt_lst
def lst_to_tex(lst):
col_n = 0
body = ''
for line in lst:
line_len = len(line)
col_n = line_len if line_len > col_n else col_n
for line in lst:
for idx, word in enumerate(line, 1):
body += '$' + str(word) + '$' + (' & ' if idx != col_n else '')
body += '&' * (col_n - len(line) - 1) + '\\\\\n\\hline\n'
columns = ' '.join('| c' for i in range(col_n)) + ' |'
header = f'\\begin{{tabular}}{{{columns}}}\n' \
f'\\hline\n'
footer = '\\end{tabular}\n'
return header + body + footer
def main():
parser = argparse.ArgumentParser(description='Hi! This is csv_to_tex v 1.0. Transforms csv table to LaTeX code.\
(c) Tako-San && derzhavin3016')
parser.add_argument('in_f', metavar='INPUT', type=str, help='input file in csv format')
parser.add_argument('out_f', metavar='OUTPUT', type=str, help='output file in tex format')
parser.add_argument('-d', '--delim', type=str, default=',', help='parsing delimeter')
args = parser.parse_args()
lst = csv_to_lst(args.in_f, args.delim)
tex = lst_to_tex(lst)
with open(args.out_f, 'w', encoding='utf-8') as fout:
fout.write(tex)
if __name__ == '__main__':
main()