看到有人问怎么转换,自己生成了一个vcf文件,学习到了QUOTED-PRINTABLE编码,就顺手写了一个简单的转换代码,不过只能转换名字和电话号码,电话的格式也不知道全不全,我的手机联系人不多,一共就只有三种,也没存邮箱,如果有需求大家应该也能简单改出来,代码如下:
import quopri
import xlwt
# 请更改为自己的vcf文件,我的从三星手机中导出
file=open('lxr.vcf','rb')
book=xlwt.Workbook(encoding="utf-8",style_compression=0)
sheet = book.add_sheet('contact', cell_overwrite_ok=True)
people_list = list()
phone_list = list()
for line in file:
line = str(line,encoding = "utf8")
if 'BEGIN:VCARD' in line:
phone_list=list()
if 'N;CHARSET=UTF-8;ENCODING=QUOTED-PRINTABLE:' in line and 'FN' not in line:
try:
name=quopri.decodestring(line[line.index('N;CHARSET=UTF-8;ENCODING=QUOTED-PRINTABLE:')+42:line.index(';;;')]).decode()
except:
line = line+str(file.readline(),encoding = "utf8")
name=quopri.decodestring(line[line.index('N;CHARSET=UTF-8;ENCODING=QUOTED-PRINTABLE:')+42:line.index(';;;')]).decode()
name=name.replace(';','')
# 如果有其他前缀按下面的方式添加,邮箱应该也同理
if 'TEL;CELL:' in line:
phone_list.append(line[line.index('TEL;CELL:')+len('TEL;CELL:'):-2])
if 'TEL;HOME:' in line:
phone_list.append(line[line.index('TEL;HOME:')+len('TEL;HOME:'):-2])
if 'TEL;CELL;PREF:' in line:
phone_list.append(line[line.index('TEL;CELL;PREF:')+len('TEL;CELL;PREF:'):-2])
if 'END:VCARD' in line:
people_list.append((name,phone_list))
for i in range(len(people_list)):
sheet.write(i,0,people_list[i][0])
for j in range(len(people_list[i][1])):
sheet.write(i,j+1,people_list[i][1][j])
book.save('concat.xls')
另祝大家在家学习愉快~
本文分享了一段Python代码,用于将VCF格式的联系人数据转换为Excel表格,包括姓名和电话号码等信息,适用于三星手机导出的联系人文件。
4097

被折叠的 条评论
为什么被折叠?



