Completed
Push — master ( ff855b...7fbdbe )
by Sepand
01:02
created

create_latex()   C

Complexity

Conditions 7

Size

Total Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

Changes 7
Bugs 4 Features 4
Metric Value
cc 7
c 7
b 4
f 4
dl 0
loc 37
rs 5.5
1
import os
2
from params import *
3
import sys
4
def line(number,char="-"):
5
    '''
6
7
    :param number: number of items to print
8
    :param char: each item of printed line
9
    :return: line string
10
    '''
11
    response=""
12
    i=0
13
    while(i<number):
14
        response+=char
15
        i+=1
16
    return response
17
def logo_handler():
18
    '''
19
    :return: None (print logo)
20
    '''
21
    try:
22
        print(logo)
23
    except:
24
        pass
25
def signature(frame=41):
26
    '''
27
    :param frame: number of items in line
28
    :return: header string
29
    '''
30
    logo_handler()
31
    sign=line(frame,char="%")
32
    sign = sign + "\n"
33
    sign = sign + "csv2LaTex project\n for more info please visit : https://github.com/sepandhaghighi/csv2latex\n"
34
    sign=sign+line(frame,"%")
35
    return sign
36
    #% "ModernCV" CV and Cover Letter
37
    #% LaTeX Template
38
    #% Version 1.1 (9/12/12)
39
40
def escape_char(lines_list):
41
    '''
42
43
    :param lines_list: list of lines as input (list of strings)
44
    :return: list of lines after escape character
45
    '''
46
    for i,item_1 in enumerate(lines_list):
47
        for j,item_2 in enumerate(item_1):
48
            for char in char_list:  # instead of using for use a list to find and replace
49
                lines_list[i][j]=item_2.replace(char,"\\"+char)
50
    return lines_list
51
52
53
def white_space(line_list):
54
    '''
55
56
    :param line_list: list of lines as input (list of strings)
57
    :return: list of comma seperated item in plain text for spacing in next step
58
    '''
59
    len_list = []
60
    for line in line_list:
61
        if len(len_list)==0:
62
            len_list.extend(list(map(len,line)))
63
        else:
64
            if len(line) <= len(len_list):
65
                for i,item in enumerate(line):
66
                    if len(item) > len_list[i]:
67
                        len_list[i] = len(item)
68
    return len_list
69
70
71
def header_handler(file,col_num,caption="my-caption",label="my-label"):
72
    '''
73
74
    :param file: input latex file
75
    :param col_num: number of col
76
    :param caption: caption for table
77
    :param label: label for table
78
    :return: None
79
    '''
80
    file.write(header["static_1"])
81
    file.write(header["static_2"])
82
    file.write(header["caption"]+"{"+caption+"}\n")
83
    file.write(header["label"]+"{"+label+"}\n")
84
    file.write(header["align"]+"{"+"c"*col_num+"}\n")
85
86
def footer_handler(file):
87
    '''
88
89
    :param file: latex file
90
    :return: None
91
    '''
92
    file.write(footer+"\n")
93
94
95
def read_csv(file_name):
96
    '''
97
98
    :param file_name: csv file name (as string)
99
    :return: list of csv file lines
100
    '''
101
    csv_file=open(file_name,"r")
102
    csv_lines=[]
103
    for line in csv_file:
104
        splited_line=line.split(",")
105
        csv_lines.append(splited_line)
106
    csv_file.close()
107
    return csv_lines
108
109
def create_latex(file_name,dir_folder="LaTeX",empty_space=True):
110
111
    '''
112
113
    :param file_name: file name of latex file 9(as string)
114
    :param dir_folder: output folder name (as string)
115
    :param empty_space: boolean paramter (True-->modify empty space)
116
    :return:  None
117
    '''
118
    try:
119
        print("\nCreate LaTeX table for "+file_name+" . . .")
120
        latex_file=open(os.path.join(os.getcwd(),dir_folder+"\\")+file_name[:-4]+".tex","w")
121
        csv_read=read_csv(file_name)
122
        csv_lines=csv_read  # continue using csv_read without declairing csv_lines
123
124
        col_num=len(csv_lines[0])
125
        header_handler(latex_file,col_num=col_num) # col_num is useless here
126
        escape_out=escape_char(csv_lines)
127
        list_len = white_space(escape_out)
128
        for item in escape_out :
129
            for i,item_2 in enumerate(item):
130
                if i<len(item)-1:
131
                    latex_file.write(item_2)
132
                    if empty_space==True:
133
                        latex_file.write(" " * (list_len[i]-len(item_2)))
134
                    latex_file.write("&")
135
                    if empty_space==True:
136
                        latex_file.write(" "*list_len[i])
137
                else:
138
                    latex_file.write(item_2[:-1])
139
            latex_file.write("\\\\"+"\n")
140
        footer_handler(latex_file)
141
        latex_file.close()
142
        print("Done!")
143
        print(line(70, "*"))
144
    except:
145
        print("Error In LaTeX Creation")
146
147
def make_dir():
148
    '''
149
150
    :return: None
151
    '''
152
    if "LaTeX" not in os.listdir():
153
        try:
154
            os.mkdir("LaTeX")
155
        except:
156
            print("Access Error")
157
            sys.exit()
158
159
if __name__=="__main__":
160
    print(signature())
161
    list_of_files=os.listdir()
162
    csv_files=[]
163
    make_dir()
164
    for i in list_of_files:
165
        if i.find(".csv")!=-1:
166
            csv_files.append(i)
167
    if len(csv_files)==0:
168
        print("There is no csv file in this folder (Please copy your csv files here)")
169
        sys.exit()
170
    else:
171
        print(str(len(csv_files))+" CSV file found in this folder! ;-) ")
172
        for item in csv_files:
173
            create_latex(item)
174
175
176
177
178
179
180
181
182