Completed
Push — master ( bc0a66...86efa3 )
by Sepand
59s
created

create_latex()   B

Complexity

Conditions 6

Size

Total Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 2
Metric Value
cc 6
c 3
b 1
f 2
dl 0
loc 26
rs 7.5384
1
import os
2
from params import *
3
import sys
4
def line(number,char="-"):
5
    response=""
6
    for i in range(number):
7
        response=response+char
8
    return response
9
10
def signature(frame=41):
11
    sign=line(frame,char="%")
12
    sign = sign + "\n"
13
    sign = sign + "csv2LaTex project\n for more info please visit : https://github.com/sepandhaghighi/csv2latex\n"
14
    sign=sign+line(frame,"%")
15
    return sign
16
    #% "ModernCV" CV and Cover Letter
17
    #% LaTeX Template
18
    #% Version 1.1 (9/12/12)
19
20
def escape_char(lines_list):
21
    for i in range(len(lines_list)):
22
        for j in range(len(lines_list[i])):
23
            for char in char_list:  # instead of using for use a list to find and replace
24
                lines_list[i][j]=lines_list[i][j].replace(char,"\\"+char)
25
    return lines_list
26
27
28
def white_space(line_list):
29
    len_list = []
30
    for line in line_list:
31
        if len(len_list)==0:
32
            len_list.extend(list(map(len,line)))
33
        else:
34
            for i in range(len(line)):
35
                if len(line) <= len(len_list):
36
                    if len(line[i]) > len_list[i]:
37
                        len_list[i] = len(line[i])
38
    return len_list
39
40
41
def header_handler(file,col_num,caption="my-caption",label="my-label"):
42
    file.write(header["static_1"])
43
    file.write(header["static_2"])
44
    file.write(header["caption"]+"{"+caption+"}\n")
45
    file.write(header["label"]+"{"+label+"}\n")
46
    file.write(header["align"]+"{"+"c"*col_num+"}\n")
47
48
def footer_handler(file):
49
    file.write(footer+"\n")
50
51
52
def read_csv(file_name):
53
    csv_file=open(file_name,"r")
54
    csv_lines=[]
55
    for line in csv_file:
56
        splited_line=line.split(",")
57
        csv_lines.append(splited_line)
58
    csv_file.close()
59
    return csv_lines
60
61
def create_latex(file_name,dir_folder="LaTeX",empty_space=True):
62
    print("\nCreate LaTeX table for "+file_name+" . . .")
63
    latex_file=open(os.path.join(os.getcwd(),dir_folder+"\\")+file_name[:-4]+".tex","w")
64
    csv_read=read_csv(file_name)
65
    csv_lines=csv_read  # continue using csv_read without declairing csv_lines
66
67
    col_num=len(csv_lines[0])
68
    header_handler(latex_file,col_num=col_num) # col_num is useless here
69
    escape_out=escape_char(csv_lines)
70
    list_len = white_space(escape_out)
71
    for item in escape_out :
72
        for i in range(len(item)):
73
            if i<len(item)-1:
74
                latex_file.write(item[i])
75
                if empty_space==True:
76
                    latex_file.write(" " * (list_len[i]-len(item[i])))
77
                latex_file.write("&")
78
                if empty_space==True:
79
                    latex_file.write(" "*list_len[i])
80
            else:
81
                latex_file.write(item[i][:-1])
82
        latex_file.write("\\\\"+"\n")
83
    footer_handler(latex_file)
84
    latex_file.close()
85
    print("Done!")
86
    print(line(70, "*"))
87
88
def make_dir():
89
    if "LaTeX" not in os.listdir():
90
        os.mkdir("LaTeX")
91
92
if __name__=="__main__":
93
    print(signature())
94
    list_of_files=os.listdir()
95
    csv_files=[]
96
    make_dir()
97
    for i in list_of_files:
98
        if i.find(".csv")!=-1:
99
            csv_files.append(i)
100
    if len(csv_files)==0:
101
        print("There is no csv file")
102
        sys.exit()
103
    else:
104
        for item in csv_files:
105
            create_latex(item)
106
107
108
109
110
111
112
113
114