用几句话让人工智能豆包帮你用Python程序excel转VCF通讯录格式程序

大家好,这是我第一次写关于分享程序实现的文章,废话少说下面是我和豆包AI对话实现的全过程,开整

在这里插入图片描述

初代
```import pandas as pd

def excel_to_vcf(excel_file_path, vcf_file_path):
    # 读取Excel文件
    df = pd.read_excel(excel_file_path)

    vcf_content = []

    for index, row in df.iterrows():
        name = row['姓名']
        phone_number = row['电话号码']

        vcf_entry = f"BEGIN:VCARD\nVERSION:3.0\nFN:{name}\nTEL;TYPE=CELL:{phone_number}\nEND:VCARD\n"
        vcf_content.append(vcf_entry)

    with open(vcf_file_path, 'w') as vcf_file:
        vcf_file.writelines(vcf_content)

if __name__ == "__main__":
    excel_file_path = "your_excel_file.xlsx"  # 替换为实际的Excel文件路径
    vcf_file_path = "output.vcf"  # 替换为输出的VCF文件路径
    excel_to_vcf(excel_file_path, vcf_file_path)

在这里插入图片描述

迭代版本1
```import pandas as pd
from tkinter import Tk
from tkinter.filedialog import askopenfilename, asksaveasfilename


def excel_to_vcf(excel_file_path, vcf_file_path):
    # 读取Excel文件
    df = pd.read_excel(excel_file_path)

    vcf_content = []

    for index, row in df.iterrows():
        name = row['姓名']
        phone_number = row['电话号码']

        vcf_entry = f"BEGIN:VCARD\nVERSION:3.0\nFN:{name}\nTEL;TYPE=CELL:{phone_number}\nEND:VCARD\n"
        vcf_content.append(vcf_entry)

    with open(vcf_file_path, 'w') as vcf_file:
        vcf_file.writelines(vcf_content)


if __name__ == "__main__":
    root = Tk()
    root.withdraw()

    # 打开Excel文件对话框选择文件
    excel_file_path = askopenfilename(title="选择Excel文件", filetypes=[("Excel Files", "*.xlsx;*.xls")])
    if not excel_file_path:
        print("未选择Excel文件,程序退出。")
        exit()

    # 打开保存VCF文件对话框指定保存路径
    vcf_file_path = asksaveasfilename(title="保存VCF文件", defaultextension=".vcf", filetypes=[("VCF Files", "*.vcf")])
    if not vcf_file_path:
        print("未指定VCF文件保存路径,程序退出。")
        exit()

    excel_to_vcf(excel_file_path, vcf_file_path)

在这里插入图片描述

迭代版本2
import pandas as pd
from tkinter import Tk
from tkinter.filedialog import askopenfilename, asksaveasfilename


def excel_to_vcf(excel_file_path, vcf_file_path):
    # 读取Excel文件
    df = pd.read_excel(excel_file_path)

    vcf_content = []

    for index, row in df.iterrows():
        name = row['姓名']
        phone_number = row['电话号码']

        vcf_entry = f"BEGIN:VCARD\nVERSION:3.0\nFN:{name}\nTEL;TYPE=CELL:{phone_number}\nEND:VCARD\n"
        vcf_content.append(vcf_entry)

    with open(vcf_file_path, 'w', encoding='GB2312') as vcf_file:
        vcf_file.writelines(vcf_content)


if __name__ == "__main__":
    root = Tk()
    root.withdraw()

    # 打开Excel文件对话框选择文件
    excel_file_path = askopenfilename(title="选择Excel文件", filetypes=[("Excel Files", "*.xlsx;*.xls")])
    if not excel_file_path:
        print("未选择Excel文件,程序退出。")
        exit()

    # 打开保存VCF文件对话框指定保存路径
    vcf_file_path = asksaveasfilename(title="保存VCF文件", defaultextension=".vcf", filetypes=[("VCF Files", "*.vcf")])
    if not vcf_file_path:
        print("未指定VCF文件保存路径,程序退出。")
        exit()

    excel_to_vcf(excel_file_path, vcf_file_path)

在这里插入图片描述

要修改的代码
with open(vcf_file_path, 'w', encoding='GB2312') as vcf_file:
    vcf_file.writelines(vcf_content)

在这里插入图片描述

with open(vcf_file_path, 'w', encoding='UTF - 8') as vcf_file:
    vcf_file.writelines(vcf_content)

在这里插入图片描述
最后的成品源代码-直接拿去用就可以了

嘎嘎好用的最终版
import pandas as pd
from tkinter import Tk
from tkinter.filedialog import askopenfilename, asksaveasfilename


def excel_to_vcf(excel_file_path, vcf_file_path):
    # 读取Excel文件
    df = pd.read_excel(excel_file_path)

    vcf_content = []

    for index, row in df.iterrows():
        name = row['姓名']
        phone_number = row['电话号码']

        vcf_entry = f"BEGIN:VCARD\nVERSION:3.0\nFN:{name}\nTEL;TYPE=CELL:{phone_number}\nEND:VCARD\n"
        vcf_content.append(vcf_entry)

    with open(vcf_file_path, 'w', encoding='UTF - 8') as vcf_file:
        vcf_file.writelines(vcf_content)


if __name__ == "__main__":
    root = Tk()
    root.withdraw()

    # 打开Excel文件对话框选择文件
    excel_file_path = askopenfilename(title="选择Excel文件", filetypes=[("Excel Files", "*.xlsx;*.xls")])
    if not excel_file_path:
        print("未选择Excel文件,程序退出。")
        exit()

    # 打开保存VCF文件对话框指定保存路径
    vcf_file_path = asksaveasfilename(title="保存VCF文件", defaultextension=".vcf", filetypes=[("VCF Files", "*.vcf")])
    if not vcf_file_path:
        print("未指定VCF文件保存路径,程序退出。")
        exit()

    excel_to_vcf(excel_file_path, vcf_file_path)

好了,分享就到这里结束了,下次见

Logo

DAMO开发者矩阵,由阿里巴巴达摩院和中国互联网协会联合发起,致力于探讨最前沿的技术趋势与应用成果,搭建高质量的交流与分享平台,推动技术创新与产业应用链接,围绕“人工智能与新型计算”构建开放共享的开发者生态。

更多推荐