Python excel转vcf
import os
import pandas as pd
import tkinter as tk
from tkinter import filedialog
import logging# 设置日志记录
logging.basicConfig(filename="app.log", level=logging.DEBUG, format="%(asctime)s - %(levelname)s - %(message)s")def excel_to_vcf(excel_file, vcf_file):
try:
df = pd.read_excel(excel_file)with open(vcf_file, 'w') as f:
for index, row in df.iterrows():
name = row['姓名']
phone = row['电话']
email = row['电子邮件']vcf_entry = f"BEGIN:VCARD\nVERSION:3.0\nFN:{name}\nTEL:{phone}\nEMAIL:{email}\nEND:VCARD\n"f.write(vcf_entry)except Exception as e:
logging.exception(f"转换失败!{str(e)}")
label_status.config(text=f"错误:转换失败!请查看日志文件 'app.log'")else:
label_status.config(text="转换完成!VCF文件已保存。")
logging.info(f"成功转换Excel文件 '{excel_file}' 为VCF文件 '{vcf_file}'")def browse_excel_file():
file_path = filedialog.askopenfilename(filetypes=[("Excel files", "*.xlsx")])
entry_excel_file.delete(0, tk.END)
entry_excel_file.insert(0, file_path)def browse_vcf_file():
initialfile = os.path.splitext(os.path.basename(entry_excel_file.get()))[0]
file_path = filedialog.asksaveasfilename(defaultextension=".vcf", filetypes=[("VCF files", "*.vcf")], initialfile=initialfile)
entry_vcf_file.delete(0, tk.END)
entry_vcf_file.insert(0, file_path)def convert_to_vcf():
excel_file = entry_excel_file.get()
vcf_file = entry_vcf_file.get()
if not excel_file or not vcf_file:
label_status.config(text="错误:请选择Excel文件和保存路径")
return
excel_to_vcf(excel_file, vcf_file)# 创建图形界面
root = tk.Tk()
root.title("Excel转换为VCF (Arwei)")
root.geometry("400x200")# 添加控件
label_select_excel = tk.Label(root, text="选择Excel文件:")
label_select_excel.pack()entry_excel_file = tk.Entry(root, width=40)
entry_excel_file.pack()button_browse_excel = tk.Button(root, text="浏览", command=browse_excel_file)
button_browse_excel.pack()label_select_vcf_file = tk.Label(root, text="选择保存路径和文件名:")
label_select_vcf_file.pack()entry_vcf_file = tk.Entry(root, width=40)
entry_vcf_file.pack()button_browse_vcf_file = tk.Button(root, text="浏览", command=browse_vcf_file)
button_browse_vcf_file.pack()button_convert = tk.Button(root, text="转换为VCF", command=convert_to_vcf)
button_convert.pack()label_status = tk.Label(root, text="")
label_status.pack()# 开始图形界面的事件循环
root.mainloop()pyinstaller --onefile --windowed --hidden-import openpyxl excelvcf.py
文章评论