Spaces:
Sleeping
Sleeping
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() | |