import os import pandas as pd from typing import List, Optional def validate_phone_number(number: str) -> bool: """Validate if number contains only digits and has appropriate length.""" cleaned = ''.join(filter(str.isdigit, str(number))) return len(cleaned) >= 10 and cleaned.isdigit() def create_vcf_contacts(excel_path: str, group_name: str = "Contacts") -> Optional[str]: """Create VCF file from Excel containing phone numbers.""" try: # Check if file exists if not os.path.exists(excel_path): raise FileNotFoundError(f"Excel file not found: {excel_path}") # Read phone numbers from first column of Excel file df = pd.read_excel(excel_path) if df.empty: raise ValueError("Excel file is empty") numbers = df.iloc[:, 0].astype(str).tolist() filename = f"{group_name}_contacts.vcf" valid_numbers = 0 skipped_numbers = 0 with open(filename, 'w') as f: for i, number in enumerate(numbers, start=1): if not validate_phone_number(number) or not number.isdigit(): skipped_numbers += 1 continue try: f.write("BEGIN:VCARD\n") f.write("VERSION:3.0\n") f.write(f"N:{group_name}_{i:03d};;;\n") f.write(f"FN:{group_name}_{i:03d}\n") f.write(f"TEL;TYPE=CELL:+91{number}\n") f.write(f"X-WhatsApp-Group:{group_name}\n") f.write("END:VCARD\n\n") valid_numbers += 1 except IOError: raise return filename except pd.errors.EmptyDataError: raise except Exception: raise if __name__ == "__main__": try: created_file = create_vcf_contacts('numbers.xlsx') except Exception as e: print(f"Failed to create contacts file: {str(e)}")