File size: 2,050 Bytes
e3a012f
1a4514d
 
 
e3a012f
1a4514d
 
 
e3a012f
1a4514d
 
e3a012f
1a4514d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e3a012f
1a4514d
 
 
e3a012f
1a4514d
 
 
 
 
 
 
 
e3a012f
1a4514d
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
import gradio as gr
import pandas as pd
import vobject
from io import BytesIO

def excel_to_vcf(file):
    # Load Excel file
    df = pd.read_excel(file.name)
    
    # Prepare a dictionary to check for existing emails
    contact_dict = {}

    # Create a vCard file in memory
    vcf_output = BytesIO()
    
    # Loop through each row in the DataFrame
    for _, row in df.iterrows():
        name = row['Name']
        designation = row['Designation']
        function = row['Function']
        grade = row['GRADE']
        email = row['Email']
        number = str(row['Number'])
        
        # If email already exists, replace the contact details
        if email in contact_dict:
            contact_dict[email]['tel'].value = number
        else:
            # Create a new vCard entry
            contact = vobject.vCard()
            contact.add('fn').value = name
            contact.add('title').value = designation
            contact.add('org').value = function
            contact.add('note').value = f"GRADE: {grade}"
            
            email_entry = contact.add('email')
            email_entry.value = email
            email_entry.type_param = 'INTERNET'
            
            tel_entry = contact.add('tel')
            tel_entry.value = number
            tel_entry.type_param = 'CELL'
            
            # Add contact to dictionary and write to VCF
            contact_dict[email] = contact
            vcf_output.write(contact.serialize().encode('utf-8'))

    # Return the generated VCF file
    vcf_output.seek(0)
    return vcf_output, "contacts.vcf"

# Set up Gradio interface
iface = gr.Interface(
    fn=excel_to_vcf,
    inputs=gr.inputs.File(label="Upload Excel File (Format: Name, Designation, Function, GRADE, Email, Number)"),
    outputs=gr.outputs.File(label="Download VCF File"),
    title="Excel to VCF Converter",
    description="Upload an Excel file with contact information to generate a VCF file. Contacts with matching email IDs will have their phone numbers updated."
)

iface.launch()