Skip to content

Commit 93fd54b

Browse files
committed
Some more changes & making a proper Python module
1 parent fab5f45 commit 93fd54b

File tree

9 files changed

+112
-199
lines changed

9 files changed

+112
-199
lines changed

.gitignore

Lines changed: 4 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -3,108 +3,14 @@ __pycache__/
33
*.py[cod]
44
*$py.class
55

6-
# C extensions
7-
*.so
8-
9-
# Distribution / packaging
10-
.Python
11-
env/
12-
build/
13-
develop-eggs/
14-
dist/
15-
downloads/
16-
eggs/
17-
.eggs/
18-
lib/
19-
lib64/
20-
parts/
21-
sdist/
22-
var/
23-
wheels/
24-
*.egg-info/
25-
.installed.cfg
26-
*.egg
27-
28-
# PyInstaller
29-
# Usually these files are written by a python script from a template
30-
# before PyInstaller builds the exe, so as to inject date/other infos into it.
31-
*.manifest
32-
*.spec
33-
34-
# Installer logs
35-
pip-log.txt
36-
pip-delete-this-directory.txt
37-
38-
# Unit test / coverage reports
39-
htmlcov/
40-
.tox/
41-
.coverage
42-
.coverage.*
43-
.cache
44-
nosetests.xml
45-
coverage.xml
46-
*.cover
47-
.hypothesis/
48-
49-
# Translations
50-
*.mo
51-
*.pot
52-
53-
# Django stuff:
54-
*.log
55-
local_settings.py
56-
57-
# Flask stuff:
58-
instance/
59-
.webassets-cache
60-
61-
# Scrapy stuff:
62-
.scrapy
63-
64-
# Sphinx documentation
65-
docs/_build/
66-
67-
# PyBuilder
68-
target/
69-
70-
# Jupyter Notebook
71-
.ipynb_checkpoints
72-
73-
# pyenv
74-
.python-version
75-
76-
# celery beat schedule file
77-
celerybeat-schedule
78-
79-
# SageMath parsed files
80-
*.sage.py
81-
82-
# dotenv
83-
.env
84-
85-
# virtualenv
86-
.venv
87-
venv/
88-
ENV/
89-
90-
# Spyder project settings
91-
.spyderproject
92-
.spyproject
93-
94-
# Rope project settings
95-
.ropeproject
96-
97-
# mkdocs documentation
98-
/site
99-
100-
# mypy
101-
.mypy_cache/
102-
1036
# pycharm
1047
.idea
1058

1069
# vcards
10710
*.vcf
10811

10912
# csvs
110-
*.csv
13+
*.csv
14+
15+
# Export folder
16+
export

README.md

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
# python-csv-to-vcard
2-
A Python script that parses a .csv file of contacts and automatically creates vCards. The vCards are super useful for sending your contact details or those of your team. You can also upload them to e.g. Dropbox and use them with QR codes! You can also use them for transering new contacts to Outlook, a new CRM etc. The specific use case in mind was to programmatically create vCards from a list of contacts in a spreadsheet, to be incorporated into business cards.
1+
# csv2vcard
2+
A Python script that parses a .csv file of contacts and automatically creates vCards. The vCards are super useful for sending your contact details or those of your team. You can also upload them to e.g. Dropbox and use them with QR codes! You can also use them for transferring new contacts to Outlook, a new CRM etc. The specific use case in mind was to programmatically create vCards from a list of contacts in a spreadsheet, to be incorporated into business cards.
33

44
# Usage
55

@@ -13,10 +13,13 @@ A Python script that parses a .csv file of contacts and automatically creates vC
1313

1414
**Important: you should name the columns exactly the same way because they are used as keys to generate the vCards**
1515

16-
2. Download `csv_to_vcard.py`
16+
2. `git clone` or download and `cd` to folder
1717

1818
3. Open python `python3` (gotcha: using Python 3.6 features)
1919

20-
4. Import module `import csv_to_vcard`
20+
4. Import module `from csv2vcard import csv2vcard`
2121

22-
5. Run script `csv_to_vcard.csv_to_vcard(your_file_name)`. This will create an /export/ dir with your vCards!
22+
5. Now you have 2 options for running. Both will create an /export/ dir with an example vCards.
23+
24+
* Test the app with `csv2vcard.test_csv2vcard()`. This will create a Forrest Gump test vCard!
25+
* Use your real data `csv2vcard.csv2vcard(yourfilename)`. This will create all your vCards!

csv-to-vcard/csv_to_vcard.py

Lines changed: 0 additions & 92 deletions
This file was deleted.
File renamed without changes.

csv2vcard/create_vcard.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
def create_vcard(contact: dict):
2+
"""
3+
The mappings used below are from https://www.w3.org/TR/vcard-rdf/#Mapping
4+
"""
5+
vc_begin = "BEGIN:VCARD\n"
6+
vc_version = "VERSION:3.0\n"
7+
vc_name = f"N;CHARSET=UTF-8:{contact['last_name']};{contact['first_name']};;;\n"
8+
vc_title = f"TITLE;CHARSET=UTF-8:{contact['title']}\n"
9+
vc_org = f"ORG;CHARSET=UTF-8:{contact['org']}\n"
10+
vc_phone = f"TEL;TYPE=WORK,VOICE:{contact['phone']}\n"
11+
vc_email = f"EMAIL;TYPE=WORK:{contact['email']}\n"
12+
vc_website = f"URL;TYPE=WORK:{contact['website']}\n"
13+
vc_address = f"ADR;TYPE=WORK;CHARSET=UTF-8:{contact['street']};{contact['city']};{contact['p_code']};{contact['country']}\n"
14+
vc_end = "END:VCARD\n"
15+
16+
vc_filename = f"{contact['last_name'].lower()}_{contact['first_name'].lower()}.vcf"
17+
vc_output = vc_begin + vc_version + vc_name + vc_title + vc_org + vc_phone + vc_email + vc_website + vc_address + vc_end
18+
19+
vc_final = {
20+
"filename" : vc_filename,
21+
"output" : vc_output,
22+
"name" : contact['first_name'] + contact['last_name'],
23+
}
24+
25+
return vc_final

csv2vcard/csv2vcard.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
from csv2vcard.export_vcard import check_export, export_vcard
2+
from csv2vcard.create_vcard import create_vcard
3+
from csv2vcard.parse_csv import parse_csv
4+
5+
6+
def csv2vcard(csv_filename: str):
7+
"""
8+
Main function
9+
"""
10+
check_export()
11+
12+
for c in parse_csv(csv_filename):
13+
vcard = create_vcard(c)
14+
export_vcard(vcard)
15+
16+
17+
def test_csv2vcard():
18+
"""
19+
Try it out with this mock Forrest Gump contact
20+
"""
21+
mock_contacts = [{
22+
"last_name": "Gump", "first_name": "Forrest", "title": "Shrimp Man",
23+
"org": "Bubba Gump Shrimp Co.",
24+
"phone": "+49 170 5 25 25 25", "email": "[email protected]",
25+
"website": "https://www.linkedin.com/in/forrestgump",
26+
"street": "42 Plantation St.", "city": "Baytown", "p_code": "30314",
27+
"country": "United States of America"
28+
}]
29+
check_export()
30+
vcard = create_vcard(mock_contacts[0])
31+
print(vcard)
32+
export_vcard(vcard)

csv2vcard/export_vcard.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import os
2+
3+
def export_vcard(vc_final):
4+
"""
5+
Exporting a vCard to /export/
6+
"""
7+
try:
8+
with open(f"export/{vc_final['filename']}", "w") as f:
9+
f.write(vc_final['output'])
10+
f.close()
11+
print(f"Created vCard 3.0 for {vc_final['name']}.")
12+
except IOError:
13+
print(f"I/O error for {vc_final['filename']}")
14+
15+
16+
def check_export():
17+
"""
18+
Checks if export folder exists in directory
19+
"""
20+
if not os.path.exists("export"):
21+
print("Creating /export folder...")
22+
os.makedirs("export")

csv2vcard/parse_csv.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import csv
2+
import codecs
3+
4+
def parse_csv(csv_filename: str):
5+
"""
6+
Simple csv parser with a ; delimiter
7+
"""
8+
print("Parsing csv..")
9+
try:
10+
with codecs.open(f"{csv_filename}", "r", "utf-8-sig") as f:
11+
contacts = csv.reader(f, delimiter=";")
12+
header = next(contacts) # saves header
13+
parsed_contacts = [dict(zip(header, row)) for row in contacts]
14+
return parsed_contacts
15+
except IOError:
16+
print(f"I/O error for {csv_filename}")
17+
return []

setup.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
from distutils.core import setup
22
setup(
3-
name = 'csv-to-vcard',
4-
packages = ['csv-to-vcard'], # this must be the same as the name above
3+
name = 'csv2vcard',
4+
packages = ['csv2vcard'], # this must be the same as the name above
55
version = '0.1',
66
description = 'A library for converting csvs to vCards',
77
author = 'Nikolay Dimolarov',
88
author_email = '[email protected]',
9-
url = 'https://github.com/tech4242/python-csv-to-vcard', # use the URL to the github repo
10-
download_url = 'https://github.com/peterldowns/mypackage/archive/0.1.tar.gz', # I'll explain this in a second
9+
url = 'https://github.com/tech4242/csv2vcard', # use the URL to the github repo
10+
download_url = 'https://github.com/tech4242/csv2vcard/archive/0.1.tar.gz', # I'll explain this in a second
1111
keywords = ['csv', 'vcard', 'export'],
1212
classifiers = [],
1313
)

0 commit comments

Comments
 (0)