khard icon indicating copy to clipboard operation
khard copied to clipboard

Simple way to import vcf card into an addressbook

Open agx opened this issue 4 months ago • 1 comments

Maybe I'm missing something obvious but I didn't find a way to import a vcf file (as is e.g. ever so often attached to an email) into an addressbook. E.g. khal has khal import <icsfile> to import calendar entries. Would be nice to have something similar for khard.

I tried

khard new -i foo.vcf

but that raises an exception:

$ khard new -i friscobrisco.vcf 
Wrapping unversioned vCard object, setting version to 3.0.
Traceback (most recent call last):
  File "/usr/bin/khard", line 8, in <module>
    sys.exit(main())
             ~~~~^^
  File "/usr/lib/python3/dist-packages/khard/khard.py", line 1182, in main
    new_subcommand(args.addressbook, input_from_stdin_or_file,
    ~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
                   args.open_editor)
                   ^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3/dist-packages/khard/khard.py", line 390, in new_subcommand
    new_contact = CarddavObject.from_yaml(
        abook, data, config.private_objects,
        config.preferred_vcard_version, config.localize_dates)
  File "/usr/lib/python3/dist-packages/khard/carddav_object.py", line 1389, in from_yaml
    contact.update(yaml)
    ~~~~~~~~~~~~~~^^^^^^
  File "/usr/lib/python3/dist-packages/khard/carddav_object.py", line 1060, in update
    contact_data = self._parse_yaml(input)
  File "/usr/lib/python3/dist-packages/khard/carddav_object.py", line 996, in _parse_yaml
    if not (contact_data.get("First name") or contact_data.get("Last name")
            ^^^^^^^^^^^^^^^^
AttributeError: 'str' object has no attribute 'get'

agx avatar Sep 04 '25 10:09 agx

Yes an import of vcf files is not yet implemented. The -i/--input-file currently only accepts yaml template files.

(I am currently copying vcf files to my address book folder by hand, but that obviously does not check them for correctness in any way.)

Pull Requests for this will be welcome. I had some external discussion with @scheibler about this some time ago, so here are some notes, if somebody wants to implement this:

  • we should add a new option --format to the new subcommand
  • the option should accept yaml and vcard (like for show)
  • the format option should be optional, the default is yaml, this means the new subcommand is fully backwards compatible

lucc avatar Sep 17 '25 20:09 lucc