Update vcf_gen.py
This commit is contained in:
parent
fdc207f150
commit
23c36f13ce
57
vcf_gen.py
57
vcf_gen.py
@ -1,24 +1,57 @@
|
|||||||
import os
|
import os
|
||||||
|
import pandas as pd
|
||||||
|
from typing import List, Optional
|
||||||
|
|
||||||
numbers = []
|
def validate_phone_number(number: str) -> bool:
|
||||||
group_name = input("Enter the group name: ")
|
"""Validate if number contains only digits and has appropriate length."""
|
||||||
|
cleaned = ''.join(filter(str.isdigit, str(number)))
|
||||||
|
return len(cleaned) >= 10 and cleaned.isdigit()
|
||||||
|
|
||||||
while True:
|
def create_vcf_contacts(excel_path: str, group_name: str = "Contacts") -> Optional[str]:
|
||||||
number = input("Enter a phone number (or press Enter to finish): ")
|
"""Create VCF file from Excel containing phone numbers."""
|
||||||
if number == "":
|
try:
|
||||||
break
|
# Check if file exists
|
||||||
numbers.append(number)
|
if not os.path.exists(excel_path):
|
||||||
|
raise FileNotFoundError(f"Excel file not found: {excel_path}")
|
||||||
|
|
||||||
filename = f"{group_name}_contacts.vcf"
|
# Read phone numbers from first column of Excel file
|
||||||
with open(filename, 'w') as f:
|
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):
|
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("BEGIN:VCARD\n")
|
||||||
f.write("VERSION:3.0\n")
|
f.write("VERSION:3.0\n")
|
||||||
f.write(f"N:{group_name}_{i:03d};;;\n")
|
f.write(f"N:{group_name}_{i:03d};;;\n")
|
||||||
f.write(f"FN:{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"TEL;TYPE=CELL:+91{number}\n")
|
||||||
f.write(f"X-WhatsApp-Group:{group_name}\n") # Add WhatsApp group tag
|
f.write(f"X-WhatsApp-Group:{group_name}\n")
|
||||||
f.write("END:VCARD\n\n")
|
f.write("END:VCARD\n\n")
|
||||||
|
valid_numbers += 1
|
||||||
|
except IOError:
|
||||||
|
raise
|
||||||
|
|
||||||
print(f"A single VCF file '{filename}' with {len(numbers)} contacts has been created in the current directory.")
|
return filename
|
||||||
print(f"These contacts are tagged for easy addition to a WhatsApp group named '{group_name}'.")
|
|
||||||
|
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)}")
|
Loading…
Reference in New Issue
Block a user