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 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
|
import sys import csv from vobject import readComponents
vcf_file = '~/Downloads/vcard-export/alle-kontakte.vcf' addressbook_file = '~/Downloads/vcard-export/addressbook.abook'
try: with open(vcf_file, 'r') as f: vcards = list(readComponents(f)) except Exception as e: print(f"Error reading {vcf_file}: {e}") sys.exit(1)
def get_first_value(obj, attribute): if hasattr(obj, attribute): attr = getattr(obj, attribute) if isinstance(attr, list): return attr[0].value return attr.value return 'N/A'
def get_all_values(obj, attribute): if hasattr(obj, attribute): attr = getattr(obj, attribute) if isinstance(attr, list): return ', '.join([a.value for a in attr]) return attr.value return 'N/A'
def get_address_component(adr, component): return getattr(adr, component, '')
def get_address(vcard): if hasattr(vcard, 'adr'): adr = vcard.adr.value address = get_address_component(adr, 'street') address2 = '' city = get_address_component(adr, 'city') state = get_address_component(adr, 'region') zip_code = get_address_component(adr, 'code') country = get_address_component(adr, 'country') return address, address2, city, state, zip_code, country return 'N/A', 'N/A', 'N/A', 'N/A', 'N/A', 'N/A'
with open(addressbook_file, 'w') as abookfile: abookfile.write('[format]\n') abookfile.write('program=abook\n') abookfile.write('version=0.6.1\n\n')
for i, vcard in enumerate(vcards): name = get_first_value(vcard, 'fn') email = get_all_values(vcard, 'email') address, address2, city, state, zip_code, country = get_address(vcard) phone = get_all_values(vcard, 'tel') workphone = get_first_value(vcard, 'workphone') fax = get_first_value(vcard, 'fax') mobile = get_first_value(vcard, 'cell') nickname = get_first_value(vcard, 'nickname') url = get_first_value(vcard, 'url') notes = get_first_value(vcard, 'note') anniversary = get_first_value(vcard, 'bday') groups = get_all_values(vcard, 'categories')
abookfile.write(f'[{i}]\n') abookfile.write(f'name={name}\n') abookfile.write(f'email={email}\n') if address != 'N/A': abookfile.write(f'address={address}\n') if address2 != 'N/A': abookfile.write(f'address2={address2}\n') if city != 'N/A': abookfile.write(f'city={city}\n') if state != 'N/A': abookfile.write(f'state={state}\n') if zip_code != 'N/A': abookfile.write(f'zip={zip_code}\n') if country != 'N/A': abookfile.write(f'country={country}\n') if phone != 'N/A': abookfile.write(f'phone={phone}\n') if workphone != 'N/A': abookfile.write(f'workphone={workphone}\n') if fax != 'N/A': abookfile.write(f'fax={fax}\n') if mobile != 'N/A': abookfile.write(f'mobile={mobile}\n') if nickname != 'N/A': abookfile.write(f'nick={nickname}\n') if url != 'N/A': abookfile.write(f'url={url}\n') if notes != 'N/A': abookfile.write(f'notes={notes}\n') if anniversary != 'N/A': abookfile.write(f'anniversary={anniversary}\n') if groups != 'N/A': abookfile.write(f'groups={groups}\n') abookfile.write('\n')
print(f"Successfully written data to {addressbook_file}")
|