优化上一篇的gui界面生成

复制代码
import pandas as pd
import json
import datetime
import os
import tkinter as tk
from tkinter import filedialog


def convert_to_excel():
    now = datetime.datetime.now()
    formatted_time = now.strftime("%Y-%m-%d_%H-%M-%S")

    # Open file dialog for selecting a file
    file_path = filedialog.askopenfilename(filetypes=[('Text files', '*.txt')])

    # Check if a file was selected
    if file_path:
        # Read JSON data from the selected file with permissive error handling
        with open(file_path, 'r', encoding='utf-8', errors='replace') as file:
            json_data = json.load(file)

        # Extract the 'data' part from JSON
        data_list = json_data.get("data", [])

        # Convert to DataFrame
        df = pd.DataFrame(data_list)

        # Save the DataFrame to an Excel file
        output_file_path = f"{formatted_time}.xlsx"
        df.to_excel(output_file_path, index=False)

        # Display a success message
        result_label.config(text=f"Conversion successful. File saved as {output_file_path}")


# Create the main application window
app = tk.Tk()
app.title("JSON to Excel Converter")

# Calculate the center coordinates for the window
screen_width = app.winfo_screenwidth()
screen_height = app.winfo_screenheight()
window_width = 400  # Adjust the width as needed
window_height = 150  # Adjust the height as needed

x_position = (screen_width - window_width) // 2
y_position = (screen_height - window_height) // 2

# Set the window geometry
app.geometry(f"{window_width}x{window_height}+{x_position}+{y_position}")

# Create a button for converting to Excel
convert_button = tk.Button(app, text="Convert to Excel", command=convert_to_excel)
convert_button.pack(pady=20)

# Create a label for displaying the result message
result_label = tk.Label(app, text="")
result_label.pack()

# Run the application loop
app.mainloop()
相关推荐
B1118521Y461 天前
flask的使用
后端·python·flask
Learn Beyond Limits1 天前
Transfer Learning|迁移学习
人工智能·python·深度学习·神经网络·机器学习·ai·吴恩达
love530love1 天前
【保姆级教程】阿里 Wan2.1-T2V-14B 模型本地部署全流程:从环境配置到视频生成(附避坑指南)
人工智能·windows·python·开源·大模型·github·音视频
He1955011 天前
Go初级之十:错误处理与程序健壮性
开发语言·python·golang
和鲸社区1 天前
《斯坦福CS336》作业1开源,从0手搓大模型|代码复现+免环境配置
人工智能·python·深度学习·计算机视觉·语言模型·自然语言处理·nlp
豌豆花下猫1 天前
Python 潮流周刊#118:Python 异步为何不够流行?(摘要)
后端·python·ai
THMAIL1 天前
深度学习从入门到精通 - LSTM与GRU深度剖析:破解长序列记忆遗忘困境
人工智能·python·深度学习·算法·机器学习·逻辑回归·lstm
wheeldown1 天前
【数学建模】数据预处理入门:从理论到动手操作
python·数学建模·matlab·python3.11
多打代码1 天前
2025.09.05 用队列实现栈 & 有效的括号 & 删除字符串中的所有相邻重复项
python·算法
@CLoudbays_Martin111 天前
为什么动态视频业务内容不可以被CDN静态缓存?
java·运维·服务器·javascript·网络·python·php