当前位置: 首页 > backend >正文

Python项目源码57:数据格式转换工具1.0(csv+json+excel+sqlite3)

1.智能路径处理:自动识别并修正文件扩展名,根据转换类型自动建议目标路径,实时路径格式验证,自动补全缺失的文件扩展名。

2.增强型预览功能:使用pandastable库实现表格预览,第三方模块自己安装一下,自动加载前10行数据,支持实时预览更新,自动调整列宽,SQLite表名自动检测。

pip install pandastable

3.改进的UI交互:分栏式布局(左侧控制/右侧预览),自动保存路径建议,智能表名检测(SQLite),实时错误反馈,一键清空预览。

4.使用说明:选择转换类型,浏览选择源文件(自动生成目标路径建议),查看右侧数据预览确认数据正确性,(数据库转换需要输入/选择表名),点击"开始转换"按钮
在这里插入图片描述

# -*- coding: utf-8 -*-
# @Author : 小红牛
# 微信公众号:WdPython
import os
import tkinter as tk
from tkinter import ttk, filedialog, messagebox
from tkinter.simpledialog import askstring
import sqlite3
import pandas as pd
from pandastable import Tableclass DataConverterApp:def __init__(self, root):self.root = rootself.root.title("智能数据转换工具1.0")self.root.geometry("1200x700")# 初始化变量self.source_path = tk.StringVar()self.target_path = tk.StringVar()self.table_name = tk.StringVar()self.conversion_type = tk.StringVar(value="excel2json")# 文件类型映射self.file_extensions = {"excel": ("Excel文件", ".xlsx"),"json": ("JSON文件", ".json"),"csv": ("CSV文件", ".csv"),"sqlite": ("SQLite数据库", ".db")}# 创建界面self.create_widgets()self.setup_preview_table()def setup_preview_table(self):"""初始化数据预览表格"""self.preview_frame = ttk.Frame(self.preview_container)self.preview_frame.pack(fill=tk.BOTH, expand=True)self.ptable = Table(self.preview_frame, showtoolbar=False, showstatusbar=True)self.ptable.show()def create_widgets(self):# 主容器main_frame = ttk.Frame(self.root, padding=20)main_frame.pack(fill=tk.BOTH, expand=True)# 左侧控制面板control_frame = ttk.Frame(main_frame, width=400)control_frame.pack(side=tk.LEFT, fill=tk.Y)# 右侧预览面板self.preview_container = ttk.LabelFrame(main_frame, text="数据预览", padding=10)self.preview_container.pack(side=tk.RIGHT, fill=tk.BOTH, expand=True)# 转换类型选择type_frame = ttk.LabelFrame(control_frame, text="转换类型", padding=10)type_frame.pack(fill=tk.X, pady=5)conversion_types = [("Excel → JSON", "excel2json"),("JSON → Excel", "json2excel"),("CSV → JSON", "csv2json"),("JSON → CSV", "json2csv"),("SQLite → Excel", "sqlite2excel"),("Excel → SQLite", "excel2sqlite"),("SQLite → CSV", "sqlite2csv"),("CSV → SQLite", "csv2sqlite")]for text, value in conversion_types:rb = ttk.Radiobutton(type_frame, text=text, variable=self.conversion_type,value=value, command=self.update_ui)rb.pack(anchor=tk.W, pady=2)# 源文件设置source_frame = ttk.LabelFrame(control_frame, text="源文件设置", padding=10)source_frame.pack(fill=tk.X, pady=5)ttk.Label(source_frame, text="源文件路径:").pack(anchor=tk.W)ttk.Entry(source_frame, textvariable=self.source_path, width=50).pack(side=tk.LEFT, fill=tk.X, expand=True)ttk.Button(source_frame, text="浏览...", command=self.browse_source).pack(side=tk.RIGHT)# 目标文件设置target_frame = ttk.LabelFrame(control_frame, text="目标设置", padding=10)target_frame.pack(fill=tk.X, pady=5)ttk.Label(target_frame, text="目标路径:").pack(anchor=tk.W)ttk.Entry(target_frame, textvariable=self.target_path, width=50).pack(side=tk.LEFT, fill=tk.X, expand=True)ttk.Button(target_frame, text="浏览...", command=self.browse_target).pack(side=tk.RIGHT)# 数据库表名设置self.table_frame = ttk.LabelFrame(control_frame, text="数据库设置", padding=10)ttk.Label(self.table_frame, text="表名:").pack(side=tk.LEFT)ttk.Entry(self.table_frame, textvariable=self.table_name).pack(side=tk.LEFT, fill=tk.X, expand=True)# 操作按钮btn_frame = ttk.Frame(control_frame)btn_frame.pack(fill=tk.X, pady=10)ttk.Button(btn_frame, text="开始转换", command=self.start_conversion).pack(side=tk.LEFT, padx=5)ttk.Button(btn_frame, text="清空预览", command=self.clear_preview).pack(side=tk.LEFT, padx=5)ttk.Button(btn_frame, text="退出", command=self.root.quit).pack(side=tk.RIGHT, padx=5)# 绑定路径变化事件self.source_path.trace_add("write", self.update_preview)self.table_name.trace_add("write", self.update_preview)self.update_ui()def get_conversion_info(self):"""获取当前转换类型信息"""ct = self.conversion_type.get()source_type = ct.split("2")[0]target_type = ct.split("2")[1]return source_type, target_typedef update_ui(self):"""更新界面元素"""source_type, target_type = self.get_conversion_info()# 更新数据库设置可见性if "sqlite" in self.conversion_type.get():self.table_frame.pack(fill=tk.X, pady=5)else:self.table_frame.pack_forget()# 自动更新目标路径后缀current_target = self.target_path.get()if current_target:base, _ = os.path.splitext(current_target)new_ext = self.file_extensions[target_type][1]self.target_path.set(f"{base}{new_ext}")def browse_source(self):"""选择源文件"""source_type, _ = self.get_conversion_info()file_type = self.file_extensions[source_type]path = filedialog.askopenfilename(title="选择源文件",filetypes=[file_type, ("所有文件", "*.*")])if path:self.source_path.set(path)self.auto_suggest_target_path(path)def auto_suggest_target_path(self, source_path):"""自动生成目标路径建议"""source_type, target_type = self.get_conversion_info()base = os.path.splitext(source_path)[0]new_ext = self.file_extensions[target_type][1]suggested_path = f"{base}_converted{new_ext}"self.target_path.set(suggested_path)def browse_target(self):"""选择目标路径"""_, target_type = self.get_conversion_info()file_type = self.file_extensions[target_type]path = filedialog.asksaveasfilename(title="保存目标文件",defaultextension=file_type[1],filetypes=[file_type, ("所有文件", "*.*")])if path:self.target_path.set(path)def clear_preview(self):"""清空预览"""self.ptable.clearTable()self.ptable.model.df = pd.DataFrame()self.ptable.redraw()def load_preview_data(self):"""加载预览数据"""source_path = self.source_path.get()if not source_path:return Nonetry:source_type, _ = self.get_conversion_info()if source_type == "excel":return pd.read_excel(source_path, nrows=10)elif source_type == "csv":return pd.read_csv(source_path, nrows=10)elif source_type == "json":return pd.read_json(source_path).head(10)elif source_type == "sqlite":conn = sqlite3.connect(source_path)table_name = self.table_name.get() or self.detect_table_name(conn)if table_name:return pd.read_sql_query(f"SELECT * FROM {table_name} LIMIT 10", conn)return Noneexcept Exception as e:messagebox.showerror("预览错误", f"无法加载数据: {str(e)}")return Nonedef detect_table_name(self, conn):"""自动检测数据库表名"""cursor = conn.cursor()cursor.execute("SELECT name FROM sqlite_master WHERE type='table';")tables = cursor.fetchall()if tables:return askstring("选择表", "检测到多个表,请选择:", initialvalue=tables[0][0])return Nonedef update_preview(self, *args):"""更新数据预览"""df = self.load_preview_data()if df is not None:self.ptable.model.df = dfself.ptable.redraw()self.ptable.autoResizeColumns()def start_conversion(self):"""执行转换操作"""source_path = self.source_path.get()target_path = self.target_path.get()conversion_type = self.conversion_type.get()table_name = self.table_name.get()try:# 自动修正目标路径后缀_, target_type = self.get_conversion_info()target_ext = self.file_extensions[target_type][1]if not target_path.endswith(target_ext):target_path = f"{os.path.splitext(target_path)[0]}{target_ext}"self.target_path.set(target_path)# 执行转换逻辑if conversion_type == "excel2json":df = pd.read_excel(source_path)df.to_json(target_path, orient='records', indent=4)elif conversion_type == "json2excel":df = pd.read_json(source_path)df.to_excel(target_path, index=False)elif conversion_type == "csv2json":df = pd.read_csv(source_path)df.to_json(target_path, orient='records', indent=4)elif conversion_type == "json2csv":df = pd.read_json(source_path)df.to_csv(target_path, index=False)elif conversion_type == "sqlite2excel":conn = sqlite3.connect(source_path)table_name = table_name or self.detect_table_name(conn)df = pd.read_sql_query(f"SELECT * FROM {table_name}", conn)df.to_excel(target_path, index=False)elif conversion_type == "excel2sqlite":df = pd.read_excel(source_path)conn = sqlite3.connect(target_path)df.to_sql(table_name or "Sheet1", conn, if_exists='replace', index=False)elif conversion_type == "sqlite2csv":conn = sqlite3.connect(source_path)table_name = table_name or self.detect_table_name(conn)df = pd.read_sql_query(f"SELECT * FROM {table_name}", conn)df.to_csv(target_path, index=False)elif conversion_type == "csv2sqlite":df = pd.read_csv(source_path)conn = sqlite3.connect(target_path)df.to_sql(table_name or "CSV_Data", conn, if_exists='replace', index=False)messagebox.showinfo("成功", f"文件已成功保存到:\n{target_path}")self.update_preview()except Exception as e:messagebox.showerror("错误", f"转换失败: {str(e)}")if __name__ == "__main__":root = tk.Tk()app = DataConverterApp(root)root.mainloop()

