Creating a multiple phone vCard using vObject
Asked Answered
S

2

8

im using vObject to create a vCard. Everything works well except I can't add multiple phone numbers.

Right now i'm doing this:

v.add('tel')
v.tel.type_param = 'WORK'
v.tel.value = employee.office_phone

v.add('tel')
v.tel.type_param = 'FAX'
v.tel.value = employee.fax

As it's working as a key value, the work phone is overwritten by the fax number.

Any idea on who to do it right?

Thanks!

Symbolize answered 25/11, 2012 at 16:13 Comment(1)
Maybe v.tel needs to accessed like a list or an array, like v.tel[0].type_param = 'WORK'. Or maybe v.add() returns an object, which is what you should assign the type_param and value to, like tel = v.add('tel'); tel.type_param = 'WORK'Londonderry
X
13

The add() method returns a specific object which can be used to fill in more data:

import vobject

j = vobject.vCard()
o = j.add('fn')
o.value = "Meiner Einer"

o = j.add('n')
o.value = vobject.vcard.Name( family='Einer', given='Meiner' )

o = j.add('tel')
o.type_param = "cell"
o.value = '+321 987 654321'

o = j.add('tel')
o.type_param = "work"
o.value = '+01 88 77 66 55'

o = j.add('tel')
o.type_param = "home"
o.value = '+49 181 99 00 00 00'

print(j.serialize())

Output:

BEGIN:VCARD
VERSION:3.0
FN:Meiner Einer
N:Einer;Meiner;;;
TEL;TYPE=cell:+321 987 654321
TEL;TYPE=work:+01 88 77 66 55
TEL;TYPE=home:+49 181 99 00 00 00
END:VCARD
Xantho answered 6/2, 2013 at 22:45 Comment(2)
How would I edit a current contact name? Right now the add function simply adds another line N. I simply want to change the current N line.Quietism
j.n should get you the current N lineAccommodative
T
0

I had the same problem and did this :

card = vobject.vCard()
        
card.add('fn')
card.fn.value = fullname
        
card.add('email')
card.email.value = email
card.email.type_param = 'INTERNET'
        
card.add('tel')
card.tel.value = cellphone
card.tel.type_param = 'CELL,VOICE,PREF'
        
if workphone != '' :
    card.tel_list.append(vobject.base.ContentLine('tel', [['TYPE:','work:']], workphone))
        
if fax != '' :
    card.tel_list.append(vobject.base.ContentLine('tel', [['TYPE:','fax:']], fax))
        
vcard_data = card.serialize()    
Tapetum answered 8/1, 2023 at 1:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.