Skip to content

Instantly share code, notes, and snippets.

@PrasadNR
Created January 11, 2020 19:17
Show Gist options
  • Save PrasadNR/31328d2036e3603ea9853729127f499c to your computer and use it in GitHub Desktop.
Save PrasadNR/31328d2036e3603ea9853729127f499c to your computer and use it in GitHub Desktop.
Automatically add country code to phone numbers in vCalendar File wherever that is absent (I wrote it after moving to USA from India). Minor changes may be needed for different cases/countries/versions of VCF. As I only found paid apps for this, I wrote this one after analysing patterns for about 2 hours.
import re
with open('PIM00004.vcf', 'r') as f:
vcfText = f.read()
regex = re.compile(r'[0-9-]+')
regexPhoneNumbers = regex.findall(vcfText)
for eachRegexPhoneNumber in regexPhoneNumbers:
tenDigitPhoneNumber = eachRegexPhoneNumber.replace("-", "").replace(" ", "").replace("(", "").replace(")", "")
if len(tenDigitPhoneNumber) == 10:
phoneNumber = "+91" + tenDigitPhoneNumber
vcfText = vcfText.replace(eachRegexPhoneNumber, phoneNumber)
with open('contacts.vcf', 'w') as f:
f.write(vcfText)
@PrasadNR
Copy link
Author

PrasadNR commented Jan 11, 2020

Also, I allow this particular Github Gist code to be used in any proprietary/free/open-source/closed-source/military applications. However, I am not liable for any of the negative consequences of the usage of the aforementioned code.

Also, the code is nice and short because, I have used the property of unique phone numbers. However, if that changes (multiple people owning same phone number with different formats including hyphens and spaces) in the future versions of VCF file (which I strongly believe won't happen), then, in that rare case, the aforementioned code might not work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment