-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvcard_csv.py
More file actions
42 lines (32 loc) · 1.17 KB
/
Copy pathvcard_csv.py
File metadata and controls
42 lines (32 loc) · 1.17 KB
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
"""Convert a vCard file to CSV.
Usage: python3.14 vcard_csv.py INPUT.vcf [-f OUTPUT.csv]
Requires the ``pyvcard`` package.
"""
from __future__ import annotations
import argparse
import sys
from pathlib import Path
import pyvcard
def view(path: str | Path) -> str:
"""Return vCards from *path* in CSV format."""
cards = pyvcard.openfile(str(path), encoding="utf-8").vcards()
return str(pyvcard.convert(cards).csv().permanent_result())
def main(argv: list[str] | None = None) -> int:
parser = argparse.ArgumentParser(description="Convert a vCard file to CSV.")
parser.add_argument("file", type=Path, help="input vCard file")
parser.add_argument("-f", "--output", type=Path, help="output CSV file")
args = parser.parse_args(argv)
if not args.file.is_file():
parser.error(f"file not found: {args.file}")
try:
csv_text = view(args.file)
if args.output:
args.output.write_text(csv_text, encoding="utf-8")
else:
print(csv_text)
except (OSError, ValueError) as exc:
print(f"Error: {exc}", file=sys.stderr)
return 1
return 0
if __name__ == "__main__":
raise SystemExit(main())