Extracting addresses from Nextcloud

I’d like to export mailing addresses for ~100 contacts from my address books in Nextcloud for sending holiday cards. This is a “one-off”, I don’t need to make anything robust and repeatable. I’m trying to come up with a text file with something like this:

Stalfia Domingo
400 28th Ave
New York, NY 10010

Bolognia Blepharia
756 28th St. S.
Chicago, IL 31029

or a CSV file resembling:

full name,street address,city/township,state/province,USA zip code
Stalfia Domingo,400 28th Ave,New York,NY,10010
Bolognia Blepharia,756 28th St. S.,Chicago,IL,31029

I skimmed the schema and only saw what looked like entire vcards in oc_cards (those would be tricky to work with). I also looked at the occ tool but I didn’t see any obvious ways to export [parts of] contacts. I tried the “Download” option from the contacts app and found the giant list of vcards a little daunting to work with.

Someone in chat recommended trying to export from my contacts client, but gnome-contacts doesn’t seem to have any export option. Android contacts does, but it just exports the same large vcards file I can get from the web UI.

Looks like CSV export/import is not supported in nextcloud server yet: Export contact group in csv .

Contacts app - How to backup/export full addressbook? is also relevant, and links to a VCF to CSV tool: https://sourceforge.net/projects/bulkvcftocsv/ … but I don’t have Excel. I also found https://github.com/nbeaver/vcard2csv but it doesn’t export addresses and it looks like VCF address parsing is nontrivial/messy.

Clients used with Nextcloud Contacts: Web (Firefox or Chromium), Android, gnome-contacts, (I could also use PHP, Python, Bash, occ, or query the database directly)

Operating system and version: Ubuntu 20.04 LTS 64-bit server
Nextcloud version: 20.0.2 (from nextcloud:20.0-apache Docker image)
The base image is php:7.4-apache-buster, I’m not sure which Apache or PHP that has inside.

I got the first version working (dump VCF as a name/address text file) with Python and vobject.

Credit to https://matrix.to/#/@amgine:matrix.org and https://matrix.to/#/@florian:datensch.eu in the Nextcloud chat for ideas and help!

Step 1: in Nextcloud, add contacts I want addresses for to the “Holiday Card” group.

Step 2: download the “Holiday Card” group from Nextcloud.

Step 3: run this Python script on the .vcf downloaded from Nextcloud:

import sys
import vobject

with open(sys.argv[1]) as fp:
    vCard_text = fp.read()

for vCard in vobject.readComponents(vCard_text):
    vCard.validate()
    print(vCard.fn.value)
    print(vCard.adr.value)
    print()
2 Likes