Spaces:
Sleeping
Sleeping
SarowarSaurav
commited on
Commit
•
1a4514d
1
Parent(s):
539accd
Update app.py
Browse files
app.py
CHANGED
@@ -1,67 +1,61 @@
|
|
1 |
-
import pandas as pd
|
2 |
import gradio as gr
|
|
|
|
|
|
|
3 |
|
4 |
-
def
|
5 |
-
|
6 |
-
|
7 |
-
PRODID:-//Apple Inc.//iPhone OS 17.2.1//EN
|
8 |
-
N:{name}
|
9 |
-
ORG:BAT Bangladesh;{function}
|
10 |
-
TITLE:{designation}
|
11 |
-
TEL;type=CELL;type=VOICE;type=pref:{number}
|
12 |
-
EMAIL:{email}
|
13 |
-
END:VCARD"""
|
14 |
-
return vcf_template.format(
|
15 |
-
name=row['Name'],
|
16 |
-
function=row['Function'],
|
17 |
-
designation=row['Designation'],
|
18 |
-
number=row['Number'],
|
19 |
-
email=row['Email']
|
20 |
-
)
|
21 |
-
|
22 |
-
def generate_vcf(file):
|
23 |
-
df = pd.read_excel(file)
|
24 |
-
df['Number'] = df['Number'].astype(str) # Ensure phone numbers are strings
|
25 |
-
vcf_data = df.apply(create_vcf, axis=1).str.cat(sep='\n\n')
|
26 |
|
27 |
-
|
28 |
-
|
29 |
-
vcf_file.write(vcf_data)
|
30 |
-
|
31 |
-
return vcf_file_name
|
32 |
|
33 |
-
#
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
51 |
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
</div>
|
56 |
-
<p style="text-align: center; color: #fff;">Upload an Excel file containing contact information in the following format: Name, Designation, Function, GRADE, Email, Number. The output will be a VCF file containing the contact information.</p>
|
57 |
-
"""
|
58 |
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
|
|
66 |
|
67 |
-
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
import pandas as pd
|
3 |
+
import vobject
|
4 |
+
from io import BytesIO
|
5 |
|
6 |
+
def excel_to_vcf(file):
|
7 |
+
# Load Excel file
|
8 |
+
df = pd.read_excel(file.name)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
+
# Prepare a dictionary to check for existing emails
|
11 |
+
contact_dict = {}
|
|
|
|
|
|
|
12 |
|
13 |
+
# Create a vCard file in memory
|
14 |
+
vcf_output = BytesIO()
|
15 |
+
|
16 |
+
# Loop through each row in the DataFrame
|
17 |
+
for _, row in df.iterrows():
|
18 |
+
name = row['Name']
|
19 |
+
designation = row['Designation']
|
20 |
+
function = row['Function']
|
21 |
+
grade = row['GRADE']
|
22 |
+
email = row['Email']
|
23 |
+
number = str(row['Number'])
|
24 |
+
|
25 |
+
# If email already exists, replace the contact details
|
26 |
+
if email in contact_dict:
|
27 |
+
contact_dict[email]['tel'].value = number
|
28 |
+
else:
|
29 |
+
# Create a new vCard entry
|
30 |
+
contact = vobject.vCard()
|
31 |
+
contact.add('fn').value = name
|
32 |
+
contact.add('title').value = designation
|
33 |
+
contact.add('org').value = function
|
34 |
+
contact.add('note').value = f"GRADE: {grade}"
|
35 |
+
|
36 |
+
email_entry = contact.add('email')
|
37 |
+
email_entry.value = email
|
38 |
+
email_entry.type_param = 'INTERNET'
|
39 |
+
|
40 |
+
tel_entry = contact.add('tel')
|
41 |
+
tel_entry.value = number
|
42 |
+
tel_entry.type_param = 'CELL'
|
43 |
+
|
44 |
+
# Add contact to dictionary and write to VCF
|
45 |
+
contact_dict[email] = contact
|
46 |
+
vcf_output.write(contact.serialize().encode('utf-8'))
|
47 |
|
48 |
+
# Return the generated VCF file
|
49 |
+
vcf_output.seek(0)
|
50 |
+
return vcf_output, "contacts.vcf"
|
|
|
|
|
|
|
51 |
|
52 |
+
# Set up Gradio interface
|
53 |
+
iface = gr.Interface(
|
54 |
+
fn=excel_to_vcf,
|
55 |
+
inputs=gr.inputs.File(label="Upload Excel File (Format: Name, Designation, Function, GRADE, Email, Number)"),
|
56 |
+
outputs=gr.outputs.File(label="Download VCF File"),
|
57 |
+
title="Excel to VCF Converter",
|
58 |
+
description="Upload an Excel file with contact information to generate a VCF file. Contacts with matching email IDs will have their phone numbers updated."
|
59 |
+
)
|
60 |
|
61 |
+
iface.launch()
|