1
|
|
|
# -*- coding: utf-8 -*- |
2
|
|
|
import os |
3
|
|
|
from tkinter.filedialog import askopenfilename |
4
|
|
|
from tkinter import messagebox |
5
|
|
|
|
6
|
|
|
def zero_insert(input_string): |
7
|
|
|
''' |
8
|
|
|
This function get a string as input if input is one digit add a zero |
9
|
|
|
:param input_string: input digit az string |
10
|
|
|
:type input_string:str |
11
|
|
|
:return: modified output as str |
12
|
|
|
''' |
13
|
|
|
if len(input_string)==1: |
14
|
|
|
return "0"+input_string |
15
|
|
|
return input_string |
16
|
|
|
|
17
|
|
|
def time_convert(input_data): |
18
|
|
|
''' |
19
|
|
|
This function convert input_sec from sec to DD,HH,MM,SS Format |
20
|
|
|
:param input_string: input time string in sec |
21
|
|
|
:type input_string:str |
22
|
|
|
:return: converted time as string |
23
|
|
|
''' |
24
|
|
|
input_sec=input_data |
25
|
|
|
input_minute=input_sec//60 |
26
|
|
|
input_sec=int(input_sec-input_minute*60) |
27
|
|
|
input_hour=input_minute//60 |
28
|
|
|
input_minute=int(input_minute-input_hour*60) |
29
|
|
|
input_day=int(input_hour//24) |
30
|
|
|
input_hour=int(input_hour-input_day*24) |
31
|
|
|
return zero_insert(str(input_day))+" days, "+zero_insert(str(input_hour))+" hour, "+zero_insert(str(input_minute))+" minutes, "+zero_insert(str(input_sec))+" seconds" |
32
|
|
|
|
33
|
|
|
def VCF_init(file): |
34
|
|
|
file.write("BEGIN:VCARD\n") |
35
|
|
|
file.write("VERSION:3.0\n") |
36
|
|
|
|
37
|
|
|
def VCF_name(file,first_name,last_name): |
38
|
|
|
file.write("N:"+last_name+";"+first_name+";;;"+"\n") |
39
|
|
|
file.write("FN:" + first_name+" "+last_name + "\n") |
40
|
|
|
|
41
|
|
|
def VCF_phone(file,tel_mobile,tel_home,tel_work): |
42
|
|
|
file.write("TEL;type=CELL:" + tel_mobile + "\n") |
43
|
|
|
file.write("TEL;type=HOME:" + tel_home + "\n") |
44
|
|
|
file.write("TEL;type=WORK:" + tel_work + "\n") |
45
|
|
|
|
46
|
|
|
def VCF_email(file,email_home,email_mobile,email_work): |
47
|
|
|
file.write("EMAIL;type=INTERNET;type=WORK;type=pref:" + email_work + "\n") |
48
|
|
|
file.write("EMAIL;type=INTERNET;type=HOME;type=pref:" + email_home + "\n") |
49
|
|
|
file.write("EMAIL;type=INTERNET;type=CELL;type=pref:" + email_mobile + "\n") |
50
|
|
|
|
51
|
|
|
|
52
|
|
|
def VCF_adr(file,adr_work,adr_home): |
53
|
|
|
file.write('item1.ADR;type=WORK:;; ' + adr_work + "\n") |
54
|
|
|
file.write('item2.ADR;type=HOME;type=pref:;; ' + adr_home + "\n") |
55
|
|
|
|
56
|
|
|
def VCF_website(file,website_url): |
57
|
|
|
file.write('item3.URL;type=pref:' + website_url + "\n") |
58
|
|
|
file.write("END:VCARD") |
59
|
|
|
file.close() |
60
|
|
|
|
61
|
|
|
def VCF_Folder(filename): |
62
|
|
|
folder_adr=os.path.dirname(filename) |
63
|
|
|
filename_split=filename.split("/")[-1].split(".")[0] |
64
|
|
|
VCF_folder_adr = os.path.join(folder_adr, "VCF_CONVERT_" + filename_split) |
65
|
|
|
if "VCF_CONVERT_"+filename_split not in os.listdir(folder_adr): |
66
|
|
|
os.mkdir(VCF_folder_adr) |
67
|
|
|
return VCF_folder_adr |
68
|
|
|
|
69
|
|
|
def VCF_creator(folder_name,first_name,last_name,tel_mobile,tel_home,tel_work,email_home,email_work,email_mobile,adr_work,adr_home,website_url): |
70
|
|
|
file=open(os.path.join(folder_name,last_name+"_"+first_name+".vcf"),"w") |
71
|
|
|
VCF_init(file) |
72
|
|
|
VCF_name(file,first_name,last_name) |
73
|
|
|
VCF_phone(file,tel_mobile,tel_home,tel_work) |
74
|
|
|
VCF_email(file,email_home,email_mobile,email_work) |
75
|
|
|
VCF_adr(file,adr_work,adr_home) |
76
|
|
|
VCF_website(file,website_url) |
77
|
|
|
|
78
|
|
|
def csv_reader(file_name): |
79
|
|
|
try: |
80
|
|
|
file=open(file_name,"r") |
81
|
|
|
unknown_index=0 |
82
|
|
|
vcf_counter=0 |
83
|
|
|
foldername=VCF_Folder(file_name) |
84
|
|
|
for index,line in enumerate(file): |
85
|
|
|
if index>0: |
86
|
|
|
stripped_line=line.strip() |
87
|
|
|
temp=stripped_line.split(",") |
88
|
|
|
if len(temp)>11: |
89
|
|
|
print("[Warning] CSV File Line "+str(index)+" Bad Format") |
90
|
|
|
continue |
91
|
|
|
else: |
92
|
|
|
if len(temp[0])==0 and len(temp[1])==0: |
93
|
|
|
unknown_index+=1 |
94
|
|
|
VCF_creator(foldername,"Unknown ",str(unknown_index),temp[2],temp[3],temp[4],temp[5],temp[6],temp[7],temp[8],temp[9],temp[10]) |
95
|
|
|
else: |
96
|
|
|
VCF_creator(foldername,temp[0],temp[1],temp[2],temp[3],temp[4],temp[5],temp[6],temp[7],temp[8],temp[9],temp[10]) |
97
|
|
|
vcf_counter+=1 |
98
|
|
|
return vcf_counter |
99
|
|
|
|
100
|
|
|
except FileNotFoundError: |
101
|
|
|
print("[Warning] Please Open CSV File") |
102
|
|
|
except Exception as e: |
103
|
|
|
messagebox.showinfo("CSV2VCF", "Error In Reading Input File") |
104
|
|
|
print("[Error] In Reading Input File") |
105
|
|
|
|
106
|
|
|
|