完毕!!感谢您的收看

----------★★跳转到历史博文集合★★----------
我的零基础Python教程,Python入门篇 进阶篇 视频教程 Py安装py项目 Python模块 Python爬虫 Json Xpath 正则表达式 Selenium Etree CssGui程序开发 Tkinter Pyqt5 列表元组字典数据可视化 matplotlib 词云图 Pyecharts 海龟画图 Pandas Bug处理 电脑小知识office自动化办公 编程工具 NumPy Pygame

http://www.xdnf.cn/news/4018.html

相关文章:

  • C++ 类与对象(下)—— 进阶特性与底层机制解析(构造函数初始化,类型转换,static成员,友元,内部类,匿名对象)
  • 基于 HTML 和 CSS 实现的 3D 翻转卡片效果
  • WebRTC 服务器之SRS服务器概述和环境搭建
  • 【算法笔记】动态规划基础(二):背包dp
  • TopK题-快速选择方法
  • 数据结构实验8.1:图的基本操作
  • 联邦学习的深度解析,有望打破数据孤岛
  • 005-nlohmann/json 基础方法-C++开源库108杰
  • Sim Studio 是一个开源的代理工作流程构建器。Sim Studio 的界面是一种轻量级、直观的方式,可快速构建和部署LLMs与您最喜欢的工具连接
  • 网络安全自动化:找准边界才能筑牢安全防线
  • 数据结构中 数组、链表、图的概念
  • 深入理解CSS盒子模型
  • 如何使用QWidgets设计一个类似于Web Toast的控件?
  • 【Java ee初阶】多线程(5)
  • Electron 架构详解:主进程与渲染进程的协作机制
  • [低代码 + AI] 明道云与 Dify 的三种融合实践方式详解
  • FreeRTOS菜鸟入门(十一)·信号量·二值、计数、递归以及互斥信号量的区别·优先级翻转以及继承机制详解
  • 英伟达语音识别模型论文速读:Token-and-Duration Transducer(TDT)架构
  • Android 控件CalendarView、TextClock用法
  • Notebook.ai 开源程序是一套工具,供作家、游戏设计师和角色扮演者创建宏伟的宇宙 - 以及其中的一切
  • GZ人博会自然资源系统(测绘)备考笔记
  • 25:三大分类器原理
  • 小刚说C语言刷题—1038编程求解数学中的分段函数
  • brpc 安装及使用
  • MVC、MVP、MVVM三大架构区别
  • HTML05:超链接标签及应用
  • C++笔记之反射、Qt中的反射系统、虚幻引擎中的反射系统
  • 利用jQuery 实现多选标签下拉框,提升表单交互体验
  • 动态指令参数:根据组件状态调整指令行为
  • AI Agent开发第50课-机器学习的基础-线性回归如何应用在商业场景中